mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 19:53:14 +01:00
Merge pull request #74 from RectorPHP/merge-abstract-change-parent-class
Merge abstract change parent class to ClassReplacerRector
This commit is contained in:
commit
d60408692a
@ -10,7 +10,7 @@
|
||||
"php": "^7.1",
|
||||
"cweagans/composer-patches": "^1.6",
|
||||
"nette/utils": "^2.4",
|
||||
"nikic/php-parser": "4.0.x-dev as 3.1.1",
|
||||
"nikic/php-parser": "4.0.x-dev#ed8a744c as 3.1.1",
|
||||
"roave/better-reflection": "^2.0",
|
||||
"symfony/console": "^3.3",
|
||||
"symfony/dependency-injection": "^3.3",
|
||||
@ -55,7 +55,7 @@
|
||||
],
|
||||
"process-test-projects": [
|
||||
"bin/rector process temp/nette/vendor --config src/config/level/nette/all.yml",
|
||||
"bin/rector process temp/symfony/src/Symfony/Component --config src/config/level/symfony/all.yml"
|
||||
"bin/rector process temp/symfony/src/Symfony/Bundle --config src/config/level/symfony/all.yml"
|
||||
]
|
||||
},
|
||||
"config": {
|
||||
|
@ -1,43 +0,0 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\Node\Attribute;
|
||||
|
||||
abstract class AbstractChangeParentClassRector extends AbstractRector
|
||||
{
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof Class_ || $node->extends === null || $node->isAnonymous()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var FullyQualified $fqnName */
|
||||
$fqnName = $node->extends->getAttribute(Attribute::RESOLVED_NAME);
|
||||
|
||||
if ($fqnName instanceof FullyQualified) {
|
||||
return $fqnName->toString() === $this->getOldClassName();
|
||||
}
|
||||
|
||||
if ($node->extends instanceof FullyQualified) {
|
||||
return $node->extends->toString() === $this->getOldClassName();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$node->extends = new FullyQualified($this->getNewClassName());
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
abstract protected function getOldClassName(): string;
|
||||
|
||||
abstract protected function getNewClassName(): string;
|
||||
}
|
@ -37,6 +37,11 @@ use Rector\Tests\Rector\Contrib\Symfony\HttpKernel\GetterToPropertyRector\Source
|
||||
*/
|
||||
final class CommandToConstructorInjectionRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const COMMAND_CLASS = 'Symfony\Component\Console\Command\Command';
|
||||
|
||||
/**
|
||||
* @var ServiceFromKernelResolver
|
||||
*/
|
||||
@ -121,6 +126,6 @@ final class CommandToConstructorInjectionRector extends AbstractRector
|
||||
private function replaceParentContainerAwareCommandWithCommand(Node $node): void
|
||||
{
|
||||
$classNode = $node->getAttribute(Attribute::CLASS_NODE);
|
||||
$classNode->extends = new FullyQualified('Symfony\Component\Console\Command\Command');
|
||||
$classNode->extends = new FullyQualified(self::COMMAND_CLASS);
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +0,0 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Contrib\Symfony\Validator;
|
||||
|
||||
use Rector\Rector\AbstractChangeParentClassRector;
|
||||
|
||||
/**
|
||||
* Ref: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#validator
|
||||
* Symfony\Component\Validator\Test\ConstraintValidatorTestCase
|
||||
*/
|
||||
final class ConstraintValidatorTestClassRenameRector extends AbstractChangeParentClassRector
|
||||
{
|
||||
protected function getOldClassName(): string
|
||||
{
|
||||
return 'Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest';
|
||||
}
|
||||
|
||||
protected function getNewClassName(): string
|
||||
{
|
||||
return 'Symfony\Component\Validator\Test\ConstraintValidatorTestCase';
|
||||
}
|
||||
}
|
@ -43,14 +43,13 @@ final class ClassReplacerRector extends AbstractRector
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node instanceof Name) {
|
||||
$newName = $this->getNewName($node->toString());
|
||||
$newName = $this->resolveNewNameFromNode($node);
|
||||
|
||||
return new FullyQualified($newName);
|
||||
}
|
||||
|
||||
if ($node instanceof Use_) {
|
||||
$name = $this->resolveNameFromNode($node);
|
||||
$newName = $this->getNewName($name);
|
||||
$newName = $this->resolveNewNameFromNode($node);
|
||||
|
||||
if ($this->isUseStatmenetAlreadyPresent($node, $newName)) {
|
||||
$this->shouldRemoveNode = true;
|
||||
@ -60,11 +59,6 @@ final class ClassReplacerRector extends AbstractRector
|
||||
return null;
|
||||
}
|
||||
|
||||
private function getNewName(string $oldName): string
|
||||
{
|
||||
return $this->oldToNewClasses[$oldName];
|
||||
}
|
||||
|
||||
private function isUseStatmenetAlreadyPresent(Use_ $useNode, string $newName): bool
|
||||
{
|
||||
/** @var UseStatements $useStatments */
|
||||
@ -73,9 +67,22 @@ final class ClassReplacerRector extends AbstractRector
|
||||
return in_array($newName, $useStatments->getUseStatements(), true);
|
||||
}
|
||||
|
||||
private function resolveNewNameFromNode(Node $node): string
|
||||
{
|
||||
$name = $this->resolveNameFromNode($node);
|
||||
|
||||
return $this->oldToNewClasses[$name];
|
||||
}
|
||||
|
||||
private function resolveNameFromNode(Node $node): string
|
||||
{
|
||||
if ($node instanceof Name) {
|
||||
// resolved name has priority, as it is FQN
|
||||
$resolvedName = $node->getAttribute(Attribute::RESOLVED_NAME);
|
||||
if ($resolvedName instanceof FullyQualified) {
|
||||
return $resolvedName->toString();
|
||||
}
|
||||
|
||||
return $node->toString();
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,10 @@
|
||||
rectors:
|
||||
- Rector\Rector\Contrib\Symfony\Validator\ConstraintUrlOptionRector
|
||||
- Rector\Rector\Contrib\Symfony\Validator\ConstraintValidatorTestClassRenameRector
|
||||
- Rector\Rector\Contrib\Symfony\Form\FormIsValidRector
|
||||
- Rector\Rector\Contrib\Symfony\Form\StringFormTypeToClassRector
|
||||
- Rector\Rector\Contrib\Symfony\VarDumper\VarDumperTestTraitMethodArgsRector
|
||||
- Rector\Rector\Contrib\Symfony\DependencyInjection\ContainerBuilderCompileEnvArgumentRector
|
||||
- Rector\Rector\Contrib\Symfony\DependencyInjection\CompilerAddPassPriorityRector
|
||||
Rector\Rector\Contrib\Symfony\Validator\ConstraintUrlOptionRector: ~
|
||||
Rector\Rector\Contrib\Symfony\Form\FormIsValidRector: ~
|
||||
Rector\Rector\Contrib\Symfony\Form\StringFormTypeToClassRector: ~
|
||||
Rector\Rector\Contrib\Symfony\VarDumper\VarDumperTestTraitMethodArgsRector: ~
|
||||
Rector\Rector\Contrib\Symfony\DependencyInjection\ContainerBuilderCompileEnvArgumentRector: ~
|
||||
Rector\Rector\Contrib\Symfony\DependencyInjection\CompilerAddPassPriorityRector: ~
|
||||
|
||||
Rector\Rector\Dynamic\ClassReplacerRector:
|
||||
'Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest': 'Symfony\Component\Validator\Test\ConstraintValidatorTestCase'
|
||||
|
@ -1,25 +0,0 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Contrib\Symfony\Validator\ConstraintValidatorTestClassRenameRector;
|
||||
|
||||
use Rector\Rector\Contrib\Symfony\Validator\ConstraintValidatorTestClassRenameRector;
|
||||
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 [ConstraintValidatorTestClassRenameRector::class];
|
||||
}
|
||||
}
|
@ -37,13 +37,27 @@ final class Test extends TestCase
|
||||
$this->assertNotSame([], $oldToNewClasses);
|
||||
}
|
||||
|
||||
public function testProcessing(): void
|
||||
/**
|
||||
* @dataProvider provideTestFiles()
|
||||
*/
|
||||
public function testProcessing(string $testedFile, string $expectedFile): void
|
||||
{
|
||||
$refactoredFileContent = $this->fileProcessor->processFileWithRectorsToString(
|
||||
new SplFileInfo(__DIR__ . '/wrong/wrong.php.inc'),
|
||||
new SplFileInfo($testedFile),
|
||||
[ClassReplacerRector::class]
|
||||
);
|
||||
|
||||
$this->assertStringEqualsFile(__DIR__ . '/correct/correct.php.inc', $refactoredFileContent);
|
||||
$this->assertStringEqualsFile($expectedFile, $refactoredFileContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
*/
|
||||
public function provideTestFiles(): array
|
||||
{
|
||||
return [
|
||||
[__DIR__ . '/wrong/wrong.php.inc', __DIR__ . '/correct/correct.php.inc'],
|
||||
[__DIR__ . '/wrong/wrong2.php.inc', __DIR__ . '/correct/correct2.php.inc'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
|
||||
|
||||
class MyCustomValidatorTest extends AbstractConstraintValidatorTest
|
||||
class MyCustomValidatorTest extends \Symfony\Component\Validator\Test\ConstraintValidatorTestCase
|
||||
{
|
||||
// ...
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user