Skip to content

Commit 2d0ffeb

Browse files
authored
[DowngradePhp83] Add DowngradeDynamicClassConstFetchRector (#280)
* [DowngradePhp83] Add DowngradeDynamicClassConstFetchRector * cleasns up * cleasns up
1 parent 2910a2a commit 2d0ffeb

File tree

6 files changed

+157
-0
lines changed

6 files changed

+157
-0
lines changed

config/set/downgrade-php83.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
use Rector\DowngradePhp83\Rector\Class_\DowngradeReadonlyAnonymousClassRector;
77
use Rector\ValueObject\PhpVersion;
88
use Rector\DowngradePhp83\Rector\ClassConst\DowngradeTypedClassConstRector;
9+
use Rector\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector;
910

1011
return static function (RectorConfig $rectorConfig): void {
1112
$rectorConfig->phpVersion(PhpVersion::PHP_82);
1213
$rectorConfig->rules([
1314
DowngradeTypedClassConstRector::class,
1415
DowngradeReadonlyAnonymousClassRector::class,
16+
DowngradeDynamicClassConstFetchRector::class,
1517
]);
1618
};
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\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeDynamicClassConstFetchRectorTest 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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector\Fixture;
6+
7+
class Fixture
8+
{
9+
public const string FOO = 'foo';
10+
11+
public function run()
12+
{
13+
$someValue = 'FOO';
14+
Fixture::{$someValue};
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
declare(strict_types=1);
23+
24+
namespace Rector\Tests\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector\Fixture;
25+
26+
class Fixture
27+
{
28+
public const string FOO = 'foo';
29+
30+
public function run()
31+
{
32+
$someValue = 'FOO';
33+
constant(Fixture::class . '::' . $someValue);
34+
}
35+
}
36+
37+
?>
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\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector\Fixture;
6+
7+
class SkipNamedClassConstFetch
8+
{
9+
public const FOO = 'foo';
10+
11+
public function run()
12+
{
13+
return self::FOO;
14+
}
15+
}
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\Config\RectorConfig;
6+
use Rector\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DowngradeDynamicClassConstFetchRector::class);
10+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DowngradePhp83\Rector\ClassConstFetch;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\BinaryOp\Concat;
9+
use PhpParser\Node\Expr\ClassConstFetch;
10+
use PhpParser\Node\Identifier;
11+
use PhpParser\Node\Scalar\String_;
12+
use Rector\Rector\AbstractRector;
13+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
14+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
15+
16+
/**
17+
* @changelog https://wiki.php.net/rfc/dynamic_class_constant_fetch
18+
*
19+
* @see \Rector\Tests\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector\DowngradeDynamicClassConstFetchRectorTest
20+
*/
21+
final class DowngradeDynamicClassConstFetchRector extends AbstractRector
22+
{
23+
/**
24+
* @return array<class-string<Node>>
25+
*/
26+
public function getNodeTypes(): array
27+
{
28+
return [ClassConstFetch::class];
29+
}
30+
31+
public function getRuleDefinition(): RuleDefinition
32+
{
33+
return new RuleDefinition(
34+
'Change dynamic class const fetch Example::{$constName} to constant(Example::class . \'::\' . $constName)',
35+
[
36+
new CodeSample(
37+
<<<'CODE_SAMPLE'
38+
$value = Example::{$constName};
39+
CODE_SAMPLE
40+
,
41+
<<<'CODE_SAMPLE'
42+
$value = constant(Example::class . '::' . $constName);
43+
CODE_SAMPLE
44+
),
45+
]
46+
);
47+
}
48+
49+
/**
50+
* @param ClassConstFetch $node
51+
*/
52+
public function refactor(Node $node): ?Node
53+
{
54+
if ($node->name instanceof Identifier) {
55+
return null;
56+
}
57+
58+
return $this->nodeFactory->createFuncCall('constant', [
59+
new Concat(
60+
new Concat(new ClassConstFetch($node->class, new Identifier('class')), new String_('::')),
61+
$node->name
62+
),
63+
]);
64+
}
65+
}

0 commit comments

Comments
 (0)