Skip to content

Commit 886ae80

Browse files
committed
[code-quality] Add VariableConstFetchToClassConstFetchRector
1 parent 1dce928 commit 886ae80

9 files changed

Lines changed: 200 additions & 5 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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\ClassWithConstants;
8+
9+
final class SkipDynamicName
10+
{
11+
public function run(ClassWithConstants $classWithConstants, $dynamic)
12+
{
13+
return $classWithConstants::$dynamic;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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\ClassWithConstants;
8+
9+
final class SkipKnownClass
10+
{
11+
public function run()
12+
{
13+
return ClassWithConstants::NAME;
14+
}
15+
}
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\ClassWithConstants;
8+
9+
final class SomeClass
10+
{
11+
public function run(ClassWithConstants $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\ClassWithConstants;
26+
27+
final class SomeClass
28+
{
29+
public function run(ClassWithConstants $classWithConstants)
30+
{
31+
return \Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Source\ClassWithConstants::NAME;
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Source;
4+
5+
final class ClassWithConstants
6+
{
7+
public const NAME = 'SomeName';
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class VariableConstFetchToClassConstFetchRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector;
6+
use Rector\Config\RectorConfig;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(VariableConstFetchToClassConstFetchRector::class);
10+
};
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\CodeQuality\Rector\ClassConstFetch;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\ClassConstFetch;
10+
use PHPStan\Type\ObjectType;
11+
use Rector\Rector\AbstractRector;
12+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
13+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
14+
15+
/**
16+
* @see \Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\VariableConstFetchToClassConstFetchRectorTest
17+
*/
18+
final class VariableConstFetchToClassConstFetchRector extends AbstractRector
19+
{
20+
public function getRuleDefinition(): RuleDefinition
21+
{
22+
return new RuleDefinition(
23+
'Change variable class constant fetch to direct class constant fetch',
24+
[
25+
new CodeSample(
26+
<<<'CODE_SAMPLE'
27+
class SomeClass
28+
{
29+
public function run(AnotherClass $anotherClass)
30+
{
31+
return $anotherClass::CONSTANT_NAME;
32+
}
33+
}
34+
CODE_SAMPLE
35+
,
36+
<<<'CODE_SAMPLE'
37+
class SomeClass
38+
{
39+
public function run(AnotherClass $anotherClass)
40+
{
41+
return AnotherClass::CONSTANT_NAME;
42+
}
43+
}
44+
CODE_SAMPLE
45+
),
46+
]
47+
);
48+
}
49+
50+
/**
51+
* @return array<class-string<Node>>
52+
*/
53+
public function getNodeTypes(): array
54+
{
55+
return [ClassConstFetch::class];
56+
}
57+
58+
/**
59+
* @param ClassConstFetch $node
60+
*/
61+
public function refactor(Node $node): ?ClassConstFetch
62+
{
63+
if (! $node->class instanceof Expr) {
64+
return null;
65+
}
66+
67+
if (! $node->name instanceof Node\Identifier) {
68+
return null;
69+
}
70+
71+
$constantName = $this->getName($node->name);
72+
73+
$classObjectType = $this->getType($node->class);
74+
if (! $classObjectType instanceof ObjectType) {
75+
return null;
76+
}
77+
78+
if (! $classObjectType->hasConstant($constantName)->yes()) {
79+
return null;
80+
}
81+
82+
$node->class = new Node\Name\FullyQualified($classObjectType->getClassName());
83+
84+
return $node;
85+
}
86+
}

rules/CodingStyle/ClassNameImport/NamespaceBeforeClassNameResolver.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ public function resolve(FullyQualifiedObjectType $fullyQualifiedObjectType): str
1515

1616
return $className === $shortName
1717
? ''
18-
: substr(
19-
$className,
20-
0,
21-
-strlen($shortName) - 1
22-
);
18+
: substr($className, 0, -strlen($shortName) - 1);
2319
}
2420
}

src/Config/Level/CodeQualityLevel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Rector\CodeQuality\Rector\Class_\ConvertStaticToSelfRector;
1616
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
1717
use Rector\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector;
18+
use Rector\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector;
1819
use Rector\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector;
1920
use Rector\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector;
2021
use Rector\CodeQuality\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector;
@@ -153,6 +154,7 @@ final class CodeQualityLevel
153154
IssetOnPropertyObjectToPropertyExistsRector::class,
154155
NewStaticToNewSelfRector::class,
155156
UnwrapSprintfOneArgumentRector::class,
157+
VariableConstFetchToClassConstFetchRector::class,
156158
SwitchNegatedTernaryRector::class,
157159
SingularSwitchToIfRector::class,
158160
SimplifyIfNullableReturnRector::class,

0 commit comments

Comments
 (0)