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 SkipDocType
10+ {
11+ /**
12+ * @param ChildClassNotWithConstant $classWithConstants
13+ */
14+ public function run ($ classWithConstants )
15+ {
16+ return $ classWithConstants ::ORIGINAL_VALUE ;
17+ }
18+ }
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+ final class SkipUnknownClass
8+ {
9+ public function run ($ classWithConstants )
10+ {
11+ return $ classWithConstants ::ORIGINAL_VALUE ;
12+ }
13+ }
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 77use PhpParser \Node ;
88use PhpParser \Node \Expr ;
99use PhpParser \Node \Expr \ClassConstFetch ;
10+ use PhpParser \Node \Identifier ;
11+ use PhpParser \Node \Name \FullyQualified ;
1012use PHPStan \Type \ObjectType ;
1113use Rector \Rector \AbstractRector ;
1214use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
@@ -64,13 +66,16 @@ 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
73- $ classObjectType = $ this ->getType ($ node ->class );
78+ $ classObjectType = $ this ->nodeTypeResolver -> getNativeType ($ node ->class );
7479 if (! $ classObjectType instanceof ObjectType) {
7580 return null ;
7681 }
@@ -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