From 97f5ff545c3154341c0e7140f1981e9feb38abd6 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Thu, 19 Mar 2020 01:07:07 +0100 Subject: [PATCH 1/6] decouple ChangesReporting package --- abz/DeadCode.php | 13 +++ composer.json | 1 + packages/changes-reporting/config/config.yaml | 11 +++ .../Application/ErrorAndDiffCollector.php | 15 +-- .../src/Collector/RectorChangeCollector.php | 38 ++++++++ .../Output/OutputFormatterInterface.php | 4 +- .../src/Output/CheckstyleOutputFormatter.php | 92 +++++++++++++++++++ .../src}/Output/ConsoleOutputFormatter.php | 10 +- .../src}/Output/JsonOutputFormatter.php | 8 +- .../RectorChangeCollectorTrait.php | 16 ++-- .../RectorWithFileAndLineChange.php | 45 +++++++++ src/Application/AppliedRectorCollector.php | 32 ------- src/Application/RectorApplication.php | 1 + src/Configuration/Configuration.php | 11 ++- src/Console/Application.php | 19 +++- src/Console/Command/ProcessCommand.php | 4 +- .../Output/OutputFormatterCollector.php | 2 +- .../AbstractRector/AbstractRectorTrait.php | 3 +- .../AbstractRector/NodeCommandersTrait.php | 4 +- .../Property/InjectAnnotationClassRector.php | 2 +- src/Standalone/RectorStandaloneRunner.php | 4 +- src/ValueObject/Reporting/FileDiff.php | 32 +++++-- 22 files changed, 289 insertions(+), 78 deletions(-) create mode 100644 abz/DeadCode.php create mode 100644 packages/changes-reporting/config/config.yaml rename {src => packages/changes-reporting/src}/Application/ErrorAndDiffCollector.php (90%) create mode 100644 packages/changes-reporting/src/Collector/RectorChangeCollector.php rename {src/Contract/Console => packages/changes-reporting/src/Contract}/Output/OutputFormatterInterface.php (62%) create mode 100644 packages/changes-reporting/src/Output/CheckstyleOutputFormatter.php rename {src/Console => packages/changes-reporting/src}/Output/ConsoleOutputFormatter.php (95%) rename {src/Console => packages/changes-reporting/src}/Output/JsonOutputFormatter.php (91%) rename src/Rector/AbstractRector/AppliedRectorCollectorTrait.php => packages/changes-reporting/src/Rector/AbstractRector/RectorChangeCollectorTrait.php (57%) create mode 100644 packages/changes-reporting/src/ValueObject/RectorWithFileAndLineChange.php delete mode 100644 src/Application/AppliedRectorCollector.php diff --git a/abz/DeadCode.php b/abz/DeadCode.php new file mode 100644 index 000000000000..1cffd858de35 --- /dev/null +++ b/abz/DeadCode.php @@ -0,0 +1,13 @@ +differAndFormatter = $differAndFormatter; - $this->appliedRectorCollector = $appliedRectorCollector; + $this->rectorChangeCollector = $rectorChangeCollector; $this->exceptionCorrector = $exceptionCorrector; $this->removedAndAddedFilesCollector = $removedAndAddedFilesCollector; $this->nodeRemovingCommander = $nodeRemovingCommander; @@ -103,14 +104,14 @@ public function addFileDiff(SmartFileInfo $smartFileInfo, string $newContent, st return; } - $appliedRectors = $this->appliedRectorCollector->getRectorClasses($smartFileInfo); + $rectorChanges = $this->rectorChangeCollector->getRectorChangesByFileInfo($smartFileInfo); // always keep the most recent diff $this->fileDiffs[$smartFileInfo->getRealPath()] = new FileDiff( $smartFileInfo, $this->differAndFormatter->diff($oldContent, $newContent), $this->differAndFormatter->diffAndFormat($oldContent, $newContent), - $appliedRectors + $rectorChanges ); } diff --git a/packages/changes-reporting/src/Collector/RectorChangeCollector.php b/packages/changes-reporting/src/Collector/RectorChangeCollector.php new file mode 100644 index 000000000000..bec55cabbf50 --- /dev/null +++ b/packages/changes-reporting/src/Collector/RectorChangeCollector.php @@ -0,0 +1,38 @@ +rectorWithFileAndLineChanges[] = new RectorWithFileAndLineChange( + $rectorClass, + $smartFileInfo->getRealPath(), + $line + ); + } + + /** + * @return RectorWithFileAndLineChange[] + */ + public function getRectorChangesByFileInfo(SmartFileInfo $smartFileInfo): array + { + return array_filter( + $this->rectorWithFileAndLineChanges, + function (RectorWithFileAndLineChange $rectorWithFileAndLineChange) use ($smartFileInfo) { + return $rectorWithFileAndLineChange->getRealPath() === $smartFileInfo->getRealPath(); + } + ); + } +} diff --git a/src/Contract/Console/Output/OutputFormatterInterface.php b/packages/changes-reporting/src/Contract/Output/OutputFormatterInterface.php similarity index 62% rename from src/Contract/Console/Output/OutputFormatterInterface.php rename to packages/changes-reporting/src/Contract/Output/OutputFormatterInterface.php index 7d9229011187..2ec41a0d5499 100644 --- a/src/Contract/Console/Output/OutputFormatterInterface.php +++ b/packages/changes-reporting/src/Contract/Output/OutputFormatterInterface.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace Rector\Core\Contract\Console\Output; +namespace Rector\ChangesReporting\Contract\Output; -use Rector\Core\Application\ErrorAndDiffCollector; +use Rector\ChangesReporting\Application\ErrorAndDiffCollector; interface OutputFormatterInterface { diff --git a/packages/changes-reporting/src/Output/CheckstyleOutputFormatter.php b/packages/changes-reporting/src/Output/CheckstyleOutputFormatter.php new file mode 100644 index 000000000000..54d26a78c4c7 --- /dev/null +++ b/packages/changes-reporting/src/Output/CheckstyleOutputFormatter.php @@ -0,0 +1,92 @@ +symfonyStyle = $symfonyStyle; + } + + public function getName(): string + { + return self::NAME; + } + + public function report(ErrorAndDiffCollector $errorAndDiffCollector): void + { + if ($errorAndDiffCollector->getErrors() === [] && $errorAndDiffCollector->getFileDiffsCount() === 0) { + return; + } + + $this->symfonyStyle->writeln(''); + $this->symfonyStyle->writeln(''); + + foreach ($errorAndDiffCollector->getFileDiffs() as $fileDiff) { + $this->writeFileErrors($fileDiff); + } + + $this->writeNonFileErrors($errorAndDiffCollector); + + $this->symfonyStyle->writeln(''); + } + + private function escape(string $string): string + { + return htmlspecialchars($string, ENT_XML1 | ENT_COMPAT, 'UTF-8'); + } + + private function writeNonFileErrors(ErrorAndDiffCollector $errorAndDiffCollector): void + { + if ($errorAndDiffCollector->getErrors() !== []) { + $this->symfonyStyle->writeln(''); + + foreach ($errorAndDiffCollector->getErrors() as $error) { + $escapedMessage = $this->escape($error->getMessage()); + + $this->symfonyStyle->writeln( + sprintf(' ', $escapedMessage) + ); + } + + $this->symfonyStyle->writeln(''); + } + } + + private function writeFileErrors(FileDiff $fileDiff): void + { + $this->symfonyStyle->writeln(sprintf('', $this->escape($fileDiff->getRelativeFilePath()))); + + foreach ($fileDiff->getRectorChanges() as $rectorChange) { + $error = sprintf( + ' ', + $this->escape((string) $rectorChange->getLine()), + $this->escape((string) $rectorChange->getRectorClass()) + ); + $this->symfonyStyle->writeln($error); + } + + $this->symfonyStyle->writeln(''); + } +} diff --git a/src/Console/Output/ConsoleOutputFormatter.php b/packages/changes-reporting/src/Output/ConsoleOutputFormatter.php similarity index 95% rename from src/Console/Output/ConsoleOutputFormatter.php rename to packages/changes-reporting/src/Output/ConsoleOutputFormatter.php index 63620c2cb645..b7af98fa218c 100644 --- a/src/Console/Output/ConsoleOutputFormatter.php +++ b/packages/changes-reporting/src/Output/ConsoleOutputFormatter.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace Rector\Core\Console\Output; +namespace Rector\ChangesReporting\Output; use Nette\Utils\Strings; -use Rector\Core\Application\ErrorAndDiffCollector; +use Rector\ChangesReporting\Application\ErrorAndDiffCollector; +use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface; use Rector\Core\Configuration\Configuration; use Rector\Core\Configuration\Option; -use Rector\Core\Contract\Console\Output\OutputFormatterInterface; use Rector\Core\PhpParser\Printer\BetterStandardPrinter; use Rector\Core\ValueObject\Application\Error; use Rector\Core\ValueObject\Reporting\FileDiff; @@ -112,10 +112,10 @@ private function reportFileDiffs(array $fileDiffs): void $this->symfonyStyle->writeln($fileDiff->getDiffConsoleFormatted()); $this->symfonyStyle->newLine(); - if ($fileDiff->getAppliedRectorClasses() !== []) { + if ($fileDiff->getRectorChanges() !== []) { $this->symfonyStyle->writeln('Applied rules:'); $this->symfonyStyle->newLine(); - $this->symfonyStyle->listing($fileDiff->getAppliedRectorClasses()); + $this->symfonyStyle->listing($fileDiff->getRectorClasses()); $this->symfonyStyle->newLine(); } } diff --git a/src/Console/Output/JsonOutputFormatter.php b/packages/changes-reporting/src/Output/JsonOutputFormatter.php similarity index 91% rename from src/Console/Output/JsonOutputFormatter.php rename to packages/changes-reporting/src/Output/JsonOutputFormatter.php index 5fb635872bb7..79198cf92a2c 100644 --- a/src/Console/Output/JsonOutputFormatter.php +++ b/packages/changes-reporting/src/Output/JsonOutputFormatter.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace Rector\Core\Console\Output; +namespace Rector\ChangesReporting\Output; use Nette\Utils\FileSystem; use Nette\Utils\Json; -use Rector\Core\Application\ErrorAndDiffCollector; +use Rector\ChangesReporting\Application\ErrorAndDiffCollector; +use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface; use Rector\Core\Configuration\Configuration; -use Rector\Core\Contract\Console\Output\OutputFormatterInterface; use Symfony\Component\Console\Style\SymfonyStyle; final class JsonOutputFormatter implements OutputFormatterInterface @@ -61,7 +61,7 @@ public function report(ErrorAndDiffCollector $errorAndDiffCollector): void $errorsArray['file_diffs'][] = [ 'file' => $relativeFilePath, 'diff' => $fileDiff->getDiff(), - 'applied_rectors' => $fileDiff->getAppliedRectorClasses(), + 'applied_rectors' => $fileDiff->getRectorClasses(), ]; // for Rector CI diff --git a/src/Rector/AbstractRector/AppliedRectorCollectorTrait.php b/packages/changes-reporting/src/Rector/AbstractRector/RectorChangeCollectorTrait.php similarity index 57% rename from src/Rector/AbstractRector/AppliedRectorCollectorTrait.php rename to packages/changes-reporting/src/Rector/AbstractRector/RectorChangeCollectorTrait.php index e5ee70b1f164..26c6083ac562 100644 --- a/src/Rector/AbstractRector/AppliedRectorCollectorTrait.php +++ b/packages/changes-reporting/src/Rector/AbstractRector/RectorChangeCollectorTrait.php @@ -2,29 +2,29 @@ declare(strict_types=1); -namespace Rector\Core\Rector\AbstractRector; +namespace Rector\ChangesReporting\Rector\AbstractRector; use PhpParser\Node; -use Rector\Core\Application\AppliedRectorCollector; +use Rector\ChangesReporting\Collector\RectorChangeCollector; use Rector\NodeTypeResolver\Node\AttributeKey; /** * This could be part of @see AbstractRector, but decopuling to trait * makes clear what code has 1 purpose. */ -trait AppliedRectorCollectorTrait +trait RectorChangeCollectorTrait { /** - * @var AppliedRectorCollector + * @var RectorChangeCollector */ - private $appliedRectorCollector; + private $rectorChangeCollector; /** * @required */ - public function setAppliedRectorCollector(AppliedRectorCollector $appliedRectorCollector): void + public function autowireAppliedRectorCollectorTrait(RectorChangeCollector $rectorChangeCollector): void { - $this->appliedRectorCollector = $appliedRectorCollector; + $this->rectorChangeCollector = $rectorChangeCollector; } protected function notifyNodeChangeFileInfo(Node $node): void @@ -36,6 +36,6 @@ protected function notifyNodeChangeFileInfo(Node $node): void return; } - $this->appliedRectorCollector->addRectorClass(static::class, $fileInfo); + $this->rectorChangeCollector->addRectorClassWithLine(static::class, $fileInfo, $node->getLine()); } } diff --git a/packages/changes-reporting/src/ValueObject/RectorWithFileAndLineChange.php b/packages/changes-reporting/src/ValueObject/RectorWithFileAndLineChange.php new file mode 100644 index 000000000000..5c4cf1bda389 --- /dev/null +++ b/packages/changes-reporting/src/ValueObject/RectorWithFileAndLineChange.php @@ -0,0 +1,45 @@ +rectorClass = $rectorClass; + $this->line = $line; + $this->realPath = $realPath; + } + + public function getRectorClass(): string + { + return $this->rectorClass; + } + + public function getLine(): int + { + return $this->line; + } + + public function getRealPath(): string + { + return $this->realPath; + } +} diff --git a/src/Application/AppliedRectorCollector.php b/src/Application/AppliedRectorCollector.php deleted file mode 100644 index 63859834ce6d..000000000000 --- a/src/Application/AppliedRectorCollector.php +++ /dev/null @@ -1,32 +0,0 @@ -rectorClassesByFile[$smartFileInfo->getRealPath()][] = $rectorClass; - } - - /** - * @return string[] - */ - public function getRectorClasses(SmartFileInfo $smartFileInfo): array - { - if (! isset($this->rectorClassesByFile[$smartFileInfo->getRealPath()])) { - return []; - } - - return array_unique($this->rectorClassesByFile[$smartFileInfo->getRealPath()]); - } -} diff --git a/src/Application/RectorApplication.php b/src/Application/RectorApplication.php index 4f113a4adb6e..ab0adc86afc9 100644 --- a/src/Application/RectorApplication.php +++ b/src/Application/RectorApplication.php @@ -7,6 +7,7 @@ use OndraM\CiDetector\CiDetector; use PHPStan\AnalysedCodeException; use PHPStan\Analyser\NodeScopeResolver; +use Rector\ChangesReporting\Application\ErrorAndDiffCollector; use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector; use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor; use Rector\Core\Configuration\Configuration; diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index b8e2dce62943..4e9de73d89d1 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -6,7 +6,8 @@ use Jean85\PrettyVersions; use Nette\Utils\Strings; -use Rector\Core\Console\Output\JsonOutputFormatter; +use Rector\ChangesReporting\Output\CheckstyleOutputFormatter; +use Rector\ChangesReporting\Output\JsonOutputFormatter; use Rector\Core\Exception\Rector\RectorNotFoundOrNotValidRectorClassException; use Rector\Core\Rector\AbstractRector; use Rector\Core\Testing\PHPUnit\PHPUnitEnvironment; @@ -166,8 +167,14 @@ public function getSource(): array private function canShowProgressBar(InputInterface $input): bool { $noProgressBar = (bool) $input->getOption(Option::OPTION_NO_PROGRESS_BAR); + if ($noProgressBar) { + return false; + } - return ! $noProgressBar && $input->getOption(Option::OPTION_OUTPUT_FORMAT) !== JsonOutputFormatter::NAME; + if ($input->getOption(Option::OPTION_OUTPUT_FORMAT) === JsonOutputFormatter::NAME) { + return false; + } + return $input->getOption(Option::OPTION_OUTPUT_FORMAT) !== CheckstyleOutputFormatter::NAME; } private function setOnlyRector(?string $rector): void diff --git a/src/Console/Application.php b/src/Console/Application.php index cd6dff274f9f..73a33bf70066 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -6,8 +6,9 @@ use Composer\XdebugHandler\XdebugHandler; use Jean85\PrettyVersions; +use Rector\ChangesReporting\Output\CheckstyleOutputFormatter; +use Rector\ChangesReporting\Output\JsonOutputFormatter; use Rector\Core\Configuration\Configuration; -use Rector\Core\Console\Output\JsonOutputFormatter; use Rector\Core\Exception\Configuration\InvalidConfigurationException; use Rector\Utils\DocumentationGenerator\Command\DumpNodesCommand; use Rector\Utils\DocumentationGenerator\Command\DumpRectorsCommand; @@ -118,14 +119,28 @@ private function getNewWorkingDir(InputInterface $input): string private function shouldPrintMetaInformation(InputInterface $input): bool { $hasNoArguments = $input->getFirstArgument() === null; + if ($hasNoArguments) { + return false; + } + $hasVersionOption = $input->hasParameterOption('--version'); + if ($hasVersionOption) { + return false; + } $hasJsonOutput = ( $input->getParameterOption('--output-format') === JsonOutputFormatter::NAME || $input->getParameterOption('-o') === JsonOutputFormatter::NAME ); + if ($hasJsonOutput) { + return false; + } - return ! ($hasVersionOption || $hasNoArguments || $hasJsonOutput); + $hasCheckstyleOutput = ( + $input->getParameterOption('--output-format') === CheckstyleOutputFormatter::NAME || + $input->getParameterOption('-o') === CheckstyleOutputFormatter::NAME + ); + return !$hasCheckstyleOutput; } private function removeUnusedOptions(InputDefinition $inputDefinition): void diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index e4260c027655..45d734541dbd 100644 --- a/src/Console/Command/ProcessCommand.php +++ b/src/Console/Command/ProcessCommand.php @@ -4,12 +4,12 @@ namespace Rector\Core\Console\Command; -use Rector\Core\Application\ErrorAndDiffCollector; +use Rector\ChangesReporting\Application\ErrorAndDiffCollector; +use Rector\ChangesReporting\Output\ConsoleOutputFormatter; use Rector\Core\Application\RectorApplication; use Rector\Core\Autoloading\AdditionalAutoloader; use Rector\Core\Configuration\Configuration; use Rector\Core\Configuration\Option; -use Rector\Core\Console\Output\ConsoleOutputFormatter; use Rector\Core\Console\Output\OutputFormatterCollector; use Rector\Core\Console\Shell; use Rector\Core\Extension\ReportingExtensionRunner; diff --git a/src/Console/Output/OutputFormatterCollector.php b/src/Console/Output/OutputFormatterCollector.php index cc8a987b1647..2f43e5961cde 100644 --- a/src/Console/Output/OutputFormatterCollector.php +++ b/src/Console/Output/OutputFormatterCollector.php @@ -4,7 +4,7 @@ namespace Rector\Core\Console\Output; -use Rector\Core\Contract\Console\Output\OutputFormatterInterface; +use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface; use Rector\Core\Exception\Console\Output\MissingOutputFormatterException; final class OutputFormatterCollector diff --git a/src/Rector/AbstractRector/AbstractRectorTrait.php b/src/Rector/AbstractRector/AbstractRectorTrait.php index 3173f2ad685a..916f1e3d4d26 100644 --- a/src/Rector/AbstractRector/AbstractRectorTrait.php +++ b/src/Rector/AbstractRector/AbstractRectorTrait.php @@ -7,11 +7,12 @@ use Nette\Utils\Strings; use PhpParser\Node; use PhpParser\Node\Stmt\Class_; +use Rector\ChangesReporting\Rector\AbstractRector\RectorChangeCollectorTrait; use Rector\Doctrine\AbstractRector\DoctrineTrait; trait AbstractRectorTrait { - use AppliedRectorCollectorTrait; + use RectorChangeCollectorTrait; use DoctrineTrait; use NodeTypeResolverTrait; use NameResolverTrait; diff --git a/src/Rector/AbstractRector/NodeCommandersTrait.php b/src/Rector/AbstractRector/NodeCommandersTrait.php index 253cb74ec454..06db6c0a2cb5 100644 --- a/src/Rector/AbstractRector/NodeCommandersTrait.php +++ b/src/Rector/AbstractRector/NodeCommandersTrait.php @@ -11,9 +11,9 @@ use PhpParser\Node\Stmt\ClassLike; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; +use Rector\ChangesReporting\Collector\RectorChangeCollector; use Rector\CodingStyle\Application\NameImportingCommander; use Rector\CodingStyle\Application\UseAddingCommander; -use Rector\Core\Application\AppliedRectorCollector; use Rector\Core\PhpParser\Node\Commander\NodeAddingCommander; use Rector\Core\PhpParser\Node\Commander\NodeRemovingCommander; use Rector\Core\PhpParser\Node\Commander\NodeReplacingCommander; @@ -25,7 +25,7 @@ * This could be part of @see AbstractRector, but decopuling to trait * makes clear what code has 1 purpose. * - * @property-read AppliedRectorCollector $appliedRectorCollector + * @property-read RectorChangeCollector $rectorChangeCollector */ trait NodeCommandersTrait { diff --git a/src/Rector/Property/InjectAnnotationClassRector.php b/src/Rector/Property/InjectAnnotationClassRector.php index 63b1c4707ca8..5a2c0c9d69ec 100644 --- a/src/Rector/Property/InjectAnnotationClassRector.php +++ b/src/Rector/Property/InjectAnnotationClassRector.php @@ -17,7 +17,7 @@ use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\BetterPhpDocParser\PhpDocNode\JMS\JMSInjectTagValueNode; use Rector\BetterPhpDocParser\PhpDocNode\PHPDI\PHPDIInjectTagValueNode; -use Rector\Core\Application\ErrorAndDiffCollector; +use Rector\ChangesReporting\Application\ErrorAndDiffCollector; use Rector\Core\Exception\NotImplementedException; use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\Rector\AbstractRector; diff --git a/src/Standalone/RectorStandaloneRunner.php b/src/Standalone/RectorStandaloneRunner.php index 583d109613c6..3b09f7875757 100644 --- a/src/Standalone/RectorStandaloneRunner.php +++ b/src/Standalone/RectorStandaloneRunner.php @@ -5,13 +5,13 @@ namespace Rector\Core\Standalone; use Psr\Container\ContainerInterface; -use Rector\Core\Application\ErrorAndDiffCollector; +use Rector\ChangesReporting\Application\ErrorAndDiffCollector; +use Rector\ChangesReporting\Output\ConsoleOutputFormatter; use Rector\Core\Application\RectorApplication; use Rector\Core\Autoloading\AdditionalAutoloader; use Rector\Core\Configuration\Configuration; use Rector\Core\Configuration\Option; use Rector\Core\Console\Command\ProcessCommand; -use Rector\Core\Console\Output\ConsoleOutputFormatter; use Rector\Core\DependencyInjection\RectorContainerFactory; use Rector\Core\Exception\FileSystem\FileNotFoundException; use Rector\Core\Extension\FinishingExtensionRunner; diff --git a/src/ValueObject/Reporting/FileDiff.php b/src/ValueObject/Reporting/FileDiff.php index 73d80ab78e74..f74f054886e7 100644 --- a/src/ValueObject/Reporting/FileDiff.php +++ b/src/ValueObject/Reporting/FileDiff.php @@ -4,6 +4,7 @@ namespace Rector\Core\ValueObject\Reporting; +use Rector\ChangesReporting\ValueObject\RectorWithFileAndLineChange; use Symplify\SmartFileSystem\SmartFileInfo; final class FileDiff @@ -19,9 +20,9 @@ final class FileDiff private $diffConsoleFormatted; /** - * @var string[] + * @var RectorWithFileAndLineChange[] */ - private $appliedRectorClasses = []; + private $rectorWithFileAndLineChanges = []; /** * @var SmartFileInfo @@ -29,17 +30,17 @@ final class FileDiff private $smartFileInfo; /** - * @param string[] $appliedRectorClasses + * @param RectorWithFileAndLineChange[] $rectorWithFileAndLineChanges */ public function __construct( SmartFileInfo $smartFileInfo, string $diff, string $diffConsoleFormatted, - array $appliedRectorClasses = [] + array $rectorWithFileAndLineChanges = [] ) { $this->smartFileInfo = $smartFileInfo; $this->diff = $diff; - $this->appliedRectorClasses = $appliedRectorClasses; + $this->rectorWithFileAndLineChanges = $rectorWithFileAndLineChanges; $this->diffConsoleFormatted = $diffConsoleFormatted; } @@ -58,11 +59,28 @@ public function getRelativeFilePath(): string return $this->smartFileInfo->getRelativeFilePath(); } + /** + * @return RectorWithFileAndLineChange[] + */ + public function getRectorChanges(): array + { + return $this->rectorWithFileAndLineChanges; + } + /** * @return string[] */ - public function getAppliedRectorClasses(): array + public function getRectorClasses(): array { - return $this->appliedRectorClasses; + $rectorClasses = []; + foreach ($this->rectorWithFileAndLineChanges as $rectorWithFileAndLineChange) { + $rectorClasses[] = $rectorWithFileAndLineChange->getRectorClass(); + } + + $rectorClasses = array_unique($rectorClasses); + + sort($rectorClasses); + + return $rectorClasses; } } From eca4d54f582635ba045e402120eda7b7308f0896 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Thu, 19 Mar 2020 01:23:31 +0100 Subject: [PATCH 2/6] add PR annotate workflow --- .github/workflows/annotated_checkstyle.yaml | 22 +++++++++++++++++++ .../ImportFullyQualifiedNamesRector.php | 7 ++++-- .../RemoveCodeAfterReturnRector.php | 11 ++-------- .../TryCatchToExpectExceptionRector.php | 2 +- src/Console/Application.php | 2 +- .../AbstractRector/AbstractRectorTrait.php | 19 ++++++++++++++++ 6 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/annotated_checkstyle.yaml diff --git a/.github/workflows/annotated_checkstyle.yaml b/.github/workflows/annotated_checkstyle.yaml new file mode 100644 index 000000000000..c7113a5f0265 --- /dev/null +++ b/.github/workflows/annotated_checkstyle.yaml @@ -0,0 +1,22 @@ +# see https://github.com/staabm/annotate-pull-request-from-checkstyle +name: Anotate Checkstyle + +on: + pull_request: null + push: + branches: + - master + +jobs: + check_fixtures: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: shivammathur/setup-php@v1 + with: + php-version: 7.2 + coverage: none # disable xdebug, pcov + tools: cs2pr + - run: composer install --no-progress + - run: | + bin/rector p abz/DeadCode.php --set dead-code -n --autoload-file abz/DeadCode.php --output-format=checkstyle | cs2pr diff --git a/rules/coding-style/src/Rector/Namespace_/ImportFullyQualifiedNamesRector.php b/rules/coding-style/src/Rector/Namespace_/ImportFullyQualifiedNamesRector.php index 5e199c3ab8b4..da28d08a7a5a 100644 --- a/rules/coding-style/src/Rector/Namespace_/ImportFullyQualifiedNamesRector.php +++ b/rules/coding-style/src/Rector/Namespace_/ImportFullyQualifiedNamesRector.php @@ -124,10 +124,13 @@ public function refactor(Node $node): ?Node /** @var PhpDocInfo|null $phpDocInfo */ $phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO); if ($phpDocInfo === null) { - return $node; + return null; } - $this->docBlockNameImporter->importNames($phpDocInfo, $node); + $hasChanged = $this->docBlockNameImporter->importNames($phpDocInfo, $node); + if (! $hasChanged) { + return null; + } return $node; } diff --git a/rules/dead-code/src/Rector/FunctionLike/RemoveCodeAfterReturnRector.php b/rules/dead-code/src/Rector/FunctionLike/RemoveCodeAfterReturnRector.php index ce67f3f63df9..24408bfc14d3 100644 --- a/rules/dead-code/src/Rector/FunctionLike/RemoveCodeAfterReturnRector.php +++ b/rules/dead-code/src/Rector/FunctionLike/RemoveCodeAfterReturnRector.php @@ -66,8 +66,6 @@ public function refactor(Node $node): ?Node } $isDeadAfterReturn = false; - $isDeadAfterReturnRemoved = false; - foreach ($node->stmts as $key => $stmt) { if ($isDeadAfterReturn) { // keep comment @@ -75,8 +73,7 @@ public function refactor(Node $node): ?Node continue; } - unset($node->stmts[$key]); - $isDeadAfterReturnRemoved = true; + $this->removeStmt($node, $key); } if ($stmt instanceof Return_) { @@ -85,10 +82,6 @@ public function refactor(Node $node): ?Node } } - if (! $isDeadAfterReturnRemoved) { - return null; - } - - return $node; + return null; } } diff --git a/rules/phpunit/src/Rector/TryCatchToExpectExceptionRector.php b/rules/phpunit/src/Rector/TryCatchToExpectExceptionRector.php index 7cb6fd87b09f..7ed0836f7858 100644 --- a/rules/phpunit/src/Rector/TryCatchToExpectExceptionRector.php +++ b/rules/phpunit/src/Rector/TryCatchToExpectExceptionRector.php @@ -80,7 +80,7 @@ public function refactor(Node $node): ?Node continue; } - unset($node->stmts[$key]); + $this->removeStmt($node, $key); } $node->stmts = array_merge((array) $node->stmts, (array) $proccesed); diff --git a/src/Console/Application.php b/src/Console/Application.php index 73a33bf70066..becbf8f35156 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -140,7 +140,7 @@ private function shouldPrintMetaInformation(InputInterface $input): bool $input->getParameterOption('--output-format') === CheckstyleOutputFormatter::NAME || $input->getParameterOption('-o') === CheckstyleOutputFormatter::NAME ); - return !$hasCheckstyleOutput; + return ! $hasCheckstyleOutput; } private function removeUnusedOptions(InputDefinition $inputDefinition): void diff --git a/src/Rector/AbstractRector/AbstractRectorTrait.php b/src/Rector/AbstractRector/AbstractRectorTrait.php index 916f1e3d4d26..921fd20e0305 100644 --- a/src/Rector/AbstractRector/AbstractRectorTrait.php +++ b/src/Rector/AbstractRector/AbstractRectorTrait.php @@ -6,8 +6,12 @@ use Nette\Utils\Strings; use PhpParser\Node; +use PhpParser\Node\Expr\Closure; use PhpParser\Node\Stmt\Class_; +use PhpParser\Node\Stmt\ClassMethod; +use PhpParser\Node\Stmt\Function_; use Rector\ChangesReporting\Rector\AbstractRector\RectorChangeCollectorTrait; +use Rector\Core\Exception\ShouldNotHappenException; use Rector\Doctrine\AbstractRector\DoctrineTrait; trait AbstractRectorTrait @@ -43,4 +47,19 @@ protected function isNonAnonymousClass(?Node $node): bool return ! Strings::contains($name, 'AnonymousClass'); } + + /** + * @param Closure|ClassMethod|Function_ $node + */ + protected function removeStmt(Node $node, $key): void + { + if ($node->stmts === null) { + throw new ShouldNotHappenException(); + } + + // notify about remove node + $this->notifyNodeChangeFileInfo($node->stmts[$key]); + + unset($node->stmts[$key]); + } } From 11114def28a69bb6e296251c9a0c5531dab6d1dc Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Thu, 19 Mar 2020 12:23:00 +0100 Subject: [PATCH 3/6] add remove* method --- ...ructorInjectionToActionInjectionRector.php | 2 +- .../RemoveDefaultArgumentValueRector.php | 2 +- ...etcookieRector.php => SetCookieRector.php} | 5 ++- ...RectorTest.php => SetCookieRectorTest.php} | 6 +-- .../TryCatchToExpectExceptionRector.php | 1 + .../AbstractRector/AbstractRectorTrait.php | 44 ++++++++++++++++++- ...nInjectionToConstructorInjectionRector.php | 3 +- src/Rector/Argument/ArgumentRemoverRector.php | 4 +- .../Interface_/MergeInterfacesRector.php | 2 +- 9 files changed, 56 insertions(+), 13 deletions(-) rename rules/php-73/src/Rector/FuncCall/{SetcookieRector.php => SetCookieRector.php} (95%) rename rules/php-73/tests/Rector/FuncCall/SetcookieRector/{SetcookieRectorTest.php => SetCookieRectorTest.php} (76%) diff --git a/rules/architecture/src/Rector/Class_/ConstructorInjectionToActionInjectionRector.php b/rules/architecture/src/Rector/Class_/ConstructorInjectionToActionInjectionRector.php index fa5b418366b4..d781511c1866 100644 --- a/rules/architecture/src/Rector/Class_/ConstructorInjectionToActionInjectionRector.php +++ b/rules/architecture/src/Rector/Class_/ConstructorInjectionToActionInjectionRector.php @@ -298,7 +298,7 @@ private function removeAssignsFromConstructor(ClassMethod $classMethod): void } // remove the assign - unset($classMethod->stmts[$key]); + $this->removeStmt($classMethod, $key); } } diff --git a/rules/dead-code/src/Rector/MethodCall/RemoveDefaultArgumentValueRector.php b/rules/dead-code/src/Rector/MethodCall/RemoveDefaultArgumentValueRector.php index 34c18fa05f35..f4a024521b8e 100644 --- a/rules/dead-code/src/Rector/MethodCall/RemoveDefaultArgumentValueRector.php +++ b/rules/dead-code/src/Rector/MethodCall/RemoveDefaultArgumentValueRector.php @@ -93,7 +93,7 @@ public function refactor(Node $node): ?Node $keysToRemove = $this->resolveKeysToRemove($node, $defaultValues); foreach ($keysToRemove as $keyToRemove) { - unset($node->args[$keyToRemove]); + $this->removeArg($node, $keyToRemove); } return $node; diff --git a/rules/php-73/src/Rector/FuncCall/SetcookieRector.php b/rules/php-73/src/Rector/FuncCall/SetCookieRector.php similarity index 95% rename from rules/php-73/src/Rector/FuncCall/SetcookieRector.php rename to rules/php-73/src/Rector/FuncCall/SetCookieRector.php index 224b79eb8062..54311602e5de 100644 --- a/rules/php-73/src/Rector/FuncCall/SetcookieRector.php +++ b/rules/php-73/src/Rector/FuncCall/SetCookieRector.php @@ -4,7 +4,6 @@ namespace Rector\Php73\Rector\FuncCall; -use function count; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\Array_; @@ -22,8 +21,10 @@ * * @see https://www.php.net/setcookie * @see https://wiki.php.net/rfc/same-site-cookie + * + * @see \Rector\Php73\Tests\Rector\FuncCall\SetcookieRector\SetcookieRectorTest */ -final class SetcookieRector extends AbstractRector +final class SetCookieRector extends AbstractRector { /** * Conversion table from argument index to options name diff --git a/rules/php-73/tests/Rector/FuncCall/SetcookieRector/SetcookieRectorTest.php b/rules/php-73/tests/Rector/FuncCall/SetcookieRector/SetCookieRectorTest.php similarity index 76% rename from rules/php-73/tests/Rector/FuncCall/SetcookieRector/SetcookieRectorTest.php rename to rules/php-73/tests/Rector/FuncCall/SetcookieRector/SetCookieRectorTest.php index f26656ade426..1978f5db2203 100644 --- a/rules/php-73/tests/Rector/FuncCall/SetcookieRector/SetcookieRectorTest.php +++ b/rules/php-73/tests/Rector/FuncCall/SetcookieRector/SetCookieRectorTest.php @@ -6,9 +6,9 @@ use Iterator; use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase; -use Rector\Php73\Rector\FuncCall\SetcookieRector; +use Rector\Php73\Rector\FuncCall\SetCookieRector; -final class SetcookieRectorTest extends AbstractRectorTestCase +final class SetCookieRectorTest extends AbstractRectorTestCase { /** * @dataProvider provideData() @@ -25,6 +25,6 @@ public function provideData(): Iterator protected function getRectorClass(): string { - return SetcookieRector::class; + return SetCookieRector::class; } } diff --git a/rules/phpunit/src/Rector/TryCatchToExpectExceptionRector.php b/rules/phpunit/src/Rector/TryCatchToExpectExceptionRector.php index 7ed0836f7858..abebf97763bf 100644 --- a/rules/phpunit/src/Rector/TryCatchToExpectExceptionRector.php +++ b/rules/phpunit/src/Rector/TryCatchToExpectExceptionRector.php @@ -80,6 +80,7 @@ public function refactor(Node $node): ?Node continue; } + /** @var int $key */ $this->removeStmt($node, $key); } diff --git a/src/Rector/AbstractRector/AbstractRectorTrait.php b/src/Rector/AbstractRector/AbstractRectorTrait.php index 921fd20e0305..8ffeedd4e776 100644 --- a/src/Rector/AbstractRector/AbstractRectorTrait.php +++ b/src/Rector/AbstractRector/AbstractRectorTrait.php @@ -7,6 +7,9 @@ use Nette\Utils\Strings; use PhpParser\Node; use PhpParser\Node\Expr\Closure; +use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; @@ -51,7 +54,7 @@ protected function isNonAnonymousClass(?Node $node): bool /** * @param Closure|ClassMethod|Function_ $node */ - protected function removeStmt(Node $node, $key): void + protected function removeStmt(Node $node, int $key): void { if ($node->stmts === null) { throw new ShouldNotHappenException(); @@ -62,4 +65,43 @@ protected function removeStmt(Node $node, $key): void unset($node->stmts[$key]); } + + protected function removeParam(ClassMethod $classMethod, int $key): void + { + if ($classMethod->params === null) { + throw new ShouldNotHappenException(); + } + + // notify about remove node + $this->notifyNodeChangeFileInfo($classMethod->params[$key]); + + unset($classMethod->params[$key]); + } + + /** + * @param FuncCall|MethodCall|StaticCall $node + */ + protected function removeArg(Node $node, int $key): void + { + if ($node->args === null) { + throw new ShouldNotHappenException(); + } + + // notify about remove node + $this->notifyNodeChangeFileInfo($node->args[$key]); + + unset($node->args[$key]); + } + + protected function removeImplements(Class_ $class, int $key): void + { + if ($class->implements === null) { + throw new ShouldNotHappenException(); + } + + // notify about remove node + $this->notifyNodeChangeFileInfo($class->implements[$key]); + + unset($class->implements[$key]); + } } diff --git a/src/Rector/Architecture/DependencyInjection/ActionInjectionToConstructorInjectionRector.php b/src/Rector/Architecture/DependencyInjection/ActionInjectionToConstructorInjectionRector.php index 960fe5eaa556..11c8876ffaa0 100644 --- a/src/Rector/Architecture/DependencyInjection/ActionInjectionToConstructorInjectionRector.php +++ b/src/Rector/Architecture/DependencyInjection/ActionInjectionToConstructorInjectionRector.php @@ -115,8 +115,7 @@ private function processClassMethod(Class_ $classNode, ClassMethod $classMethod) $paramName = $this->getName($paramNode->var); $this->addPropertyToClass($classNode, $paramNodeType, $paramName); - // remove arguments - unset($classMethod->params[$key]); + $this->removeParam($classMethod, $key); $this->variablesToPropertyFetchCollection->addVariableNameAndType($paramName, $paramNodeType); } diff --git a/src/Rector/Argument/ArgumentRemoverRector.php b/src/Rector/Argument/ArgumentRemoverRector.php index 06c237c831cd..9dc4eeffee33 100644 --- a/src/Rector/Argument/ArgumentRemoverRector.php +++ b/src/Rector/Argument/ArgumentRemoverRector.php @@ -131,7 +131,7 @@ private function removeByName(Node $node, int $position, string $name): void { if ($node instanceof MethodCall || $node instanceof StaticCall) { if (isset($node->args[$position]) && $this->isName($node->args[$position], $name)) { - unset($node->args[$position]); + $this->removeArg($node, $position); } return; @@ -139,7 +139,7 @@ private function removeByName(Node $node, int $position, string $name): void if ($node instanceof ClassMethod) { if (isset($node->params[$position]) && $this->isName($node->params[$position], $name)) { - unset($node->params[$position]); + $this->removeParam($node, $position); } return; diff --git a/src/Rector/Interface_/MergeInterfacesRector.php b/src/Rector/Interface_/MergeInterfacesRector.php index a8d8e231bb43..f5f585b71880 100644 --- a/src/Rector/Interface_/MergeInterfacesRector.php +++ b/src/Rector/Interface_/MergeInterfacesRector.php @@ -92,7 +92,7 @@ private function makeImplementsUnique(Class_ $classNode): void foreach ($classNode->implements as $key => $name) { $fqnName = $this->getName($name); if (in_array($fqnName, $alreadyAddedNames, true)) { - unset($classNode->implements[$key]); + $this->removeImplements($classNode, $key); continue; } From 3b571816036a79c9db50d85ed66c5245407e2587 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Thu, 19 Mar 2020 12:52:38 +0100 Subject: [PATCH 4/6] add NotifyingNodeRemover --- .../src/Collector/RectorChangeCollector.php | 14 ++++ .../NodeManipulator/NotifyingNodeRemover.php | 83 +++++++++++++++++++ .../NotifyingRemovingNodeTrait.php | 57 +++++++++++++ .../RectorChangeCollectorTrait.php | 41 --------- .../ParamTypeDeclarationRector.php | 2 +- .../ReturnTypeDeclarationRector.php | 2 +- .../Node/Manipulator/ArrayManipulator.php | 13 +++ src/Rector/AbstractRector.php | 2 +- .../AbstractRector/AbstractRectorTrait.php | 65 +-------------- .../AbstractRector/NodeCommandersTrait.php | 32 ++++--- 10 files changed, 192 insertions(+), 119 deletions(-) create mode 100644 packages/changes-reporting/src/NodeManipulator/NotifyingNodeRemover.php create mode 100644 packages/changes-reporting/src/Rector/AbstractRector/NotifyingRemovingNodeTrait.php delete mode 100644 packages/changes-reporting/src/Rector/AbstractRector/RectorChangeCollectorTrait.php diff --git a/packages/changes-reporting/src/Collector/RectorChangeCollector.php b/packages/changes-reporting/src/Collector/RectorChangeCollector.php index bec55cabbf50..054a5f0929a9 100644 --- a/packages/changes-reporting/src/Collector/RectorChangeCollector.php +++ b/packages/changes-reporting/src/Collector/RectorChangeCollector.php @@ -4,7 +4,9 @@ namespace Rector\ChangesReporting\Collector; +use PhpParser\Node; use Rector\ChangesReporting\ValueObject\RectorWithFileAndLineChange; +use Rector\NodeTypeResolver\Node\AttributeKey; use Symplify\SmartFileSystem\SmartFileInfo; final class RectorChangeCollector @@ -35,4 +37,16 @@ function (RectorWithFileAndLineChange $rectorWithFileAndLineChange) use ($smartF } ); } + + public function notifyNodeFileInfo(Node $node): void + { + $fileInfo = $node->getAttribute(AttributeKey::FILE_INFO); + if ($fileInfo === null) { + // this file was changed before and this is a sub-new node + // array Traverse to all new nodes would have to be used, but it's not worth the performance + return; + } + + $this->addRectorClassWithLine(static::class, $fileInfo, $node->getLine()); + } } diff --git a/packages/changes-reporting/src/NodeManipulator/NotifyingNodeRemover.php b/packages/changes-reporting/src/NodeManipulator/NotifyingNodeRemover.php new file mode 100644 index 000000000000..0b1bfb0a6361 --- /dev/null +++ b/packages/changes-reporting/src/NodeManipulator/NotifyingNodeRemover.php @@ -0,0 +1,83 @@ +rectorChangeCollector = $rectorChangeCollector; + } + + /** + * @param Closure|ClassMethod|Function_ $node + */ + public function removeStmt(Node $node, int $key): void + { + if ($node->stmts === null) { + throw new ShouldNotHappenException(); + } + + // notify about remove node + $this->rectorChangeCollector->notifyNodeFileInfo($node->stmts[$key]); + + unset($node->stmts[$key]); + } + + public function removeParam(ClassMethod $classMethod, int $key): void + { + if ($classMethod->params === null) { + throw new ShouldNotHappenException(); + } + + // notify about remove node + $this->rectorChangeCollector->notifyNodeFileInfo($classMethod->params[$key]); + + unset($classMethod->params[$key]); + } + + /** + * @param FuncCall|MethodCall|StaticCall $node + */ + public function removeArg(Node $node, int $key): void + { + if ($node->args === null) { + throw new ShouldNotHappenException(); + } + + // notify about remove node + $this->rectorChangeCollector->notifyNodeFileInfo($node->args[$key]); + + unset($node->args[$key]); + } + + public function removeImplements(Class_ $class, int $key): void + { + if ($class->implements === null) { + throw new ShouldNotHappenException(); + } + + // notify about remove node + $this->rectorChangeCollector->notifyNodeFileInfo($class->implements[$key]); + + unset($class->implements[$key]); + } +} diff --git a/packages/changes-reporting/src/Rector/AbstractRector/NotifyingRemovingNodeTrait.php b/packages/changes-reporting/src/Rector/AbstractRector/NotifyingRemovingNodeTrait.php new file mode 100644 index 000000000000..3c9258570d76 --- /dev/null +++ b/packages/changes-reporting/src/Rector/AbstractRector/NotifyingRemovingNodeTrait.php @@ -0,0 +1,57 @@ +notifyingNodeRemover = $notifyingNodeRemover; + } + + /** + * @param Closure|ClassMethod|Function_ $node + */ + protected function removeStmt(Node $node, int $key): void + { + $this->notifyingNodeRemover->removeStmt($node, $key); + } + + protected function removeParam(ClassMethod $classMethod, int $key): void + { + $this->notifyingNodeRemover->removeParam($classMethod, $key); + } + + /** + * @param FuncCall|MethodCall|StaticCall $node + */ + protected function removeArg(Node $node, int $key): void + { + $this->notifyingNodeRemover->removeArg($node, $key); + } + + protected function removeImplements(Class_ $class, int $key): void + { + $this->notifyingNodeRemover->removeImplements($class, $key); + } +} diff --git a/packages/changes-reporting/src/Rector/AbstractRector/RectorChangeCollectorTrait.php b/packages/changes-reporting/src/Rector/AbstractRector/RectorChangeCollectorTrait.php deleted file mode 100644 index 26c6083ac562..000000000000 --- a/packages/changes-reporting/src/Rector/AbstractRector/RectorChangeCollectorTrait.php +++ /dev/null @@ -1,41 +0,0 @@ -rectorChangeCollector = $rectorChangeCollector; - } - - protected function notifyNodeChangeFileInfo(Node $node): void - { - $fileInfo = $node->getAttribute(AttributeKey::FILE_INFO); - if ($fileInfo === null) { - // this file was changed before and this is a sub-new node - // array Traverse to all new nodes would have to be used, but it's not worth the performance - return; - } - - $this->rectorChangeCollector->addRectorClassWithLine(static::class, $fileInfo, $node->getLine()); - } -} diff --git a/rules/type-declaration/src/Rector/FunctionLike/ParamTypeDeclarationRector.php b/rules/type-declaration/src/Rector/FunctionLike/ParamTypeDeclarationRector.php index c06deac2f3b6..ecfa40cbbb12 100644 --- a/rules/type-declaration/src/Rector/FunctionLike/ParamTypeDeclarationRector.php +++ b/rules/type-declaration/src/Rector/FunctionLike/ParamTypeDeclarationRector.php @@ -193,7 +193,7 @@ private function addParamTypeToMethod( $paramNode->type = $resolvedChildType; $paramNode->type->setAttribute(self::HAS_NEW_INHERITED_TYPE, true); - $this->notifyNodeChangeFileInfo($paramNode); + $this->notifyNodeFileInfo($paramNode); } /** diff --git a/rules/type-declaration/src/Rector/FunctionLike/ReturnTypeDeclarationRector.php b/rules/type-declaration/src/Rector/FunctionLike/ReturnTypeDeclarationRector.php index f10950592748..c5e921839633 100644 --- a/rules/type-declaration/src/Rector/FunctionLike/ReturnTypeDeclarationRector.php +++ b/rules/type-declaration/src/Rector/FunctionLike/ReturnTypeDeclarationRector.php @@ -260,7 +260,7 @@ private function addReturnTypeToChildMethod( // make sure the type is not overridden $currentClassMethod->returnType->setAttribute(self::DO_NOT_CHANGE, true); - $this->notifyNodeChangeFileInfo($currentClassMethod); + $this->notifyNodeFileInfo($currentClassMethod); } /** diff --git a/src/PhpParser/Node/Manipulator/ArrayManipulator.php b/src/PhpParser/Node/Manipulator/ArrayManipulator.php index d5ef77a2bedd..d6ad18a121f4 100644 --- a/src/PhpParser/Node/Manipulator/ArrayManipulator.php +++ b/src/PhpParser/Node/Manipulator/ArrayManipulator.php @@ -8,9 +8,20 @@ use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Scalar; use PhpParser\Node\Scalar\String_; +use Rector\ChangesReporting\Collector\RectorChangeCollector; final class ArrayManipulator { + /** + * @var RectorChangeCollector + */ + private $rectorChangeCollector; + + public function __construct(RectorChangeCollector $rectorChangeCollector) + { + $this->rectorChangeCollector = $rectorChangeCollector; + } + public function isArrayOnlyScalarValues(Array_ $array): bool { foreach ($array->items as $arrayItem) { @@ -56,7 +67,9 @@ public function findItemInInArrayByKeyAndUnset(Array_ $arrayNode, string $keyNam } // remove + recount for the printer + $removedArrayItem = $arrayNode->items[$i]; unset($arrayNode->items[$i]); + $this->rectorChangeCollector->notifyNodeFileInfo($removedArrayItem); return $item; } diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index e70b9ab7761c..6ab3d777d35e 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -174,7 +174,7 @@ final public function enterNode(Node $node) $this->mirrorAttributes($originalNodeWithAttributes, $node); $this->updateAttributes($node); $this->keepFileInfoAttribute($node, $originalNode); - $this->notifyNodeChangeFileInfo($node); + $this->notifyNodeFileInfo($node); } // if stmt ("$value;") was replaced by expr ("$value"), add the ending ";" (Expression) to prevent breaking the code diff --git a/src/Rector/AbstractRector/AbstractRectorTrait.php b/src/Rector/AbstractRector/AbstractRectorTrait.php index 8ffeedd4e776..de992150ca20 100644 --- a/src/Rector/AbstractRector/AbstractRectorTrait.php +++ b/src/Rector/AbstractRector/AbstractRectorTrait.php @@ -6,20 +6,12 @@ use Nette\Utils\Strings; use PhpParser\Node; -use PhpParser\Node\Expr\Closure; -use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Expr\MethodCall; -use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Stmt\Class_; -use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\Node\Stmt\Function_; -use Rector\ChangesReporting\Rector\AbstractRector\RectorChangeCollectorTrait; -use Rector\Core\Exception\ShouldNotHappenException; +use Rector\ChangesReporting\Rector\AbstractRector\NotifyingRemovingNodeTrait; use Rector\Doctrine\AbstractRector\DoctrineTrait; trait AbstractRectorTrait { - use RectorChangeCollectorTrait; use DoctrineTrait; use NodeTypeResolverTrait; use NameResolverTrait; @@ -32,6 +24,7 @@ trait AbstractRectorTrait use CallableNodeTraverserTrait; use ComplexRemovalTrait; use NodeCollectorTrait; + use NotifyingRemovingNodeTrait; protected function isNonAnonymousClass(?Node $node): bool { @@ -50,58 +43,4 @@ protected function isNonAnonymousClass(?Node $node): bool return ! Strings::contains($name, 'AnonymousClass'); } - - /** - * @param Closure|ClassMethod|Function_ $node - */ - protected function removeStmt(Node $node, int $key): void - { - if ($node->stmts === null) { - throw new ShouldNotHappenException(); - } - - // notify about remove node - $this->notifyNodeChangeFileInfo($node->stmts[$key]); - - unset($node->stmts[$key]); - } - - protected function removeParam(ClassMethod $classMethod, int $key): void - { - if ($classMethod->params === null) { - throw new ShouldNotHappenException(); - } - - // notify about remove node - $this->notifyNodeChangeFileInfo($classMethod->params[$key]); - - unset($classMethod->params[$key]); - } - - /** - * @param FuncCall|MethodCall|StaticCall $node - */ - protected function removeArg(Node $node, int $key): void - { - if ($node->args === null) { - throw new ShouldNotHappenException(); - } - - // notify about remove node - $this->notifyNodeChangeFileInfo($node->args[$key]); - - unset($node->args[$key]); - } - - protected function removeImplements(Class_ $class, int $key): void - { - if ($class->implements === null) { - throw new ShouldNotHappenException(); - } - - // notify about remove node - $this->notifyNodeChangeFileInfo($class->implements[$key]); - - unset($class->implements[$key]); - } } diff --git a/src/Rector/AbstractRector/NodeCommandersTrait.php b/src/Rector/AbstractRector/NodeCommandersTrait.php index 06db6c0a2cb5..9a341be02feb 100644 --- a/src/Rector/AbstractRector/NodeCommandersTrait.php +++ b/src/Rector/AbstractRector/NodeCommandersTrait.php @@ -24,8 +24,6 @@ /** * This could be part of @see AbstractRector, but decopuling to trait * makes clear what code has 1 purpose. - * - * @property-read RectorChangeCollector $rectorChangeCollector */ trait NodeCommandersTrait { @@ -59,6 +57,11 @@ trait NodeCommandersTrait */ private $nodeReplacingCommander; + /** + * @var RectorChangeCollector + */ + private $rectorChangeCollector; + /** * @required */ @@ -68,7 +71,8 @@ public function autowireNodeCommandersTrait( PropertyAddingCommander $propertyAddingCommander, UseAddingCommander $useAddingCommander, NameImportingCommander $nameImportingCommander, - NodeReplacingCommander $nodeReplacingCommander + NodeReplacingCommander $nodeReplacingCommander, + RectorChangeCollector $rectorChangeCollector ): void { $this->nodeRemovingCommander = $nodeRemovingCommander; $this->nodeAddingCommander = $nodeAddingCommander; @@ -76,6 +80,7 @@ public function autowireNodeCommandersTrait( $this->useAddingCommander = $useAddingCommander; $this->nameImportingCommander = $nameImportingCommander; $this->nodeReplacingCommander = $nodeReplacingCommander; + $this->rectorChangeCollector = $rectorChangeCollector; } /** @@ -92,28 +97,26 @@ protected function addNodeAfterNode(Node $newNode, Node $positionNode): void { $this->nodeAddingCommander->addNodeAfterNode($newNode, $positionNode); - $this->notifyNodeChangeFileInfo($positionNode); + $this->rectorChangeCollector->notifyNodeFileInfo($positionNode); } protected function addNodeBeforeNode(Node $newNode, Node $positionNode): void { $this->nodeAddingCommander->addNodeBeforeNode($newNode, $positionNode); - $this->notifyNodeChangeFileInfo($positionNode); + $this->rectorChangeCollector->notifyNodeFileInfo($positionNode); } protected function addPropertyToClass(Class_ $class, ?Type $propertyType, string $propertyName): void { $this->propertyAddingCommander->addPropertyToClass($propertyName, $propertyType, $class); - - $this->notifyNodeChangeFileInfo($class); + $this->rectorChangeCollector->notifyNodeFileInfo($class); } protected function addConstantToClass(Class_ $class, ClassConst $classConst): void { $this->propertyAddingCommander->addConstantToClass($class, $classConst); - - $this->notifyNodeChangeFileInfo($class); + $this->rectorChangeCollector->notifyNodeFileInfo($class); } protected function addPropertyWithoutConstructorToClass( @@ -123,21 +126,21 @@ protected function addPropertyWithoutConstructorToClass( ): void { $this->propertyAddingCommander->addPropertyWithoutConstructorToClass($propertyName, $propertyType, $classNode); - $this->notifyNodeChangeFileInfo($classNode); + $this->rectorChangeCollector->notifyNodeFileInfo($classNode); } protected function removeNode(Node $node): void { $this->nodeRemovingCommander->addNode($node); - $this->notifyNodeChangeFileInfo($node); + $this->rectorChangeCollector->notifyNodeFileInfo($node); } protected function replaceNode(Node $node, Node $replaceWith): void { $this->nodeReplacingCommander->replaceNode($node, $replaceWith); - $this->notifyNodeChangeFileInfo($replaceWith); + $this->rectorChangeCollector->notifyNodeFileInfo($replaceWith); } /** @@ -169,4 +172,9 @@ protected function removeNodes(array $nodes): void $this->removeNode($node); } } + + protected function notifyNodeFileInfo(Node $node): void + { + $this->rectorChangeCollector->notifyNodeFileInfo($node); + } } From ddf8de2e17382780489dbb3646fa0efc779105b2 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Thu, 19 Mar 2020 13:03:29 +0100 Subject: [PATCH 5/6] move AffectedFilesCollector to ChangesReporting --- .../src/Application/ErrorAndDiffCollector.php | 3 +- .../src/Collector}/AffectedFilesCollector.php | 2 +- .../src/Collector/RectorChangeCollector.php | 6 ++++ .../src/Output/CheckstyleOutputFormatter.php | 32 +++++++++---------- .../RectorWithFileAndLineChange.php | 7 ++++ src/Application/FileProcessor.php | 2 +- src/Exception/NotRectorException.php | 18 +++++++++++ .../Node/Commander/NodeRemovingCommander.php | 2 +- 8 files changed, 52 insertions(+), 20 deletions(-) rename {src/Report => packages/changes-reporting/src/Collector}/AffectedFilesCollector.php (93%) create mode 100644 src/Exception/NotRectorException.php diff --git a/packages/changes-reporting/src/Application/ErrorAndDiffCollector.php b/packages/changes-reporting/src/Application/ErrorAndDiffCollector.php index e9f6e44239cf..ced7b1b115b9 100644 --- a/packages/changes-reporting/src/Application/ErrorAndDiffCollector.php +++ b/packages/changes-reporting/src/Application/ErrorAndDiffCollector.php @@ -149,7 +149,8 @@ public function addThrowableWithFileInfo(Throwable $throwable, SmartFileInfo $fi if ($rectorClass) { $this->addErrorWithRectorClassMessageAndFileInfo($rectorClass, $throwable->getMessage(), $fileInfo); } else { - $this->addError(new Error($fileInfo, $throwable->getMessage(), $throwable->getCode())); + $error = new Error($fileInfo, $throwable->getMessage(), $throwable->getCode()); + $this->addError($error); } } } diff --git a/src/Report/AffectedFilesCollector.php b/packages/changes-reporting/src/Collector/AffectedFilesCollector.php similarity index 93% rename from src/Report/AffectedFilesCollector.php rename to packages/changes-reporting/src/Collector/AffectedFilesCollector.php index ef1522211646..2c9be12e611b 100644 --- a/src/Report/AffectedFilesCollector.php +++ b/packages/changes-reporting/src/Collector/AffectedFilesCollector.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Rector\Core\Report; +namespace Rector\ChangesReporting\Collector; use Symplify\SmartFileSystem\SmartFileInfo; diff --git a/packages/changes-reporting/src/Collector/RectorChangeCollector.php b/packages/changes-reporting/src/Collector/RectorChangeCollector.php index 054a5f0929a9..e84f0e4b3632 100644 --- a/packages/changes-reporting/src/Collector/RectorChangeCollector.php +++ b/packages/changes-reporting/src/Collector/RectorChangeCollector.php @@ -6,6 +6,8 @@ use PhpParser\Node; use Rector\ChangesReporting\ValueObject\RectorWithFileAndLineChange; +use Rector\Core\Contract\Rector\RectorInterface; +use Rector\Core\Exception\NotRectorException; use Rector\NodeTypeResolver\Node\AttributeKey; use Symplify\SmartFileSystem\SmartFileInfo; @@ -18,6 +20,10 @@ final class RectorChangeCollector public function addRectorClassWithLine(string $rectorClass, SmartFileInfo $smartFileInfo, int $line): void { + if (! is_a($rectorClass, RectorInterface::class, true)) { + throw new NotRectorException($rectorClass); + } + $this->rectorWithFileAndLineChanges[] = new RectorWithFileAndLineChange( $rectorClass, $smartFileInfo->getRealPath(), diff --git a/packages/changes-reporting/src/Output/CheckstyleOutputFormatter.php b/packages/changes-reporting/src/Output/CheckstyleOutputFormatter.php index 54d26a78c4c7..5b1168074970 100644 --- a/packages/changes-reporting/src/Output/CheckstyleOutputFormatter.php +++ b/packages/changes-reporting/src/Output/CheckstyleOutputFormatter.php @@ -57,6 +57,22 @@ private function escape(string $string): string return htmlspecialchars($string, ENT_XML1 | ENT_COMPAT, 'UTF-8'); } + private function writeFileErrors(FileDiff $fileDiff): void + { + $this->symfonyStyle->writeln(sprintf('', $this->escape($fileDiff->getRelativeFilePath()))); + + foreach ($fileDiff->getRectorChanges() as $rectorChange) { + $error = sprintf( + ' ', + $this->escape((string) $rectorChange->getLine()), + $this->escape((string) $rectorChange->getRectorClass()) + ); + $this->symfonyStyle->writeln($error); + } + + $this->symfonyStyle->writeln(''); + } + private function writeNonFileErrors(ErrorAndDiffCollector $errorAndDiffCollector): void { if ($errorAndDiffCollector->getErrors() !== []) { @@ -73,20 +89,4 @@ private function writeNonFileErrors(ErrorAndDiffCollector $errorAndDiffCollector $this->symfonyStyle->writeln(''); } } - - private function writeFileErrors(FileDiff $fileDiff): void - { - $this->symfonyStyle->writeln(sprintf('', $this->escape($fileDiff->getRelativeFilePath()))); - - foreach ($fileDiff->getRectorChanges() as $rectorChange) { - $error = sprintf( - ' ', - $this->escape((string) $rectorChange->getLine()), - $this->escape((string) $rectorChange->getRectorClass()) - ); - $this->symfonyStyle->writeln($error); - } - - $this->symfonyStyle->writeln(''); - } } diff --git a/packages/changes-reporting/src/ValueObject/RectorWithFileAndLineChange.php b/packages/changes-reporting/src/ValueObject/RectorWithFileAndLineChange.php index 5c4cf1bda389..525a62b660e5 100644 --- a/packages/changes-reporting/src/ValueObject/RectorWithFileAndLineChange.php +++ b/packages/changes-reporting/src/ValueObject/RectorWithFileAndLineChange.php @@ -4,6 +4,9 @@ namespace Rector\ChangesReporting\ValueObject; +use Rector\Core\Contract\Rector\RectorInterface; +use Rector\Core\Exception\NotRectorException; + final class RectorWithFileAndLineChange { /** @@ -23,6 +26,10 @@ final class RectorWithFileAndLineChange public function __construct(string $rectorClass, string $realPath, int $line) { + if (! is_a($rectorClass, RectorInterface::class, true)) { + throw new NotRectorException($rectorClass); + } + $this->rectorClass = $rectorClass; $this->line = $line; $this->realPath = $realPath; diff --git a/src/Application/FileProcessor.php b/src/Application/FileProcessor.php index 9fe90c40d606..9939ce75ae22 100644 --- a/src/Application/FileProcessor.php +++ b/src/Application/FileProcessor.php @@ -6,11 +6,11 @@ use PhpParser\Lexer; use PhpParser\Node; +use Rector\ChangesReporting\Collector\AffectedFilesCollector; use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\PhpParser\NodeTraverser\RectorNodeTraverser; use Rector\Core\PhpParser\Parser\Parser; use Rector\Core\PhpParser\Printer\FormatPerservingPrinter; -use Rector\Core\Report\AffectedFilesCollector; use Rector\Core\Stubs\StubLoader; use Rector\NodeTypeResolver\FileSystem\CurrentFileInfoProvider; use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator; diff --git a/src/Exception/NotRectorException.php b/src/Exception/NotRectorException.php new file mode 100644 index 000000000000..a162ff0410f4 --- /dev/null +++ b/src/Exception/NotRectorException.php @@ -0,0 +1,18 @@ + Date: Thu, 19 Mar 2020 13:46:17 +0100 Subject: [PATCH 6/6] add CurrentRectorProvider --- .github/workflows/annotated_checkstyle.yaml | 2 +- abz/DeadCode.php | 13 ---------- docs/AllRectorsOverview.md | 4 +-- .../src/Collector/RectorChangeCollector.php | 26 ++++++++++++++----- .../src/Output/CheckstyleOutputFormatter.php | 9 +++---- .../RectorWithFileAndLineChange.php | 25 +++++++++++------- src/Logging/CurrentRectorProvider.php | 25 ++++++++++++++++++ src/Rector/AbstractRector.php | 12 ++++++++- .../Standalone/RectorStandaloneRunnerTest.php | 15 +++-------- 9 files changed, 80 insertions(+), 51 deletions(-) delete mode 100644 abz/DeadCode.php create mode 100644 src/Logging/CurrentRectorProvider.php diff --git a/.github/workflows/annotated_checkstyle.yaml b/.github/workflows/annotated_checkstyle.yaml index c7113a5f0265..caed2db3ff75 100644 --- a/.github/workflows/annotated_checkstyle.yaml +++ b/.github/workflows/annotated_checkstyle.yaml @@ -19,4 +19,4 @@ jobs: tools: cs2pr - run: composer install --no-progress - run: | - bin/rector p abz/DeadCode.php --set dead-code -n --autoload-file abz/DeadCode.php --output-format=checkstyle | cs2pr + bin/rector process --config rector-ci.yaml --no-progress-bar --ansi --dry-run --output-format=checkstyle | cs2pr diff --git a/abz/DeadCode.php b/abz/DeadCode.php deleted file mode 100644 index 1cffd858de35..000000000000 --- a/abz/DeadCode.php +++ /dev/null @@ -1,13 +0,0 @@ - -### `SetcookieRector` +### `SetCookieRector` -- class: [`Rector\Php73\Rector\FuncCall\SetcookieRector`](/../master/rules/php-73/src/Rector/FuncCall/SetcookieRector.php) +- class: [`Rector\Php73\Rector\FuncCall\SetCookieRector`](/../master/rules/php-73/src/Rector/FuncCall/SetCookieRector.php) - [test fixtures](/../master/rules/php-73/tests/Rector/FuncCall/SetcookieRector/Fixture) Convert setcookie argument to PHP7.3 option array diff --git a/packages/changes-reporting/src/Collector/RectorChangeCollector.php b/packages/changes-reporting/src/Collector/RectorChangeCollector.php index e84f0e4b3632..022d67496869 100644 --- a/packages/changes-reporting/src/Collector/RectorChangeCollector.php +++ b/packages/changes-reporting/src/Collector/RectorChangeCollector.php @@ -7,7 +7,8 @@ use PhpParser\Node; use Rector\ChangesReporting\ValueObject\RectorWithFileAndLineChange; use Rector\Core\Contract\Rector\RectorInterface; -use Rector\Core\Exception\NotRectorException; +use Rector\Core\Exception\ShouldNotHappenException; +use Rector\Core\Logging\CurrentRectorProvider; use Rector\NodeTypeResolver\Node\AttributeKey; use Symplify\SmartFileSystem\SmartFileInfo; @@ -18,14 +19,20 @@ final class RectorChangeCollector */ private $rectorWithFileAndLineChanges = []; - public function addRectorClassWithLine(string $rectorClass, SmartFileInfo $smartFileInfo, int $line): void + /** + * @var CurrentRectorProvider + */ + private $currentRectorProvider; + + public function __construct(CurrentRectorProvider $currentRectorProvider) { - if (! is_a($rectorClass, RectorInterface::class, true)) { - throw new NotRectorException($rectorClass); - } + $this->currentRectorProvider = $currentRectorProvider; + } + public function addRectorClassWithLine(RectorInterface $rector, SmartFileInfo $smartFileInfo, int $line): void + { $this->rectorWithFileAndLineChanges[] = new RectorWithFileAndLineChange( - $rectorClass, + $rector, $smartFileInfo->getRealPath(), $line ); @@ -53,6 +60,11 @@ public function notifyNodeFileInfo(Node $node): void return; } - $this->addRectorClassWithLine(static::class, $fileInfo, $node->getLine()); + $currentRector = $this->currentRectorProvider->getCurrentRector(); + if ($currentRector === null) { + throw new ShouldNotHappenException(); + } + + $this->addRectorClassWithLine($currentRector, $fileInfo, $node->getLine()); } } diff --git a/packages/changes-reporting/src/Output/CheckstyleOutputFormatter.php b/packages/changes-reporting/src/Output/CheckstyleOutputFormatter.php index 5b1168074970..d77928076b65 100644 --- a/packages/changes-reporting/src/Output/CheckstyleOutputFormatter.php +++ b/packages/changes-reporting/src/Output/CheckstyleOutputFormatter.php @@ -36,10 +36,6 @@ public function getName(): string public function report(ErrorAndDiffCollector $errorAndDiffCollector): void { - if ($errorAndDiffCollector->getErrors() === [] && $errorAndDiffCollector->getFileDiffsCount() === 0) { - return; - } - $this->symfonyStyle->writeln(''); $this->symfonyStyle->writeln(''); @@ -62,10 +58,13 @@ private function writeFileErrors(FileDiff $fileDiff): void $this->symfonyStyle->writeln(sprintf('', $this->escape($fileDiff->getRelativeFilePath()))); foreach ($fileDiff->getRectorChanges() as $rectorChange) { + $message = $rectorChange->getRectorDefinitionsDescription() . ' (Reported by: ' . $rectorChange->getRectorClass() . ')'; + $message = $this->escape($message); + $error = sprintf( ' ', $this->escape((string) $rectorChange->getLine()), - $this->escape((string) $rectorChange->getRectorClass()) + $message ); $this->symfonyStyle->writeln($error); } diff --git a/packages/changes-reporting/src/ValueObject/RectorWithFileAndLineChange.php b/packages/changes-reporting/src/ValueObject/RectorWithFileAndLineChange.php index 525a62b660e5..affb851758df 100644 --- a/packages/changes-reporting/src/ValueObject/RectorWithFileAndLineChange.php +++ b/packages/changes-reporting/src/ValueObject/RectorWithFileAndLineChange.php @@ -5,14 +5,13 @@ namespace Rector\ChangesReporting\ValueObject; use Rector\Core\Contract\Rector\RectorInterface; -use Rector\Core\Exception\NotRectorException; final class RectorWithFileAndLineChange { /** - * @var string + * @var RectorInterface */ - private $rectorClass; + private $rector; /** * @var int @@ -24,20 +23,26 @@ final class RectorWithFileAndLineChange */ private $realPath; - public function __construct(string $rectorClass, string $realPath, int $line) + public function __construct(RectorInterface $rector, string $realPath, int $line) { - if (! is_a($rectorClass, RectorInterface::class, true)) { - throw new NotRectorException($rectorClass); - } - - $this->rectorClass = $rectorClass; + $this->rector = $rector; $this->line = $line; $this->realPath = $realPath; } + public function getRector(): RectorInterface + { + return $this->rector; + } + + public function getRectorDefinitionsDescription(): string + { + return $this->rector->getDefinition()->getDescription(); + } + public function getRectorClass(): string { - return $this->rectorClass; + return get_class($this->rector); } public function getLine(): int diff --git a/src/Logging/CurrentRectorProvider.php b/src/Logging/CurrentRectorProvider.php new file mode 100644 index 000000000000..0f9d4cc14e64 --- /dev/null +++ b/src/Logging/CurrentRectorProvider.php @@ -0,0 +1,25 @@ +currentRector = $rector; + } + + public function getCurrentRector(): ?RectorInterface + { + return $this->currentRector; + } +} diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 6ab3d777d35e..af0b1a619698 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -20,6 +20,7 @@ use Rector\Core\Configuration\Option; use Rector\Core\Contract\Rector\PhpRectorInterface; use Rector\Core\Exclusion\ExclusionManager; +use Rector\Core\Logging\CurrentRectorProvider; use Rector\NodeTypeResolver\FileSystem\CurrentFileInfoProvider; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator; @@ -85,6 +86,11 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn */ protected $staticTypeMapper; + /** + * @var CurrentRectorProvider + */ + private $currentRectorProvider; + /** * @var string[] */ @@ -122,7 +128,8 @@ public function autowireAbstractRectorDependencies( PhpDocInfoPrinter $phpDocInfoPrinter, DocBlockManipulator $docBlockManipulator, StaticTypeMapper $staticTypeMapper, - ParameterProvider $parameterProvider + ParameterProvider $parameterProvider, + CurrentRectorProvider $currentRectorProvider ): void { $this->symfonyStyle = $symfonyStyle; $this->phpVersionProvider = $phpVersionProvider; @@ -134,6 +141,7 @@ public function autowireAbstractRectorDependencies( $this->docBlockManipulator = $docBlockManipulator; $this->staticTypeMapper = $staticTypeMapper; $this->parameterProvider = $parameterProvider; + $this->currentRectorProvider = $currentRectorProvider; } /** @@ -145,6 +153,8 @@ final public function enterNode(Node $node) return null; } + $this->currentRectorProvider->changeCurrentRector($this); + // show current Rector class on --debug if ($this->symfonyStyle->isDebug()) { // indented on purpose to improve log nesting under [refactoring] diff --git a/tests/Standalone/RectorStandaloneRunnerTest.php b/tests/Standalone/RectorStandaloneRunnerTest.php index f62d1a07ab1d..e18f6eb20cbc 100644 --- a/tests/Standalone/RectorStandaloneRunnerTest.php +++ b/tests/Standalone/RectorStandaloneRunnerTest.php @@ -5,24 +5,15 @@ namespace Rector\Core\Tests\Standalone; use PHPUnit\Framework\TestCase; -use Rector\Core\Standalone\RectorStandaloneRunner; use Rector\Core\Standalone\RectorStandaloneRunnerStaticFactory; final class RectorStandaloneRunnerTest extends TestCase { - /** - * @var RectorStandaloneRunner - */ - private $rectorStandaloneRunner; - - protected function setUp(): void - { - $this->rectorStandaloneRunner = RectorStandaloneRunnerStaticFactory::create(); - } - public function test(): void { - $errorAndDiffCollector = $this->rectorStandaloneRunner->processSourceWithSet( + $rectorStandaloneRunner = RectorStandaloneRunnerStaticFactory::create(); + + $errorAndDiffCollector = $rectorStandaloneRunner->processSourceWithSet( [__DIR__ . '/Source/LowQualityFile.php'], 'code-quality', true,