File tree Expand file tree Collapse file tree 5 files changed +117
-0
lines changed
tests/PHPStan/Rules/Constants Expand file tree Collapse file tree 5 files changed +117
-0
lines changed Original file line number Diff line number Diff line change 50
50
- PHPStan\Rules\Classes\NewStaticRule
51
51
- PHPStan\Rules\Classes\NonClassAttributeClassRule
52
52
- PHPStan\Rules\Classes\TraitAttributeClassRule
53
+ - PHPStan\Rules\Constants\FinalConstantRule
53
54
- PHPStan\Rules\Exceptions\ThrowExpressionRule
54
55
- PHPStan\Rules\Functions\ArrowFunctionAttributesRule
55
56
- PHPStan\Rules\Functions\ArrowFunctionReturnNullsafeByRefRule
Original file line number Diff line number Diff line change @@ -132,4 +132,9 @@ public function isInterfaceConstantImplicitlyFinal(): bool
132
132
return $ this ->versionId < 80100 ;
133
133
}
134
134
135
+ public function supportsFinalConstants (): bool
136
+ {
137
+ return $ this ->versionId >= 80100 ;
138
+ }
139
+
135
140
}
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace PHPStan \Rules \Constants ;
4
+
5
+ use PhpParser \Node ;
6
+ use PhpParser \Node \Stmt \ClassConst ;
7
+ use PHPStan \Analyser \Scope ;
8
+ use PHPStan \Php \PhpVersion ;
9
+ use PHPStan \Rules \Rule ;
10
+ use PHPStan \Rules \RuleErrorBuilder ;
11
+
12
+ /** @implements Rule<ClassConst> */
13
+ class FinalConstantRule implements Rule
14
+ {
15
+
16
+ private PhpVersion $ phpVersion ;
17
+
18
+ public function __construct (PhpVersion $ phpVersion )
19
+ {
20
+ $ this ->phpVersion = $ phpVersion ;
21
+ }
22
+
23
+ public function getNodeType (): string
24
+ {
25
+ return ClassConst::class;
26
+ }
27
+
28
+ public function processNode (Node $ node , Scope $ scope ): array
29
+ {
30
+ if (!$ node ->isFinal ()) {
31
+ return [];
32
+ }
33
+
34
+ if ($ this ->phpVersion ->supportsFinalConstants ()) {
35
+ return [];
36
+ }
37
+
38
+ return [
39
+ RuleErrorBuilder::message ('Final class constants are supported only on PHP 8.1 and later. ' )->nonIgnorable ()->build (),
40
+ ];
41
+ }
42
+
43
+ }
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace PHPStan \Rules \Constants ;
4
+
5
+ use PHPStan \Php \PhpVersion ;
6
+ use PHPStan \Rules \Rule ;
7
+ use PHPStan \Testing \RuleTestCase ;
8
+
9
+ /**
10
+ * @extends RuleTestCase<FinalConstantRule>
11
+ */
12
+ class FinalConstantRuleTest extends RuleTestCase
13
+ {
14
+
15
+ /** @var int */
16
+ private $ phpVersionId ;
17
+
18
+ protected function getRule (): Rule
19
+ {
20
+ return new FinalConstantRule (new PhpVersion ($ this ->phpVersionId ));
21
+ }
22
+
23
+ public function dataRule (): array
24
+ {
25
+ return [
26
+ [
27
+ 80000 ,
28
+ [
29
+ [
30
+ 'Final class constants are supported only on PHP 8.1 and later. ' ,
31
+ 9 ,
32
+ ],
33
+ ],
34
+ ],
35
+ [
36
+ 80100 ,
37
+ [],
38
+ ],
39
+ ];
40
+ }
41
+
42
+ /**
43
+ * @dataProvider dataRule
44
+ * @param int $phpVersionId
45
+ * @param mixed[] $errors
46
+ */
47
+ public function testRule (int $ phpVersionId , array $ errors ): void
48
+ {
49
+ if (!self ::$ useStaticReflectionProvider ) {
50
+ $ this ->markTestSkipped ('Test requires static reflection. ' );
51
+ }
52
+
53
+ $ this ->phpVersionId = $ phpVersionId ;
54
+ $ this ->analyse ([__DIR__ . '/data/final-constant.php ' ], $ errors );
55
+ }
56
+
57
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace FinalConstant ;
4
+
5
+ class Foo
6
+ {
7
+
8
+ const TEST = 1 ;
9
+ final const BAR = 2 ;
10
+
11
+ }
You can’t perform that action at this time.
0 commit comments