add rename class support in twig/latte as well

This commit is contained in:
TomasVotruba 2020-06-23 13:25:19 +02:00
parent 1952b6177f
commit 94cc4e84e8
12 changed files with 72 additions and 22 deletions

View File

@ -0,0 +1,5 @@
services:
Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\OldClass: null
-----
services:
Rector\Renaming\Tests\Rector\Class_\RenameClassRector\Source\NewClass: null

View File

@ -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}

View File

@ -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') }}

View File

@ -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()
);
}
/**

View File

@ -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();

View File

@ -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;
}

View File

@ -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);

View File

@ -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);

View File

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Rector\Core\ValueObject;
final class StaticNonPhpFileSuffixes
{
/**
* @var string[]
*/
public const SUFFIXES = ['neon', 'yaml', 'xml', 'yml', 'twig', 'latte'];
public static function getSuffixRegexPattern(): string
{
return '#\.(' . implode('|', self::SUFFIXES) . ')$#i';
}
}