mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 16:30:51 +00:00
decouple AbstractChangeParentClassRector
This commit is contained in:
parent
413f57d620
commit
d0f9d77df3
48
src/Rector/AbstractChangeParentClassRector.php
Normal file
48
src/Rector/AbstractChangeParentClassRector.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
|
||||
abstract class AbstractChangeParentClassRector extends AbstractRector
|
||||
{
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof Class_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->getParentClassName($node) === $this->getOldClassName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$node->extends = new Name('\\' . $this->getNewClassName());
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
abstract protected function getOldClassName(): string;
|
||||
|
||||
abstract protected function getNewClassName(): string;
|
||||
|
||||
private function getParentClassName(Class_ $classNode): string
|
||||
{
|
||||
if (! $classNode->extends) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/** @var Name $parentClassName */
|
||||
$parentClassNameNode = $classNode->extends;
|
||||
|
||||
/** @var Node\Name\FullyQualified $fsqName */
|
||||
$fsqName = $parentClassNameNode->getAttribute('resolvedName');
|
||||
|
||||
return $fsqName->toString();
|
||||
}
|
||||
}
|
|
@ -2,14 +2,10 @@
|
|||
|
||||
namespace Rector\Rector\Contrib\Symfony;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\Deprecation\SetNames;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\Rector\AbstractChangeParentClassRector;
|
||||
|
||||
/**
|
||||
* @todo decouple abstract class AbstractReplaceParentClass
|
||||
*
|
||||
* Ref: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#validator
|
||||
*
|
||||
* Converts all:
|
||||
|
@ -18,7 +14,7 @@ use Rector\Rector\AbstractRector;
|
|||
* into:
|
||||
* Symfony\Component\Validator\Test\ConstraintValidatorTestCase
|
||||
*/
|
||||
final class ConstraintValidatorTestClassRenameRector extends AbstractRector
|
||||
final class ConstraintValidatorTestClassRenameRector extends AbstractChangeParentClassRector
|
||||
{
|
||||
public function getSetName(): string
|
||||
{
|
||||
|
@ -30,40 +26,12 @@ final class ConstraintValidatorTestClassRenameRector extends AbstractRector
|
|||
return 4.0;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof Class_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $node->extends) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var Node\Name $parentClassName */
|
||||
$parentClassNameNode = $node->extends;
|
||||
|
||||
/** @var Node\Name\FullyQualified $fsqName */
|
||||
$fsqName = $parentClassNameNode->getAttribute('resolvedName');
|
||||
|
||||
return $fsqName->toString() === $this->getOldClassName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$node->extends = new Node\Name('\\' . $this->getNewClassName());
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function getOldClassName(): string
|
||||
protected function getOldClassName(): string
|
||||
{
|
||||
return 'Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest';
|
||||
}
|
||||
|
||||
private function getNewClassName(): string
|
||||
protected function getNewClassName(): string
|
||||
{
|
||||
return 'Symfony\Component\Validator\Test\ConstraintValidatorTestCase';
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user