From 94cc4e84e862084f3b9c8bbfe024961791594052 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 23 Jun 2020 13:25:19 +0200 Subject: [PATCH] add rename class support in twig/latte as well --- .../config/local_config.neon | 0 .../config/local_config.xml | 0 .../config/local_config.yaml | 0 .../config/local_config.yml | 5 +++++ .../template/dummy_template.latte | 5 +++++ .../template/dummy_template.twig | 3 +++ ...lXmlRectorTest.php => RenameNonPhpTest.php} | 8 ++++++-- src/Console/Command/ProcessCommand.php | 12 ++++++------ .../NonPhpFileProcessor.php} | 17 +++++++++++++++-- .../AbstractFileSystemRectorTestCase.php | 13 +++++++------ src/Testing/PHPUnit/AbstractRectorTestCase.php | 13 +++++++------ src/ValueObject/StaticNonPhpFileSuffixes.php | 18 ++++++++++++++++++ 12 files changed, 72 insertions(+), 22 deletions(-) rename rules/renaming/tests/Rector/Class_/RenameClassRector/{FixtureRenameNeonYamlXml => FixtureRenameNonPhp}/config/local_config.neon (100%) rename rules/renaming/tests/Rector/Class_/RenameClassRector/{FixtureRenameNeonYamlXml => FixtureRenameNonPhp}/config/local_config.xml (100%) rename rules/renaming/tests/Rector/Class_/RenameClassRector/{FixtureRenameNeonYamlXml => FixtureRenameNonPhp}/config/local_config.yaml (100%) create mode 100644 rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/config/local_config.yml create mode 100644 rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/template/dummy_template.latte create mode 100644 rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/template/dummy_template.twig rename rules/renaming/tests/Rector/Class_/RenameClassRector/{RenameNeonYamlXmlRectorTest.php => RenameNonPhpTest.php} (76%) rename src/{NeonYaml/NeonYamlXmlProcessor.php => NonPhpFile/NonPhpFileProcessor.php} (83%) create mode 100644 src/ValueObject/StaticNonPhpFileSuffixes.php diff --git a/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNeonYamlXml/config/local_config.neon b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/config/local_config.neon similarity index 100% rename from rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNeonYamlXml/config/local_config.neon rename to rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/config/local_config.neon diff --git a/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNeonYamlXml/config/local_config.xml b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/config/local_config.xml similarity index 100% rename from rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNeonYamlXml/config/local_config.xml rename to rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/config/local_config.xml diff --git a/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNeonYamlXml/config/local_config.yaml b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/config/local_config.yaml similarity index 100% rename from rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNeonYamlXml/config/local_config.yaml rename to rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/config/local_config.yaml diff --git a/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/config/local_config.yml b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/config/local_config.yml new file mode 100644 index 00000000000..c2ecbda467f --- /dev/null +++ b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/config/local_config.yml @@ -0,0 +1,5 @@ +services: + Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\OldClass: null +----- +services: + Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\NewClass: null diff --git a/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/template/dummy_template.latte b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/template/dummy_template.latte new file mode 100644 index 00000000000..b3129cf048c --- /dev/null +++ b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/template/dummy_template.latte @@ -0,0 +1,5 @@ +{if \Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\OldClass::SOME_COSTANT === $value} +{/if} +----- +{if \Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\NewClass::SOME_COSTANT === $value} +{/if} diff --git a/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/template/dummy_template.twig b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/template/dummy_template.twig new file mode 100644 index 00000000000..0324d268227 --- /dev/null +++ b/rules/renaming/tests/Rector/Class_/RenameClassRector/FixtureRenameNonPhp/template/dummy_template.twig @@ -0,0 +1,3 @@ +{{ constant('Rector\\Renaming\\Tests\\Rector\\Class_\\RenameClassRector\\Source\\OldClass::SOME_COSTANT') }} +----- +{{ constant('Rector\\Renaming\\Tests\\Rector\\Class_\\RenameClassRector\\Source\\NewClass::SOME_COSTANT') }} diff --git a/rules/renaming/tests/Rector/Class_/RenameClassRector/RenameNeonYamlXmlRectorTest.php b/rules/renaming/tests/Rector/Class_/RenameClassRector/RenameNonPhpTest.php similarity index 76% rename from rules/renaming/tests/Rector/Class_/RenameClassRector/RenameNeonYamlXmlRectorTest.php rename to rules/renaming/tests/Rector/Class_/RenameClassRector/RenameNonPhpTest.php index 2dc9e37ee7c..3c3465612a4 100644 --- a/rules/renaming/tests/Rector/Class_/RenameClassRector/RenameNeonYamlXmlRectorTest.php +++ b/rules/renaming/tests/Rector/Class_/RenameClassRector/RenameNonPhpTest.php @@ -6,11 +6,12 @@ namespace Rector\Renaming\Tests\Rector\Class_\RenameClassRector; use Iterator; use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase; +use Rector\Core\ValueObject\StaticNonPhpFileSuffixes; use Rector\Renaming\Rector\Class_\RenameClassRector; use Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\NewClass; use Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\OldClass; -final class RenameNeonYamlXmlRectorTest extends AbstractRectorTestCase +final class RenameNonPhpTest extends AbstractRectorTestCase { /** * @dataProvider provideData() @@ -22,7 +23,10 @@ final class RenameNeonYamlXmlRectorTest extends AbstractRectorTestCase public function provideData(): Iterator { - return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureRenameNeonYamlXml', '#\.(neon|yaml|xml)$#'); + return $this->yieldFilesFromDirectory( + __DIR__ . '/FixtureRenameNonPhp', + StaticNonPhpFileSuffixes::getSuffixRegexPattern() + ); } /** diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index 7736a44d9e8..9921bf66ad2 100644 --- a/src/Console/Command/ProcessCommand.php +++ b/src/Console/Command/ProcessCommand.php @@ -16,7 +16,7 @@ use Rector\Core\Console\Output\OutputFormatterCollector; use Rector\Core\EventDispatcher\Event\AfterReportEvent; use Rector\Core\FileSystem\FilesFinder; use Rector\Core\Guard\RectorGuard; -use Rector\Core\NeonYaml\NeonYamlXmlProcessor; +use Rector\Core\NonPhpFile\NonPhpFileProcessor; use Rector\Core\PhpParser\NodeTraverser\RectorNodeTraverser; use Rector\Core\Stubs\StubLoader; use Symfony\Component\Console\Input\InputArgument; @@ -77,9 +77,9 @@ final class ProcessCommand extends AbstractCommand private $stubLoader; /** - * @var NeonYamlXmlProcessor + * @var NonPhpFileProcessor */ - private $neonYamlXmlProcessor; + private $nonPhpFileProcessor; /** * @var UnchangedFilesFilter @@ -111,7 +111,7 @@ final class ProcessCommand extends AbstractCommand OutputFormatterCollector $outputFormatterCollector, RectorNodeTraverser $rectorNodeTraverser, StubLoader $stubLoader, - NeonYamlXmlProcessor $neonYamlXmlProcessor, + NonPhpFileProcessor $nonPhpFileProcessor, ChangedFilesDetector $changedFilesDetector, UnchangedFilesFilter $unchangedFilesFilter, SymfonyStyle $symfonyStyle, @@ -126,7 +126,7 @@ final class ProcessCommand extends AbstractCommand $this->outputFormatterCollector = $outputFormatterCollector; $this->rectorNodeTraverser = $rectorNodeTraverser; $this->stubLoader = $stubLoader; - $this->neonYamlXmlProcessor = $neonYamlXmlProcessor; + $this->nonPhpFileProcessor = $nonPhpFileProcessor; $this->unchangedFilesFilter = $unchangedFilesFilter; parent::__construct(); @@ -234,7 +234,7 @@ final class ProcessCommand extends AbstractCommand // must run after PHP rectors, because they might change class names, and these class names must be changed in configs $neonYamlFileInfos = $this->filesFinder->findInDirectoriesAndFiles($source, ['neon', 'yaml', 'xml']); - $this->neonYamlXmlProcessor->runOnFileInfos($neonYamlFileInfos); + $this->nonPhpFileProcessor->runOnFileInfos($neonYamlFileInfos); $this->reportZeroCacheRectorsCondition(); diff --git a/src/NeonYaml/NeonYamlXmlProcessor.php b/src/NonPhpFile/NonPhpFileProcessor.php similarity index 83% rename from src/NeonYaml/NeonYamlXmlProcessor.php rename to src/NonPhpFile/NonPhpFileProcessor.php index b3f070f83b1..d65ce10a62a 100644 --- a/src/NeonYaml/NeonYamlXmlProcessor.php +++ b/src/NonPhpFile/NonPhpFileProcessor.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Rector\Core\NeonYaml; +namespace Rector\Core\NonPhpFile; use Nette\Utils\FileSystem; use Nette\Utils\Strings; @@ -12,7 +12,7 @@ use Rector\PSR4\Collector\RenamedClassesCollector; use Symfony\Component\Console\Style\SymfonyStyle; use Symplify\SmartFileSystem\SmartFileInfo; -final class NeonYamlXmlProcessor +final class NonPhpFileProcessor { /** * @var Configuration @@ -78,6 +78,19 @@ final class NeonYamlXmlProcessor $newContent = Strings::replace($newContent, '#' . preg_quote($oldClass, '#') . '#', $newClass); } + // process with double quotes too, e.g. in twig + foreach ($this->getOldToNewClasses() as $oldClass => $newClass) { + $doubleSlashOldClass = str_replace('\\', '\\\\', $oldClass); + $doubleSlashNewClass = str_replace('\\', '\\\\\\', $newClass); + + /** @var string $newContent */ + $newContent = Strings::replace( + $newContent, + '#' . preg_quote($doubleSlashOldClass, '#') . '#', + $doubleSlashNewClass + ); + } + return $newContent; } diff --git a/src/Testing/PHPUnit/AbstractFileSystemRectorTestCase.php b/src/Testing/PHPUnit/AbstractFileSystemRectorTestCase.php index a2be967e40b..87370670a4d 100644 --- a/src/Testing/PHPUnit/AbstractFileSystemRectorTestCase.php +++ b/src/Testing/PHPUnit/AbstractFileSystemRectorTestCase.php @@ -10,7 +10,8 @@ use Rector\Core\Application\FileProcessor; use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor; use Rector\Core\Configuration\Configuration; use Rector\Core\HttpKernel\RectorKernel; -use Rector\Core\NeonYaml\NeonYamlXmlProcessor; +use Rector\Core\NonPhpFile\NonPhpFileProcessor; +use Rector\Core\ValueObject\StaticNonPhpFileSuffixes; use Rector\FileSystemRector\Contract\FileSystemRectorInterface; use Rector\FileSystemRector\FileSystemFileProcessor; use ReflectionClass; @@ -35,9 +36,9 @@ abstract class AbstractFileSystemRectorTestCase extends AbstractGenericRectorTes private $fileProcessor; /** - * @var NeonYamlXmlProcessor + * @var NonPhpFileProcessor */ - private $neonYamlXmlProcessor; + private $nonPhpFileProcessor; protected function setUp(): void { @@ -50,7 +51,7 @@ abstract class AbstractFileSystemRectorTestCase extends AbstractGenericRectorTes $this->fileProcessor = self::$container->get(FileProcessor::class); $this->fileSystemFileProcessor = self::$container->get(FileSystemFileProcessor::class); $this->removedAndAddedFilesProcessor = self::$container->get(RemovedAndAddedFilesProcessor::class); - $this->neonYamlXmlProcessor = self::$container->get(NeonYamlXmlProcessor::class); + $this->nonPhpFileProcessor = self::$container->get(NonPhpFileProcessor::class); } /** @@ -92,8 +93,8 @@ abstract class AbstractFileSystemRectorTestCase extends AbstractGenericRectorTes continue; } - if (in_array($fileInfo->getSuffix(), ['neon', 'yaml', 'xml'], true)) { - $this->neonYamlXmlProcessor->processFileInfo($fileInfo); + if (in_array($fileInfo->getSuffix(), StaticNonPhpFileSuffixes::SUFFIXES, true)) { + $this->nonPhpFileProcessor->processFileInfo($fileInfo); } else { $this->fileProcessor->postFileRefactor($fileInfo); $this->fileProcessor->printToFile($fileInfo); diff --git a/src/Testing/PHPUnit/AbstractRectorTestCase.php b/src/Testing/PHPUnit/AbstractRectorTestCase.php index d4e2fc58ce0..9b70ec14434 100644 --- a/src/Testing/PHPUnit/AbstractRectorTestCase.php +++ b/src/Testing/PHPUnit/AbstractRectorTestCase.php @@ -16,12 +16,13 @@ use Rector\Core\Configuration\Option; use Rector\Core\Contract\Rector\PhpRectorInterface; use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\HttpKernel\RectorKernel; -use Rector\Core\NeonYaml\NeonYamlXmlProcessor; +use Rector\Core\NonPhpFile\NonPhpFileProcessor; use Rector\Core\Set\Set; use Rector\Core\Stubs\StubLoader; use Rector\Core\Testing\Application\EnabledRectorsProvider; use Rector\Core\Testing\Contract\RunnableInterface; use Rector\Core\Testing\Finder\RectorsFinder; +use Rector\Core\ValueObject\StaticNonPhpFileSuffixes; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\DependencyInjection\Container; @@ -73,9 +74,9 @@ abstract class AbstractRectorTestCase extends AbstractGenericRectorTestCase private $runnableRectorFactory; /** - * @var NeonYamlXmlProcessor + * @var NonPhpFileProcessor */ - private $neonYamlXmlProcessor; + private $nonPhpFileProcessor; protected function setUp(): void { @@ -113,7 +114,7 @@ abstract class AbstractRectorTestCase extends AbstractGenericRectorTestCase $symfonyStyle->setVerbosity(OutputInterface::VERBOSITY_QUIET); $this->fileProcessor = static::$container->get(FileProcessor::class); - $this->neonYamlXmlProcessor = static::$container->get(NeonYamlXmlProcessor::class); + $this->nonPhpFileProcessor = static::$container->get(NonPhpFileProcessor::class); $this->parameterProvider = static::$container->get(ParameterProvider::class); // needed for PHPStan, because the analyzed file is just create in /temp @@ -290,8 +291,8 @@ abstract class AbstractRectorTestCase extends AbstractGenericRectorTestCase $removedAndAddedFilesProcessor = self::$container->get(RemovedAndAddedFilesProcessor::class); $removedAndAddedFilesProcessor->run(); - } elseif (in_array($originalFileInfo->getSuffix(), ['neon', 'yaml', 'xml'], true)) { - $changedContent = $this->neonYamlXmlProcessor->processFileInfo($originalFileInfo); + } elseif (in_array($originalFileInfo->getSuffix(), StaticNonPhpFileSuffixes::SUFFIXES, true)) { + $changedContent = $this->nonPhpFileProcessor->processFileInfo($originalFileInfo); } else { $message = sprintf('Suffix "%s" is not supported yet', $originalFileInfo->getSuffix()); throw new ShouldNotHappenException($message); diff --git a/src/ValueObject/StaticNonPhpFileSuffixes.php b/src/ValueObject/StaticNonPhpFileSuffixes.php new file mode 100644 index 00000000000..ac59fc27328 --- /dev/null +++ b/src/ValueObject/StaticNonPhpFileSuffixes.php @@ -0,0 +1,18 @@ +