From c9976ef43bfdbe8b7946c9f17f9a09b7b712a57d Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Thu, 6 Feb 2020 20:12:48 +0100 Subject: [PATCH] Add YamlProcessor --- .../src/Rector/Class_/RenameClassRector.php | 4 + rename-core.yaml | 1 - src/Configuration/ChangeConfiguration.php | 29 +++++++ src/Configuration/Configuration.php | 21 +++++ src/Console/Command/ProcessCommand.php | 12 +++ .../AutowiredEventDispatcher.php | 1 + src/Yaml/YamlProcessor.php | 85 +++++++++++++++++++ 7 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 src/Configuration/ChangeConfiguration.php create mode 100644 src/Yaml/YamlProcessor.php diff --git a/packages/Renaming/src/Rector/Class_/RenameClassRector.php b/packages/Renaming/src/Rector/Class_/RenameClassRector.php index 3d691e501bf..22b3e949e09 100644 --- a/packages/Renaming/src/Rector/Class_/RenameClassRector.php +++ b/packages/Renaming/src/Rector/Class_/RenameClassRector.php @@ -19,6 +19,7 @@ use PhpParser\Node\Stmt\Use_; use PhpParser\Node\Stmt\UseUse; use PHPStan\Type\ObjectType; use Rector\CodingStyle\Naming\ClassNaming; +use Rector\Configuration\ChangeConfiguration; use Rector\NodeTypeResolver\ClassExistenceStaticHelper; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpDoc\PhpDocClassRenamer; @@ -60,11 +61,14 @@ final class RenameClassRector extends AbstractRector public function __construct( ClassNaming $classNaming, PhpDocClassRenamer $phpDocClassRenamer, + ChangeConfiguration $changeConfiguration, array $oldToNewClasses = [] ) { $this->classNaming = $classNaming; $this->oldToNewClasses = $oldToNewClasses; $this->phpDocClassRenamer = $phpDocClassRenamer; + + $changeConfiguration->setOldToNewClasses($oldToNewClasses); } public function getDefinition(): RectorDefinition diff --git a/rename-core.yaml b/rename-core.yaml index d4a08654220..24505e80cbf 100644 --- a/rename-core.yaml +++ b/rename-core.yaml @@ -221,4 +221,3 @@ services: Rector\Php\Regex\RegexPatternArgumentManipulator: Rector\Core\Php\Regex\RegexPatternArgumentManipulator Rector\Php\PhpVersionProvider: Rector\Core\Php\PhpVersionProvider Rector\Php\TypeAnalyzer: Rector\Core\Php\TypeAnalyzer - diff --git a/src/Configuration/ChangeConfiguration.php b/src/Configuration/ChangeConfiguration.php new file mode 100644 index 00000000000..d0c1e795e07 --- /dev/null +++ b/src/Configuration/ChangeConfiguration.php @@ -0,0 +1,29 @@ +oldToNewClasses = $oldToNewClasses; + } + + /** + * @return string[] + */ + public function getOldToNewClasses(): array + { + return $this->oldToNewClasses; + } +} diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index 3818c5685f6..da0b3f2a176 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -54,6 +54,11 @@ final class Configuration */ private $outputFile; + /** + * @var mixed[] + */ + private $source = []; + /** * Needs to run in the start of the life cycle, since the rest of workflow uses it. */ @@ -142,6 +147,22 @@ final class Configuration return $this->outputFile; } + /** + * @param string[] $source + */ + public function setSource(array $source): void + { + $this->source = $source; + } + + /** + * @return string[] + */ + public function getSource(): array + { + return $this->source; + } + private function canShowProgressBar(InputInterface $input): bool { $noProgressBar = (bool) $input->getOption(Option::OPTION_NO_PROGRESS_BAR); diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index a2a647f3d47..b4575f9fca1 100644 --- a/src/Console/Command/ProcessCommand.php +++ b/src/Console/Command/ProcessCommand.php @@ -17,6 +17,7 @@ use Rector\FileSystem\FilesFinder; use Rector\Guard\RectorGuard; use Rector\PhpParser\NodeTraverser\RectorNodeTraverser; use Rector\Stubs\StubLoader; +use Rector\Yaml\YamlProcessor; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -85,6 +86,11 @@ final class ProcessCommand extends AbstractCommand */ private $stubLoader; + /** + * @var YamlProcessor + */ + private $yamlProcessor; + /** * @param string[] $paths * @param string[] $fileExtensions @@ -100,6 +106,7 @@ final class ProcessCommand extends AbstractCommand ReportingExtensionRunner $reportingExtensionRunner, RectorNodeTraverser $rectorNodeTraverser, StubLoader $stubLoader, + YamlProcessor $yamlProcessor, array $paths, array $fileExtensions ) { @@ -117,6 +124,7 @@ final class ProcessCommand extends AbstractCommand $this->paths = $paths; parent::__construct(); + $this->yamlProcessor = $yamlProcessor; } protected function configure(): void @@ -197,6 +205,7 @@ final class ProcessCommand extends AbstractCommand $this->stubLoader->loadStubs(); $source = $this->resolvesSourcePaths($input); + $this->configuration->setSource($source); $phpFileInfos = $this->filesFinder->findInDirectoriesAndFiles( $source, @@ -206,6 +215,9 @@ final class ProcessCommand extends AbstractCommand $this->additionalAutoloader->autoloadWithInputAndSource($input, $source); + // yaml + $this->yamlProcessor->run(); + $this->rectorApplication->runOnFileInfos($phpFileInfos); // report diffs and errors diff --git a/src/EventDispatcher/AutowiredEventDispatcher.php b/src/EventDispatcher/AutowiredEventDispatcher.php index c27b0ee8a63..ac77e7046a5 100644 --- a/src/EventDispatcher/AutowiredEventDispatcher.php +++ b/src/EventDispatcher/AutowiredEventDispatcher.php @@ -18,6 +18,7 @@ final class AutowiredEventDispatcher extends EventDispatcher $this->addSubscriber($eventSubscriber); } + // Symfony 4.4/5 compat if (method_exists(parent::class, '__construct')) { parent::__construct(); } diff --git a/src/Yaml/YamlProcessor.php b/src/Yaml/YamlProcessor.php new file mode 100644 index 00000000000..e5736011e60 --- /dev/null +++ b/src/Yaml/YamlProcessor.php @@ -0,0 +1,85 @@ +configuration = $configuration; + $this->filesFinder = $filesFinder; + $this->changeConfiguration = $changeConfiguration; + $this->symfonyStyle = $symfonyStyle; + } + + public function run(): void + { + $source = $this->configuration->getSource(); + $yamlFileInfos = $this->filesFinder->findInDirectoriesAndFiles($source, ['yaml']); + + // 1. raw class rename + + $oldToNewClasses = $this->changeConfiguration->getOldToNewClasses(); + if ($oldToNewClasses === []) { + return; + } + + foreach ($yamlFileInfos as $yamlFileInfo) { + $oldContent = $yamlFileInfo->getContents(); + $newContent = $oldContent; + + foreach ($oldToNewClasses as $oldClass => $newClass) { + /** @var string $newContent */ + $newContent = Strings::replace($newContent, '#' . preg_quote($oldClass) . '#', $newClass); + } + + // nothing has changed + if ($oldContent === $newContent) { + continue; + } + + $relativeFilePath = $yamlFileInfo->getRelativeFilePathFromCwd(); + + if ($this->configuration->isDryRun()) { + $this->symfonyStyle->note(sprintf('File %s would be changed (dry-run is on now)', $relativeFilePath)); + continue; + } + + $this->symfonyStyle->note(sprintf('File %s was changed', $relativeFilePath)); + FileSystem::write($yamlFileInfo->getRealPath(), $newContent); + } + } +}