Skip to content

Commit 36b2cf4

Browse files
committed
include override in cild class
1 parent 886ae80 commit 36b2cf4

7 files changed

Lines changed: 124 additions & 4 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
?>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace 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
}

rules/CodeQuality/Rector/ClassConstFetch/VariableConstFetchToClassConstFetchRector.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Expr\ClassConstFetch;
10+
use PhpParser\Node\Identifier;
11+
use PhpParser\Node\Name\FullyQualified;
1012
use PHPStan\Type\ObjectType;
1113
use Rector\Rector\AbstractRector;
1214
use 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
}

0 commit comments

Comments
 (0)