Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/set/downgrade-php83.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
use Rector\DowngradePhp83\Rector\Class_\DowngradeReadonlyAnonymousClassRector;
use Rector\ValueObject\PhpVersion;
use Rector\DowngradePhp83\Rector\ClassConst\DowngradeTypedClassConstRector;
use Rector\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->phpVersion(PhpVersion::PHP_82);
$rectorConfig->rules([
DowngradeTypedClassConstRector::class,
DowngradeReadonlyAnonymousClassRector::class,
DowngradeDynamicClassConstFetchRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DowngradeDynamicClassConstFetchRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector\Fixture;

class Fixture
{
public const string FOO = 'foo';

public function run()
{
$someValue = 'FOO';
Fixture::{$someValue};
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector\Fixture;

class Fixture
{
public const string FOO = 'foo';

public function run()
{
$someValue = 'FOO';
constant(Fixture::class . '::' . $someValue);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector\Fixture;

class SkipNamedClassConstFetch
{
public const FOO = 'foo';

public function run()
{
return self::FOO;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeDynamicClassConstFetchRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp83\Rector\ClassConstFetch;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://wiki.php.net/rfc/dynamic_class_constant_fetch
*
* @see \Rector\Tests\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector\DowngradeDynamicClassConstFetchRectorTest
*/
final class DowngradeDynamicClassConstFetchRector extends AbstractRector
{
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassConstFetch::class];
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change dynamic class const fetch Example::{$constName} to constant(Example::class . \'::\' . $constName)',
[
new CodeSample(
<<<'CODE_SAMPLE'
$value = Example::{$constName};
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$value = constant(Example::class . '::' . $constName);
CODE_SAMPLE
),
]
);
}

/**
* @param ClassConstFetch $node
*/
public function refactor(Node $node): ?Node
{
if ($node->name instanceof Identifier) {
return null;
}

return $this->nodeFactory->createFuncCall('constant', [
new Concat(
new Concat(new ClassConstFetch($node->class, new Identifier('class')), new String_('::')),
$node->name
),
]);
}
}
Loading