-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Playground rule - PromoteParameterRule
- Loading branch information
1 parent
7f90fec
commit 59ccf55
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Playground; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function sprintf; | ||
|
||
/** | ||
* @template TNodeType of Node | ||
* @implements Rule<TNodeType> | ||
*/ | ||
final class PromoteParameterRule implements Rule | ||
{ | ||
|
||
/** | ||
* @param Rule<TNodeType> $rule | ||
* @param class-string<TNodeType> $nodeType | ||
*/ | ||
public function __construct( | ||
private Rule $rule, | ||
private string $nodeType, | ||
private bool $parameterValue, | ||
private string $parameterName, | ||
) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return $this->nodeType; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->parameterValue) { | ||
return []; | ||
} | ||
|
||
if ($this->nodeType !== $this->rule->getNodeType()) { | ||
return []; | ||
} | ||
|
||
$errors = []; | ||
foreach ($this->rule->processNode($node, $scope) as $error) { | ||
$errors[] = RuleErrorBuilder::message($error->getMessage()) | ||
->identifier('phpstanPlayground.configParameter') | ||
->tip(sprintf('This error would be reported if the <fg=cyan>%s: true</> parameter was enabled in your <fg=cyan>%%configurationFile%%</>.', $this->parameterName)) | ||
->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
tests/PHPStan/Rules/Playground/PromoteParameterRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Playground; | ||
|
||
use PHPStan\Node\ClassPropertiesNode; | ||
use PHPStan\Reflection\ConstructorsHelper; | ||
use PHPStan\Rules\Properties\UninitializedPropertyRule; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<PromoteParameterRule<ClassPropertiesNode>> | ||
*/ | ||
class PromoteParameterRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new PromoteParameterRule( | ||
new UninitializedPropertyRule(new ConstructorsHelper( | ||
self::getContainer(), | ||
[], | ||
)), | ||
ClassPropertiesNode::class, | ||
false, | ||
'checkUninitializedProperties', | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/promote-parameter.php'], [ | ||
[ | ||
'Class PromoteParameter\Foo has an uninitialized property $test. Give it default value or assign it in the constructor.', | ||
5, | ||
'This error would be reported if the <fg=cyan>checkUninitializedProperties: true</> parameter was enabled in your <fg=cyan>%configurationFile%</>.', | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace PromoteParameter; | ||
|
||
class Foo | ||
{ | ||
|
||
public int $test; | ||
|
||
} |