File tree Expand file tree Collapse file tree 3 files changed +108
-0
lines changed
tests/PHPStan/Rules/Playground Expand file tree Collapse file tree 3 files changed +108
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace PHPStan \Rules \Playground ;
4+
5+ use PhpParser \Node ;
6+ use PHPStan \Analyser \Scope ;
7+ use PHPStan \Rules \Rule ;
8+ use PHPStan \Rules \RuleErrorBuilder ;
9+ use function sprintf ;
10+
11+ /**
12+ * @template TNodeType of Node
13+ * @implements Rule<TNodeType>
14+ */
15+ final class PromoteParameterRule implements Rule
16+ {
17+
18+ /**
19+ * @param Rule<TNodeType> $rule
20+ * @param class-string<TNodeType> $nodeType
21+ */
22+ public function __construct (
23+ private Rule $ rule ,
24+ private string $ nodeType ,
25+ private bool $ parameterValue ,
26+ private string $ parameterName ,
27+ )
28+ {
29+ }
30+
31+ public function getNodeType (): string
32+ {
33+ return $ this ->nodeType ;
34+ }
35+
36+ public function processNode (Node $ node , Scope $ scope ): array
37+ {
38+ if ($ this ->parameterValue ) {
39+ return [];
40+ }
41+
42+ if ($ this ->nodeType !== $ this ->rule ->getNodeType ()) {
43+ return [];
44+ }
45+
46+ $ errors = [];
47+ foreach ($ this ->rule ->processNode ($ node , $ scope ) as $ error ) {
48+ $ errors [] = RuleErrorBuilder::message ($ error ->getMessage ())
49+ ->identifier ('phpstanPlayground.configParameter ' )
50+ ->tip (sprintf ('This error would be reported if the <fg=cyan>%s: true</> parameter was enabled in your <fg=cyan>%%configurationFile%%</>. ' , $ this ->parameterName ))
51+ ->build ();
52+ }
53+
54+ return $ errors ;
55+ }
56+
57+ }
Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace PHPStan \Rules \Playground ;
4+
5+ use PHPStan \Node \ClassPropertiesNode ;
6+ use PHPStan \Reflection \ConstructorsHelper ;
7+ use PHPStan \Rules \Properties \UninitializedPropertyRule ;
8+ use PHPStan \Rules \Rule ;
9+ use PHPStan \Testing \RuleTestCase ;
10+
11+ /**
12+ * @extends RuleTestCase<PromoteParameterRule<ClassPropertiesNode>>
13+ */
14+ class PromoteParameterRuleTest extends RuleTestCase
15+ {
16+
17+ protected function getRule (): Rule
18+ {
19+ return new PromoteParameterRule (
20+ new UninitializedPropertyRule (new ConstructorsHelper (
21+ self ::getContainer (),
22+ [],
23+ )),
24+ ClassPropertiesNode::class,
25+ false ,
26+ 'checkUninitializedProperties ' ,
27+ );
28+ }
29+
30+ public function testRule (): void
31+ {
32+ $ this ->analyse ([__DIR__ . '/data/promote-parameter.php ' ], [
33+ [
34+ 'Class PromoteParameter\Foo has an uninitialized property $test. Give it default value or assign it in the constructor. ' ,
35+ 5 ,
36+ 'This error would be reported if the <fg=cyan>checkUninitializedProperties: true</> parameter was enabled in your <fg=cyan>%configurationFile%</>. ' ,
37+ ],
38+ ]);
39+ }
40+
41+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace PromoteParameter ;
4+
5+ class Foo
6+ {
7+
8+ public int $ test ;
9+
10+ }
You can’t perform that action at this time.
0 commit comments