mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-29 19:37:55 +01:00
[Symfony] add FrameworkBundleClassReplacements
This commit is contained in:
parent
9f5e26f7e6
commit
608b30ae84
@ -0,0 +1,70 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Contrib\Symfony;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use Rector\Deprecation\SetNames;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
/**
|
||||
* Ref.: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#frameworkbundle
|
||||
*
|
||||
* FrameworkBundle classes replaced by new ones
|
||||
*
|
||||
* @todo extract AbstractClassReplacerRector
|
||||
*/
|
||||
final class FrameworkBundleClassReplacementsRector extends AbstractRector
|
||||
{
|
||||
public function getSetName(): string
|
||||
{
|
||||
return SetNames::SYMFONY;
|
||||
}
|
||||
|
||||
public function sinceVersion(): float
|
||||
{
|
||||
return 4.0;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof Name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$fqnName = $node->toString();
|
||||
|
||||
if (! isset($this->getOldClassToNewClassMap()[$fqnName])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Name $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$newName = $this->getNewName($node->toString());
|
||||
|
||||
return new FullyQualified($newName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
public function getOldClassToNewClassMap(): array
|
||||
{
|
||||
return [
|
||||
'Symfony\Bundle\FrameworkBundle\DependencyInjectino\Compiler\SerializerPass' => 'Symfony\Component\Serializer\DependencyInjection\SerializerPass'
|
||||
];
|
||||
}
|
||||
|
||||
private function getNewName(string $oldName): string
|
||||
{
|
||||
return $this->getOldClassToNewClassMap()[$oldName];
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
$containerBuilder = new \Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
$containerBuilder->addCompilerPass(new \Symfony\Component\Serializer\DependencyInjection\SerializerPass);
|
@ -0,0 +1,25 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Contrib\Symfony\FrameworkBundleClassReplacementsRector;
|
||||
|
||||
use Rector\Rector\Contrib\Symfony\FrameworkBundleClassReplacementsRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class Test extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFileMatchesExpectedContent(
|
||||
__DIR__ . '/Wrong/wrong.php.inc',
|
||||
__DIR__ . '/Correct/correct.php.inc'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getRectorClasses(): array
|
||||
{
|
||||
return [FrameworkBundleClassReplacementsRector::class];
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
$containerBuilder = new \Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
$containerBuilder->addCompilerPass(new Symfony\Bundle\FrameworkBundle\DependencyInjectino\Compiler\SerializerPass);
|
Loading…
x
Reference in New Issue
Block a user