From 0f60135b0806c89763cf847a81c3029bcf9faab4 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 6 Aug 2019 09:37:31 +0200 Subject: [PATCH 1/2] add removed nodes collector --- src/Application/ErrorAndDiffCollector.php | 15 +++++++++++- src/Console/Output/ConsoleOutputFormatter.php | 14 +++++++++++ src/Console/Output/JsonOutputFormatter.php | 1 + src/Rector/AbstractRector.php | 2 +- .../AbstractRector/NodeCommandersTrait.php | 14 +++++++++-- src/Reporting/RemovedNodesCollector.php | 23 +++++++++++++++++++ 6 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 src/Reporting/RemovedNodesCollector.php diff --git a/src/Application/ErrorAndDiffCollector.php b/src/Application/ErrorAndDiffCollector.php index 14dc7506bf9..da5acd355e0 100644 --- a/src/Application/ErrorAndDiffCollector.php +++ b/src/Application/ErrorAndDiffCollector.php @@ -7,6 +7,7 @@ use Rector\Application\FileSystem\RemovedAndAddedFilesCollector; use Rector\ConsoleDiffer\DifferAndFormatter; use Rector\Error\ExceptionCorrector; use Rector\Reporting\FileDiff; +use Rector\Reporting\RemovedNodesCollector; use Symplify\PackageBuilder\FileSystem\SmartFileInfo; use Throwable; @@ -42,16 +43,23 @@ final class ErrorAndDiffCollector */ private $removedAndAddedFilesCollector; + /** + * @var RemovedNodesCollector + */ + private $removedNodesCollector; + public function __construct( DifferAndFormatter $differAndFormatter, AppliedRectorCollector $appliedRectorCollector, ExceptionCorrector $exceptionCorrector, - RemovedAndAddedFilesCollector $removedAndAddedFilesCollector + RemovedAndAddedFilesCollector $removedAndAddedFilesCollector, + RemovedNodesCollector $removedNodesCollector ) { $this->differAndFormatter = $differAndFormatter; $this->appliedRectorCollector = $appliedRectorCollector; $this->exceptionCorrector = $exceptionCorrector; $this->removedAndAddedFilesCollector = $removedAndAddedFilesCollector; + $this->removedNodesCollector = $removedNodesCollector; } public function addError(Error $error): void @@ -72,6 +80,11 @@ final class ErrorAndDiffCollector return $this->removedAndAddedFilesCollector->getAffectedFilesCount(); } + public function getRemovedNodeCount(): int + { + return $this->removedNodesCollector->getCount(); + } + public function addFileDiff(SmartFileInfo $smartFileInfo, string $newContent, string $oldContent): void { if ($newContent === $oldContent) { diff --git a/src/Console/Output/ConsoleOutputFormatter.php b/src/Console/Output/ConsoleOutputFormatter.php index c363c3d87a5..d7bc211b591 100644 --- a/src/Console/Output/ConsoleOutputFormatter.php +++ b/src/Console/Output/ConsoleOutputFormatter.php @@ -29,6 +29,7 @@ final class ConsoleOutputFormatter implements OutputFormatterInterface { $this->reportFileDiffs($errorAndDiffCollector->getFileDiffs()); $this->reportErrors($errorAndDiffCollector->getErrors()); + $this->reportRemovedFilesAndNodes($errorAndDiffCollector); if ($errorAndDiffCollector->getErrors() !== []) { return; @@ -100,4 +101,17 @@ final class ConsoleOutputFormatter implements OutputFormatterInterface $this->symfonyStyle->error($message); } } + + private function reportRemovedFilesAndNodes(ErrorAndDiffCollector $errorAndDiffCollector): void + { + if ($errorAndDiffCollector->getRemovedAndAddedFilesCount()) { + $this->symfonyStyle->note( + sprintf('%d files were added/removed', $errorAndDiffCollector->getRemovedAndAddedFilesCount()) + ); + } + + if ($errorAndDiffCollector->getRemovedNodeCount()) { + $this->symfonyStyle->note(sprintf('%d nodes were removed', $errorAndDiffCollector->getRemovedNodeCount())); + } + } } diff --git a/src/Console/Output/JsonOutputFormatter.php b/src/Console/Output/JsonOutputFormatter.php index 9ffdc96e5af..100c904cf21 100644 --- a/src/Console/Output/JsonOutputFormatter.php +++ b/src/Console/Output/JsonOutputFormatter.php @@ -48,6 +48,7 @@ final class JsonOutputFormatter implements OutputFormatterInterface 'totals' => [ 'changed_files' => count($fileDiffs), 'removed_and_added_files_count' => $errorAndDiffCollector->getRemovedAndAddedFilesCount(), + 'removed_node_count' => $errorAndDiffCollector->getRemovedNodeCount(), ], ]; diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 92b4ce972a6..021a91987e4 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -51,7 +51,7 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn /** * @required */ - public function setAbstractRectorDependencies( + public function autowireAbstractRectorDependencies( SymfonyStyle $symfonyStyle, RemovedAndAddedFilesCollector $removedAndAddedFilesCollector, PhpVersionProvider $phpVersionProvider, diff --git a/src/Rector/AbstractRector/NodeCommandersTrait.php b/src/Rector/AbstractRector/NodeCommandersTrait.php index c1766541eba..d31e49f722f 100644 --- a/src/Rector/AbstractRector/NodeCommandersTrait.php +++ b/src/Rector/AbstractRector/NodeCommandersTrait.php @@ -10,6 +10,7 @@ use Rector\PhpParser\Node\Commander\NodeAddingCommander; use Rector\PhpParser\Node\Commander\NodeRemovingCommander; use Rector\PhpParser\Node\Commander\PropertyAddingCommander; use Rector\PhpParser\Node\VariableInfo; +use Rector\Reporting\RemovedNodesCollector; /** * This could be part of @see AbstractRector, but decopuling to trait @@ -39,19 +40,26 @@ trait NodeCommandersTrait */ private $useAddingCommander; + /** + * @var RemovedNodesCollector + */ + private $removedNodesCollector; + /** * @required */ - public function setRequiredCommanders( + public function autowireNodeCommandersTrait( NodeRemovingCommander $nodeRemovingCommander, NodeAddingCommander $nodeAddingCommander, PropertyAddingCommander $propertyAddingCommander, - UseAddingCommander $useAddingCommander + UseAddingCommander $useAddingCommander, + RemovedNodesCollector $removedNodesCollector ): void { $this->nodeRemovingCommander = $nodeRemovingCommander; $this->nodeAddingCommander = $nodeAddingCommander; $this->propertyAddingCommander = $propertyAddingCommander; $this->useAddingCommander = $useAddingCommander; + $this->removedNodesCollector = $removedNodesCollector; } protected function addNodeAfterNode(Node $newNode, Node $positionNode): void @@ -81,6 +89,8 @@ trait NodeCommandersTrait $this->nodeRemovingCommander->addNode($node); $this->notifyNodeChangeFileInfo($node); + + $this->removedNodesCollector->collect($node); } protected function isNodeRemoved(Node $node): bool diff --git a/src/Reporting/RemovedNodesCollector.php b/src/Reporting/RemovedNodesCollector.php new file mode 100644 index 00000000000..ad128691347 --- /dev/null +++ b/src/Reporting/RemovedNodesCollector.php @@ -0,0 +1,23 @@ +removedNodes[] = $node; + } + + public function getCount(): int + { + return count($this->removedNodes); + } +} From fc6e80c81480579dc03a12e88a6fd270366e3dd2 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 6 Aug 2019 10:13:11 +0200 Subject: [PATCH 2/2] fix scope resolver files for tests --- src/Testing/PHPUnit/AbstractRectorTestCase.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Testing/PHPUnit/AbstractRectorTestCase.php b/src/Testing/PHPUnit/AbstractRectorTestCase.php index d71010dbdd8..bba5383dc87 100644 --- a/src/Testing/PHPUnit/AbstractRectorTestCase.php +++ b/src/Testing/PHPUnit/AbstractRectorTestCase.php @@ -3,6 +3,7 @@ namespace Rector\Testing\PHPUnit; use Nette\Utils\FileSystem; +use PHPStan\Analyser\NodeScopeResolver; use Psr\Container\ContainerInterface; use Rector\Application\FileProcessor; use Rector\Configuration\Option; @@ -45,6 +46,11 @@ abstract class AbstractRectorTestCase extends AbstractKernelTestCase */ private static $allRectorContainer; + /** + * @var NodeScopeResolver + */ + private $nodeScopeResolver; + protected function setUp(): void { $this->fixtureSplitter = new FixtureSplitter($this->getTempPath()); @@ -79,6 +85,9 @@ abstract class AbstractRectorTestCase extends AbstractKernelTestCase $this->fileProcessor = static::$container->get(FileProcessor::class); $this->parameterProvider = static::$container->get(ParameterProvider::class); + + // needed for PHPStan, because the analyzed file is just create in /temp + $this->nodeScopeResolver = static::$container->get(NodeScopeResolver::class); } /** @@ -123,6 +132,9 @@ abstract class AbstractRectorTestCase extends AbstractKernelTestCase $smartFileInfo, $this->autoloadTestFixture ); + + $this->nodeScopeResolver->setAnalysedFiles([$originalFile]); + $this->doTestFileMatchesExpectedContent($originalFile, $changedFile, $smartFileInfo->getRealPath()); }