mirror of
https://github.com/rectorphp/rector.git
synced 2024-07-01 07:03:32 +00:00
[Downgrade PHP 5.5] Add DowngradeClassConstantToStringRector (#1194)
* [Downgrade PHP 5.5] Add DowngradeClassConstantToStringRector * Complete DowngradeLevelSetList down to 5.3
This commit is contained in:
parent
08ce1d06ed
commit
e777553f59
16
config/set/downgrade-php55.php
Normal file
16
config/set/downgrade-php55.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Rector\Core\ValueObject\PhpVersion;
|
||||
use Rector\DowngradePhp55\Rector\ClassConstFetch\DowngradeClassConstantToStringRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_54);
|
||||
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(DowngradeClassConstantToStringRector::class);
|
||||
};
|
12
config/set/level/down-to-php53.php
Normal file
12
config/set/level/down-to-php53.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Set\ValueObject\DowngradeLevelSetList;
|
||||
use Rector\Set\ValueObject\DowngradeSetList;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$containerConfigurator->import(DowngradeLevelSetList::DOWN_TO_PHP_54);
|
||||
$containerConfigurator->import(DowngradeSetList::PHP_54);
|
||||
};
|
12
config/set/level/down-to-php54.php
Normal file
12
config/set/level/down-to-php54.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Set\ValueObject\DowngradeLevelSetList;
|
||||
use Rector\Set\ValueObject\DowngradeSetList;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$containerConfigurator->import(DowngradeLevelSetList::DOWN_TO_PHP_55);
|
||||
$containerConfigurator->import(DowngradeSetList::PHP_55);
|
||||
};
|
12
config/set/level/down-to-php55.php
Normal file
12
config/set/level/down-to-php55.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Set\ValueObject\DowngradeLevelSetList;
|
||||
use Rector\Set\ValueObject\DowngradeSetList;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$containerConfigurator->import(DowngradeLevelSetList::DOWN_TO_PHP_56);
|
||||
$containerConfigurator->import(DowngradeSetList::PHP_56);
|
||||
};
|
|
@ -42,4 +42,19 @@ final class DowngradeLevelSetList implements SetListInterface
|
|||
* @var string
|
||||
*/
|
||||
public const DOWN_TO_PHP_56 = __DIR__ . '/../../../config/set/level/down-to-php56.php';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const DOWN_TO_PHP_55 = __DIR__ . '/../../../config/set/level/down-to-php55.php';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const DOWN_TO_PHP_54 = __DIR__ . '/../../../config/set/level/down-to-php54.php';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const DOWN_TO_PHP_53 = __DIR__ . '/../../../config/set/level/down-to-php53.php';
|
||||
}
|
||||
|
|
|
@ -18,6 +18,11 @@ final class DowngradeSetList implements SetListInterface
|
|||
*/
|
||||
public const PHP_54 = __DIR__ . '/../../../config/set/downgrade-php54.php';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PHP_55 = __DIR__ . '/../../../config/set/downgrade-php55.php';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp55\Rector\ClassConstFetch\DowngradeClassConstantToStringRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class DowngradeClassConstantToStringRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp55\Rector\ClassConstFetch\DowngradeClassConstantToStringRector\Fixture;
|
||||
|
||||
abstract class AnotherClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return self::class . '/' . static::class;
|
||||
}
|
||||
}
|
||||
|
||||
final class ChildClass extends AnotherClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return parent::Class;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp55\Rector\ClassConstFetch\DowngradeClassConstantToStringRector\Fixture;
|
||||
|
||||
abstract class AnotherClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return get_class() . '/' . get_called_class();
|
||||
}
|
||||
}
|
||||
|
||||
final class ChildClass extends AnotherClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return get_parent_class();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp55\Rector\ClassConstFetch\DowngradeClassConstantToStringRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
}
|
||||
|
||||
echo SomeClass::class;
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DowngradePhp55\Rector\ClassConstFetch\DowngradeClassConstantToStringRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
}
|
||||
|
||||
echo 'Rector\Tests\DowngradePhp55\Rector\ClassConstFetch\DowngradeClassConstantToStringRector\Fixture\SomeClass';
|
||||
|
||||
?>
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\DowngradePhp55\Rector\ClassConstFetch\DowngradeClassConstantToStringRector;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(DowngradeClassConstantToStringRector::class);
|
||||
};
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DowngradePhp55\Rector\ClassConstFetch;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @changelog https://wiki.php.net/rfc/class_name_scalars
|
||||
*
|
||||
* @see Rector\Tests\DowngradePhp55\Rector\ClassConstFetch\DowngradeClassConstantToStringRector\DowngradeClassConstantToStringRectorTest
|
||||
*/
|
||||
class DowngradeClassConstantToStringRector extends AbstractRector
|
||||
{
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
'Replace <class>::class constant by string class names',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class AnotherClass
|
||||
{
|
||||
}
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return \AnotherClass::class;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class AnotherClass
|
||||
{
|
||||
}
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return 'AnotherClass';
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassConstFetch::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassConstFetch $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (
|
||||
! $node->name instanceof Identifier
|
||||
|| strtolower($node->name->name) !== 'class'
|
||||
|| ! $node->class instanceof Name
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$className = $node->class->toString();
|
||||
|
||||
$func = match (strtolower($className)) {
|
||||
'self' => 'get_class',
|
||||
'static' => 'get_called_class',
|
||||
'parent' => 'get_parent_class',
|
||||
default => null,
|
||||
};
|
||||
|
||||
if ($func !== null) {
|
||||
return $this->nodeFactory->createFuncCall($func);
|
||||
}
|
||||
|
||||
return new String_($className);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user