File tree Expand file tree Collapse file tree
rules-tests/CodeQuality/Rector/ClassConstFetch/VariableConstFetchToClassConstFetchRector
rules/CodeQuality/Rector/ClassConstFetch Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Rector \Tests \CodeQuality \Rector \ClassConstFetch \VariableConstFetchToClassConstFetchRector \Fixture ;
6+
7+ use Rector \Tests \CodeQuality \Rector \ClassConstFetch \VariableConstFetchToClassConstFetchRector \Source \ChildClassNotWithConstant ;
8+
9+ final class AllowOverride
10+ {
11+ public function run (ChildClassNotWithConstant $ classWithConstants )
12+ {
13+ return $ classWithConstants ::ORIGINAL_VALUE ;
14+ }
15+ }
16+
17+ ?>
18+ -----
19+ <?php
20+
21+ declare (strict_types=1 );
22+
23+ namespace Rector \Tests \CodeQuality \Rector \ClassConstFetch \VariableConstFetchToClassConstFetchRector \Fixture ;
24+
25+ use Rector \Tests \CodeQuality \Rector \ClassConstFetch \VariableConstFetchToClassConstFetchRector \Source \ChildClassNotWithConstant ;
26+
27+ final class AllowOverride
28+ {
29+ public function run (ChildClassNotWithConstant $ classWithConstants )
30+ {
31+ return \Rector \Tests \CodeQuality \Rector \ClassConstFetch \VariableConstFetchToClassConstFetchRector \Source \ChildClassNotWithConstant::ORIGINAL_VALUE ;
32+ }
33+ }
34+
35+ ?>
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Rector \Tests \CodeQuality \Rector \ClassConstFetch \VariableConstFetchToClassConstFetchRector \Fixture ;
6+
7+ use Rector \Tests \CodeQuality \Rector \ClassConstFetch \VariableConstFetchToClassConstFetchRector \Source \ChildClassNotWithConstant ;
8+
9+ final class ParentHopFetch
10+ {
11+ public function run (ChildClassNotWithConstant $ classWithConstants )
12+ {
13+ return $ classWithConstants ::NAME ;
14+ }
15+ }
16+
17+ ?>
18+ -----
19+ <?php
20+
21+ declare (strict_types=1 );
22+
23+ namespace Rector \Tests \CodeQuality \Rector \ClassConstFetch \VariableConstFetchToClassConstFetchRector \Fixture ;
24+
25+ use Rector \Tests \CodeQuality \Rector \ClassConstFetch \VariableConstFetchToClassConstFetchRector \Source \ChildClassNotWithConstant ;
26+
27+ final class ParentHopFetch
28+ {
29+ public function run (ChildClassNotWithConstant $ classWithConstants )
30+ {
31+ return \Rector \Tests \CodeQuality \Rector \ClassConstFetch \VariableConstFetchToClassConstFetchRector \Source \ChildClassNotWithConstant::NAME ;
32+ }
33+ }
34+
35+ ?>
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Rector \Tests \CodeQuality \Rector \ClassConstFetch \VariableConstFetchToClassConstFetchRector \Fixture ;
6+
7+ use Rector \Tests \CodeQuality \Rector \ClassConstFetch \VariableConstFetchToClassConstFetchRector \Source \ChildClassNotWithConstant ;
8+
9+ final class SkipUnknownClass
10+ {
11+ public function run ($ classWithConstants )
12+ {
13+ return $ classWithConstants ::ORIGINAL_VALUE ;
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Rector \Tests \CodeQuality \Rector \ClassConstFetch \VariableConstFetchToClassConstFetchRector \Source ;
6+
7+ final class ChildClassNotWithConstant extends ClassWithConstants
8+ {
9+ public const ORIGINAL_VALUE = 456 ;
10+ }
Original file line number Diff line number Diff line change 11<?php
22
3+ declare (strict_types=1 );
4+
35namespace Rector \Tests \CodeQuality \Rector \ClassConstFetch \VariableConstFetchToClassConstFetchRector \Source ;
46
5- final class ClassWithConstants
7+ class ClassWithConstants
68{
79 public const NAME = 'SomeName ' ;
10+
11+ public const ORIGINAL_VALUE = 123 ;
812}
Original file line number Diff line number Diff line change 44
55namespace Rector \CodeQuality \Rector \ClassConstFetch ;
66
7+ use PhpParser \Node \Identifier ;
8+ use PhpParser \Node \Name \FullyQualified ;
79use PhpParser \Node ;
810use PhpParser \Node \Expr ;
911use PhpParser \Node \Expr \ClassConstFetch ;
@@ -64,11 +66,14 @@ public function refactor(Node $node): ?ClassConstFetch
6466 return null ;
6567 }
6668
67- if (! $ node ->name instanceof Node \ Identifier) {
69+ if (! $ node ->name instanceof Identifier) {
6870 return null ;
6971 }
7072
7173 $ constantName = $ this ->getName ($ node ->name );
74+ if (! is_string ($ constantName )) {
75+ return null ;
76+ }
7277
7378 $ classObjectType = $ this ->getType ($ node ->class );
7479 if (! $ classObjectType instanceof ObjectType) {
@@ -79,7 +84,7 @@ public function refactor(Node $node): ?ClassConstFetch
7984 return null ;
8085 }
8186
82- $ node ->class = new Node \ Name \ FullyQualified ($ classObjectType ->getClassName ());
87+ $ node ->class = new FullyQualified ($ classObjectType ->getClassName ());
8388
8489 return $ node ;
8590 }
You can’t perform that action at this time.
0 commit comments