Add YamlProcessor

This commit is contained in:
TomasVotruba 2020-02-06 20:12:48 +01:00
parent 9cb8a12ba6
commit c9976ef43b
7 changed files with 152 additions and 1 deletions

View File

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

View File

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

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Rector\Configuration;
final class ChangeConfiguration
{
/**
* @var string[]
*/
private $oldToNewClasses = [];
/**
* @param string[] $oldToNewClasses
*/
public function setOldToNewClasses(array $oldToNewClasses): void
{
$this->oldToNewClasses = $oldToNewClasses;
}
/**
* @return string[]
*/
public function getOldToNewClasses(): array
{
return $this->oldToNewClasses;
}
}

View File

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

View File

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

View File

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

View File

@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace Rector\Yaml;
use Nette\Utils\FileSystem;
use Nette\Utils\Strings;
use Rector\Configuration\ChangeConfiguration;
use Rector\Configuration\Configuration;
use Rector\FileSystem\FilesFinder;
use Symfony\Component\Console\Style\SymfonyStyle;
final class YamlProcessor
{
/**
* @var Configuration
*/
private $configuration;
/**
* @var FilesFinder
*/
private $filesFinder;
/**
* @var ChangeConfiguration
*/
private $changeConfiguration;
/**
* @var SymfonyStyle
*/
private $symfonyStyle;
public function __construct(
Configuration $configuration,
FilesFinder $filesFinder,
ChangeConfiguration $changeConfiguration,
SymfonyStyle $symfonyStyle
) {
$this->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);
}
}
}