File tree 5 files changed +89
-0
lines changed
tests/PHPStan/Rules/Constants
5 files changed +89
-0
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,7 @@ lint:
108
108
--exclude tests/PHPStan/Rules/Properties/data/final-property-hooks.php \
109
109
--exclude tests/PHPStan/Rules/Properties/data/final-properties.php \
110
110
--exclude tests/PHPStan/Rules/Properties/data/property-in-interface-explicit-abstract.php \
111
+ --exclude tests/PHPStan/Rules/Constants/data/final-private-const.php \
111
112
src tests
112
113
113
114
cs :
Original file line number Diff line number Diff line change 46
46
- PHPStan\Rules\Constants\FinalConstantRule
47
47
- PHPStan\Rules\Constants\MagicConstantContextRule
48
48
- PHPStan\Rules\Constants\NativeTypedClassConstantRule
49
+ - PHPStan\Rules\Constants\FinalPrivateConstantRule
49
50
- PHPStan\Rules\EnumCases\EnumCaseAttributesRule
50
51
- PHPStan\Rules\Exceptions\NoncapturingCatchRule
51
52
- PHPStan\Rules\Exceptions\ThrowExpressionRule
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 \Rules \Rule ;
9
+ use PHPStan \Rules \RuleErrorBuilder ;
10
+ use PHPStan \ShouldNotHappenException ;
11
+ use function sprintf ;
12
+
13
+ /** @implements Rule<ClassConst> */
14
+ final class FinalPrivateConstantRule implements Rule
15
+ {
16
+
17
+ public function getNodeType (): string
18
+ {
19
+ return ClassConst::class;
20
+ }
21
+
22
+ public function processNode (Node $ node , Scope $ scope ): array
23
+ {
24
+ if (!$ scope ->isInClass ()) {
25
+ throw new ShouldNotHappenException ();
26
+ }
27
+ $ classReflection = $ scope ->getClassReflection ();
28
+
29
+ if (!$ node ->isFinal ()) {
30
+ return [];
31
+ }
32
+
33
+ if (!$ node ->isPrivate ()) {
34
+ return [];
35
+ }
36
+
37
+ $ errors = [];
38
+ foreach ($ node ->consts as $ classConstNode ) {
39
+ $ errors [] = RuleErrorBuilder::message (sprintf (
40
+ 'Private constant %s::%s() cannot be final as it is never overridden by other classes. ' ,
41
+ $ classReflection ->getDisplayName (),
42
+ $ classConstNode ->name ->name ,
43
+ ))->identifier ('classConstant.finalPrivate ' )->nonIgnorable ()->build ();
44
+ }
45
+
46
+ return $ errors ;
47
+ }
48
+
49
+ }
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 \Rules \Rule ;
6
+ use PHPStan \Testing \RuleTestCase ;
7
+
8
+ /** @extends RuleTestCase<FinalPrivateConstantRule> */
9
+ class FinalPrivateConstantRuleTest extends RuleTestCase
10
+ {
11
+
12
+ protected function getRule (): Rule
13
+ {
14
+ return new FinalPrivateConstantRule ();
15
+ }
16
+
17
+ public function testRule (): void
18
+ {
19
+ $ this ->analyse ([__DIR__ . '/data/final-private-const.php ' ], [
20
+ [
21
+ 'Private constant FinalPrivateConstants\User::FINAL_PRIVATE() cannot be final as it is never overridden by other classes. ' ,
22
+ 8 ,
23
+ ],
24
+ ]);
25
+ }
26
+
27
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace FinalPrivateConstants ;
4
+
5
+ class User
6
+ {
7
+ private const PRIVATE = 'mailto: example.org ' ;
8
+ final private const FINAL_PRIVATE = 'mailto: example.org ' ;
9
+ final protected const PROTECTED = 'mailto: example.org ' ;
10
+ final public const PUBLIC = 'mailto: example.org ' ;
11
+ }
You can’t perform that action at this time.
0 commit comments