File tree Expand file tree Collapse file tree
rules-tests/CodeQuality/Rector/ClassConstFetch/ConvertStaticPrivateConstantToSelfRector
rules/CodeQuality/Rector/ClassConstFetch Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \CodeQuality \Rector \ClassConstFetch \ConvertStaticPrivateConstantToSelfRector \Fixture ;
4+
5+ use Rector \Tests \CodeQuality \Rector \ClassConstFetch \ConvertStaticPrivateConstantToSelfRector \Source \ParentClass ;
6+
7+ final class FinalClassWithInheritedConstant extends ParentClass
8+ {
9+ public function run (): void
10+ {
11+ return static ::FAILURE ;
12+ return static ::SUCCESS ;
13+ }
14+ }
15+
16+ ?>
17+ -----
18+ <?php
19+
20+ namespace Rector \Tests \CodeQuality \Rector \ClassConstFetch \ConvertStaticPrivateConstantToSelfRector \Fixture ;
21+
22+ use Rector \Tests \CodeQuality \Rector \ClassConstFetch \ConvertStaticPrivateConstantToSelfRector \Source \ParentClass ;
23+
24+ final class FinalClassWithInheritedConstant extends ParentClass
25+ {
26+ public function run (): void
27+ {
28+ return self ::FAILURE ;
29+ return self ::SUCCESS ;
30+ }
31+ }
32+
33+ ?>
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \CodeQuality \Rector \ClassConstFetch \ConvertStaticPrivateConstantToSelfRector \Source ;
4+
5+ class ParentClass
6+ {
7+ public const FAILURE = 1 ;
8+ protected const SUCCESS = 0 ;
9+ }
Original file line number Diff line number Diff line change 1717 * @see \Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\ConvertStaticPrivateConstantToSelfRectorTest
1818 *
1919 * @see https://3v4l.org/8Y0ba
20+ * @see https://3v4l.org/ZIeA1
2021 * @see https://phpstan.org/r/11d4c850-1a40-4fae-b665-291f96104d11
2122 */
2223final class ConvertStaticPrivateConstantToSelfRector extends AbstractRector
2324{
2425 public function getRuleDefinition (): RuleDefinition
2526 {
2627 return new RuleDefinition (
27- 'Replaces static::* access to private constants with self::* ' ,
28+ 'Replaces static::* constant access with self::* for private constants and in final classes. ' ,
2829 [
2930 new CodeSample (
3031 <<<'CODE_SAMPLE'
32+ class Foo
33+ {
34+ private const BAR = 'bar';
35+ public const BAZ = 'baz';
36+
37+ public function run()
38+ {
39+ $bar = static::BAR;
40+ $baz = static::BAZ;
41+ }
42+ }
43+ CODE_SAMPLE
44+ ,
45+ <<<'CODE_SAMPLE'
46+ class Foo
47+ {
48+ private const BAR = 'bar';
49+ public const BAZ = 'baz';
50+
51+ public function run()
52+ {
53+ $bar = self::BAR;
54+ $baz = static::BAZ;
55+ }
56+ }
57+ CODE_SAMPLE
58+ ),
59+ new CodeSample (
60+ <<<'CODE_SAMPLE'
3161final class Foo
3262{
3363 private const BAR = 'bar';
64+ public const BAZ = 'baz';
3465
3566 public function run()
3667 {
3768 $bar = static::BAR;
69+ $baz = static::BAZ;
3870 }
3971}
4072CODE_SAMPLE
@@ -43,10 +75,12 @@ public function run()
4375final class Foo
4476{
4577 private const BAR = 'bar';
78+ public const BAZ = 'baz';
4679
4780 public function run()
4881 {
4982 $bar = self::BAR;
83+ $baz = self::BAZ;
5084 }
5185}
5286CODE_SAMPLE
@@ -68,7 +102,7 @@ public function getNodeTypes(): array
68102 */
69103 public function refactor (Node $ node ): ?Class_
70104 {
71- if ($ node ->getConstants () === []) {
105+ if ($ node ->getConstants () === [] && ! $ node -> isFinal () ) {
72106 return null ;
73107 }
74108
You can’t perform that action at this time.
0 commit comments