mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
decouple AbstractClassReplacerRector
This commit is contained in:
parent
608b30ae84
commit
b34a93fcdd
@ -52,3 +52,7 @@ parameters:
|
||||
SlevomatCodingStandard\Sniffs\Classes\UnusedPrivateElementsSniff:
|
||||
# will be used soon
|
||||
- packages/NodeTypeResolver/src/TypeContext.php
|
||||
|
||||
PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff:
|
||||
# long FQN classes that might not exist
|
||||
- src/Rector/Contrib/Symfony/FrameworkBundleClassReplacementsRector.php
|
||||
|
@ -13,7 +13,7 @@ final class NodeConnector extends NodeVisitorAbstract
|
||||
/**
|
||||
* @var Node
|
||||
*/
|
||||
private $stack;
|
||||
private $stack = [];
|
||||
|
||||
/**
|
||||
* @var Node
|
||||
|
41
src/Rector/AbstractClassReplacerRector.php
Normal file
41
src/Rector/AbstractClassReplacerRector.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
|
||||
abstract class AbstractClassReplacerRector extends AbstractRector
|
||||
{
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof Name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$fqnName = $node->toString();
|
||||
|
||||
return isset($this->getOldToNewClasses()[$fqnName]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Name $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$newName = $this->getNewName($node->toString());
|
||||
|
||||
return new FullyQualified($newName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
abstract protected function getOldToNewClasses(): array;
|
||||
|
||||
private function getNewName(string $oldName): string
|
||||
{
|
||||
return $this->getOldToNewClasses()[$oldName];
|
||||
}
|
||||
}
|
@ -2,21 +2,15 @@
|
||||
|
||||
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;
|
||||
use Rector\Rector\AbstractClassReplacerRector;
|
||||
|
||||
/**
|
||||
* 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
|
||||
final class FrameworkBundleClassReplacementsRector extends AbstractClassReplacerRector
|
||||
{
|
||||
public function getSetName(): string
|
||||
{
|
||||
@ -28,43 +22,13 @@ final class FrameworkBundleClassReplacementsRector extends AbstractRector
|
||||
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
|
||||
* @return string[]
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$newName = $this->getNewName($node->toString());
|
||||
|
||||
return new FullyQualified($newName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
public function getOldClassToNewClassMap(): array
|
||||
protected function getOldToNewClasses(): array
|
||||
{
|
||||
return [
|
||||
'Symfony\Bundle\FrameworkBundle\DependencyInjectino\Compiler\SerializerPass' => 'Symfony\Component\Serializer\DependencyInjection\SerializerPass'
|
||||
'Symfony\Bundle\FrameworkBundle\DependencyInjectino\Compiler\SerializerPass' => 'Symfony\Component\Serializer\DependencyInjection\SerializerPass',
|
||||
];
|
||||
}
|
||||
|
||||
private function getNewName(string $oldName): string
|
||||
{
|
||||
return $this->getOldClassToNewClassMap()[$oldName];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user