From 5c037ec4495c1977f41ea01b7ed1111c29b72cf8 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Sat, 12 Nov 2022 20:37:17 +0100 Subject: [PATCH 1/9] e2e: Turn absolute paths into relative ones Otherwise `RemovedAndAddedFilesCollector` will print something like the following and that would need to be baked into `expected-output.diff`: [NOTE] File "/home/user/Projects/rector-src/e2e/removed-and-added-files-collector/test" will be added --- e2e/e2eTestRunner.php | 1 + 1 file changed, 1 insertion(+) diff --git a/e2e/e2eTestRunner.php b/e2e/e2eTestRunner.php index c7be130e324..0bf1730ac7c 100644 --- a/e2e/e2eTestRunner.php +++ b/e2e/e2eTestRunner.php @@ -34,6 +34,7 @@ exec($e2eCommand, $output, $exitCode); $output = trim(implode("\n", $output)); +$output = str_replace(__DIR__, '.', $output); $expectedDiff = 'expected-output.diff'; if (!file_exists($expectedDiff)) { From f0a0bb20b4b0166f2895cc2784f92f7552f50991 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Sat, 12 Nov 2022 20:09:55 +0100 Subject: [PATCH 2/9] e2e: Add test for https://github.com/rectorphp/rector/issues/7231 Currently failing when parallel running is enabled. --- .github/workflows/e2e.yaml | 1 + .../.gitignore | 1 + .../TestRector.php | 66 +++++++++++++++++++ .../composer.json | 10 +++ .../expected-output.diff | 6 ++ .../rector.php | 17 +++++ .../src/Test.php | 4 ++ 7 files changed, 105 insertions(+) create mode 100644 e2e/removed-and-added-files-collector/.gitignore create mode 100644 e2e/removed-and-added-files-collector/TestRector.php create mode 100644 e2e/removed-and-added-files-collector/composer.json create mode 100644 e2e/removed-and-added-files-collector/expected-output.diff create mode 100644 e2e/removed-and-added-files-collector/rector.php create mode 100644 e2e/removed-and-added-files-collector/src/Test.php diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index b682af5cb7c..ddb859558e9 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -33,6 +33,7 @@ jobs: - 'e2e/use-rector-configurator' - 'e2e/applied-rule-removed-node' - 'e2e/parallel with space' + - 'e2e/removed-and-added-files-collector' name: End to end test - ${{ matrix.directory }} diff --git a/e2e/removed-and-added-files-collector/.gitignore b/e2e/removed-and-added-files-collector/.gitignore new file mode 100644 index 00000000000..61ead86667c --- /dev/null +++ b/e2e/removed-and-added-files-collector/.gitignore @@ -0,0 +1 @@ +/vendor diff --git a/e2e/removed-and-added-files-collector/TestRector.php b/e2e/removed-and-added-files-collector/TestRector.php new file mode 100644 index 00000000000..831ccedb390 --- /dev/null +++ b/e2e/removed-and-added-files-collector/TestRector.php @@ -0,0 +1,66 @@ +removedAndAddedFilesCollector = $removedAndAddedFilesCollector; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition( + 'Test adding file.', + [ + new CodeSample( + <<<'CODE_SAMPLE' + function test() {} + CODE_SAMPLE, + <<<'CODE_SAMPLE' + function test() {} + /* file named “test” is added */ + CODE_SAMPLE + ), + ] + ); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [Function_::class]; + } + + /** + * @param Function_ $node + */ + public function refactor(Node $node): ?Node + { + // Create a fixture. + $this->removedAndAddedFilesCollector->addAddedFile( + new AddedFileWithContent( + __DIR__ . '/test', + 'test' + ) + ); + + return $new; + } +} diff --git a/e2e/removed-and-added-files-collector/composer.json b/e2e/removed-and-added-files-collector/composer.json new file mode 100644 index 00000000000..cf3f9a325c0 --- /dev/null +++ b/e2e/removed-and-added-files-collector/composer.json @@ -0,0 +1,10 @@ +{ + "require": { + "php": "^8.1" + }, + "autoload-dev": { + "psr-4": { + "Maintenance\\": "." + } + } +} diff --git a/e2e/removed-and-added-files-collector/expected-output.diff b/e2e/removed-and-added-files-collector/expected-output.diff new file mode 100644 index 00000000000..93bc3b61811 --- /dev/null +++ b/e2e/removed-and-added-files-collector/expected-output.diff @@ -0,0 +1,6 @@ +! [NOTE] File "./removed-and-added-files-collector/test" will be added + + ! [NOTE] 1 files were added + + + [OK] 1 file would have changed (dry-run) by Rector diff --git a/e2e/removed-and-added-files-collector/rector.php b/e2e/removed-and-added-files-collector/rector.php new file mode 100644 index 00000000000..0d5e5cae3c3 --- /dev/null +++ b/e2e/removed-and-added-files-collector/rector.php @@ -0,0 +1,17 @@ +rule(TestRector::class); + $rectorConfig->paths([ + __DIR__ . '/src', + ]); + + // TODO: Make it run in parallel. + $rectorConfig->disableParallel(); +}; diff --git a/e2e/removed-and-added-files-collector/src/Test.php b/e2e/removed-and-added-files-collector/src/Test.php new file mode 100644 index 00000000000..5dfa3887e78 --- /dev/null +++ b/e2e/removed-and-added-files-collector/src/Test.php @@ -0,0 +1,4 @@ + Date: Mon, 19 Dec 2022 15:53:13 +0700 Subject: [PATCH 3/9] [Parallel] Fix missing process RemovedAndAddedFilesProcessor->run() on parallel process on PhpFileProcessor --- e2e/removed-and-added-files-collector/expected-output.diff | 3 +-- e2e/removed-and-added-files-collector/rector.php | 3 --- src/Application/FileProcessor/PhpFileProcessor.php | 4 ++++ 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/e2e/removed-and-added-files-collector/expected-output.diff b/e2e/removed-and-added-files-collector/expected-output.diff index 93bc3b61811..5e0f01b58d4 100644 --- a/e2e/removed-and-added-files-collector/expected-output.diff +++ b/e2e/removed-and-added-files-collector/expected-output.diff @@ -1,6 +1,5 @@ ! [NOTE] File "./removed-and-added-files-collector/test" will be added - ! [NOTE] 1 files were added - [OK] 1 file would have changed (dry-run) by Rector + [OK] Rector is done! \ No newline at end of file diff --git a/e2e/removed-and-added-files-collector/rector.php b/e2e/removed-and-added-files-collector/rector.php index 0d5e5cae3c3..c52824bc0a2 100644 --- a/e2e/removed-and-added-files-collector/rector.php +++ b/e2e/removed-and-added-files-collector/rector.php @@ -11,7 +11,4 @@ $rectorConfig->paths([ __DIR__ . '/src', ]); - - // TODO: Make it run in parallel. - $rectorConfig->disableParallel(); }; diff --git a/src/Application/FileProcessor/PhpFileProcessor.php b/src/Application/FileProcessor/PhpFileProcessor.php index 2c20a8da73e..c1d52b9eab1 100644 --- a/src/Application/FileProcessor/PhpFileProcessor.php +++ b/src/Application/FileProcessor/PhpFileProcessor.php @@ -9,6 +9,7 @@ use Rector\Core\Application\FileDecorator\FileDiffFileDecorator; use Rector\Core\Application\FileProcessor; use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector; +use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor; use Rector\Core\Contract\Console\OutputStyleInterface; use Rector\Core\Contract\Processor\FileProcessorInterface; use Rector\Core\Exception\ShouldNotHappenException; @@ -30,6 +31,7 @@ public function __construct( private readonly FormatPerservingPrinter $formatPerservingPrinter, private readonly FileProcessor $fileProcessor, private readonly RemovedAndAddedFilesCollector $removedAndAddedFilesCollector, + private readonly RemovedAndAddedFilesProcessor $removedAndAddedFilesProcessor, private readonly OutputStyleInterface $rectorOutputStyle, private readonly FileDiffFileDecorator $fileDiffFileDecorator, private readonly CurrentFileProvider $currentFileProvider, @@ -71,6 +73,8 @@ public function process(File $file, Configuration $configuration): array // 4. print to file or string // important to detect if file has changed $this->printFile($file, $configuration); + + $this->removedAndAddedFilesProcessor->run($configuration); } while ($file->hasChanged()); // return json here From 53caa84b2c7d5ec23e0f93d197a2765024e52823 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 19 Dec 2022 15:56:31 +0700 Subject: [PATCH 4/9] use expected output on CI --- e2e/removed-and-added-files-collector/expected-output.diff | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/e2e/removed-and-added-files-collector/expected-output.diff b/e2e/removed-and-added-files-collector/expected-output.diff index 5e0f01b58d4..14879270096 100644 --- a/e2e/removed-and-added-files-collector/expected-output.diff +++ b/e2e/removed-and-added-files-collector/expected-output.diff @@ -1,4 +1,6 @@ -! [NOTE] File "./removed-and-added-files-collector/test" will be added +! [NOTE] File + ! "./removed-and-added-files-c + ! ollector/test" will be added From 5084f1d4e8cb44bab319c5e24036db9539a92f0f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 19 Dec 2022 16:09:27 +0700 Subject: [PATCH 5/9] Final touch: move after do while loop --- src/Application/FileProcessor/PhpFileProcessor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Application/FileProcessor/PhpFileProcessor.php b/src/Application/FileProcessor/PhpFileProcessor.php index c1d52b9eab1..0609ffa30c7 100644 --- a/src/Application/FileProcessor/PhpFileProcessor.php +++ b/src/Application/FileProcessor/PhpFileProcessor.php @@ -73,10 +73,10 @@ public function process(File $file, Configuration $configuration): array // 4. print to file or string // important to detect if file has changed $this->printFile($file, $configuration); - - $this->removedAndAddedFilesProcessor->run($configuration); } while ($file->hasChanged()); + $this->removedAndAddedFilesProcessor->run($configuration); + // return json here $fileDiff = $file->getFileDiff(); if (! $fileDiff instanceof FileDiff) { From 2431a596545ba1cdcf26e196c5b1265719dcc3b2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 19 Dec 2022 16:41:00 +0700 Subject: [PATCH 6/9] move to worker runner --- packages/Parallel/WorkerRunner.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/Parallel/WorkerRunner.php b/packages/Parallel/WorkerRunner.php index 1693dc36aa0..83ca4b8883f 100644 --- a/packages/Parallel/WorkerRunner.php +++ b/packages/Parallel/WorkerRunner.php @@ -9,6 +9,7 @@ use Nette\Utils\FileSystem; use PHPStan\Analyser\NodeScopeResolver; use Rector\Core\Application\FileProcessor\PhpFileProcessor; +use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor; use Rector\Core\Console\Style\RectorConsoleOutputStyle; use Rector\Core\Provider\CurrentFileProvider; use Rector\Core\StaticReflection\DynamicSourceLocatorDecorator; @@ -35,7 +36,8 @@ public function __construct( private readonly PhpFileProcessor $phpFileProcessor, private readonly NodeScopeResolver $nodeScopeResolver, private readonly DynamicSourceLocatorDecorator $dynamicSourceLocatorDecorator, - private readonly RectorConsoleOutputStyle $rectorConsoleOutputStyle + private readonly RectorConsoleOutputStyle $rectorConsoleOutputStyle, + private readonly RemovedAndAddedFilesProcessor $removedAndAddedFilesProcessor ) { } @@ -111,6 +113,8 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura ++$systemErrorsCount; $systemErrors = $this->collectSystemErrors($systemErrors, $throwable, $filePath); } + + $this->removedAndAddedFilesProcessor->run($configuration); } /** From bc75b831d56c3411c4ff29ca0b9d5aad466fdae9 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 19 Dec 2022 16:42:31 +0700 Subject: [PATCH 7/9] remove run at PhpFileProcessor --- src/Application/FileProcessor/PhpFileProcessor.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Application/FileProcessor/PhpFileProcessor.php b/src/Application/FileProcessor/PhpFileProcessor.php index 0609ffa30c7..2a5529a3511 100644 --- a/src/Application/FileProcessor/PhpFileProcessor.php +++ b/src/Application/FileProcessor/PhpFileProcessor.php @@ -75,8 +75,6 @@ public function process(File $file, Configuration $configuration): array $this->printFile($file, $configuration); } while ($file->hasChanged()); - $this->removedAndAddedFilesProcessor->run($configuration); - // return json here $fileDiff = $file->getFileDiff(); if (! $fileDiff instanceof FileDiff) { From 19f544275d033811515c48bce97ccf5a82c3128d Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Mon, 19 Dec 2022 09:46:35 +0000 Subject: [PATCH 8/9] [ci-review] Rector Rectify --- src/Application/FileProcessor/PhpFileProcessor.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Application/FileProcessor/PhpFileProcessor.php b/src/Application/FileProcessor/PhpFileProcessor.php index 2a5529a3511..2c20a8da73e 100644 --- a/src/Application/FileProcessor/PhpFileProcessor.php +++ b/src/Application/FileProcessor/PhpFileProcessor.php @@ -9,7 +9,6 @@ use Rector\Core\Application\FileDecorator\FileDiffFileDecorator; use Rector\Core\Application\FileProcessor; use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector; -use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor; use Rector\Core\Contract\Console\OutputStyleInterface; use Rector\Core\Contract\Processor\FileProcessorInterface; use Rector\Core\Exception\ShouldNotHappenException; @@ -31,7 +30,6 @@ public function __construct( private readonly FormatPerservingPrinter $formatPerservingPrinter, private readonly FileProcessor $fileProcessor, private readonly RemovedAndAddedFilesCollector $removedAndAddedFilesCollector, - private readonly RemovedAndAddedFilesProcessor $removedAndAddedFilesProcessor, private readonly OutputStyleInterface $rectorOutputStyle, private readonly FileDiffFileDecorator $fileDiffFileDecorator, private readonly CurrentFileProvider $currentFileProvider, From b0d97319d6ff4383e7139e487f2bf311758f5c73 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 19 Dec 2022 17:51:26 +0700 Subject: [PATCH 9/9] use FileProcessorInterface to apply all FileProcessor classes --- packages/Parallel/WorkerRunner.php | 45 ++++++++++++++++++++---------- phpstan.neon | 1 + 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/packages/Parallel/WorkerRunner.php b/packages/Parallel/WorkerRunner.php index 83ca4b8883f..38cb1d6f2a3 100644 --- a/packages/Parallel/WorkerRunner.php +++ b/packages/Parallel/WorkerRunner.php @@ -8,15 +8,16 @@ use Clue\React\NDJson\Encoder; use Nette\Utils\FileSystem; use PHPStan\Analyser\NodeScopeResolver; -use Rector\Core\Application\FileProcessor\PhpFileProcessor; use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor; use Rector\Core\Console\Style\RectorConsoleOutputStyle; +use Rector\Core\Contract\Processor\FileProcessorInterface; use Rector\Core\Provider\CurrentFileProvider; use Rector\Core\StaticReflection\DynamicSourceLocatorDecorator; use Rector\Core\Util\ArrayParametersMerger; use Rector\Core\ValueObject\Application\File; use Rector\Core\ValueObject\Configuration; use Rector\Core\ValueObject\Error\SystemError; +use Rector\Core\ValueObject\Reporting\FileDiff; use Rector\Parallel\ValueObject\Bridge; use Symplify\EasyParallel\Enum\Action; use Symplify\EasyParallel\Enum\ReactCommand; @@ -30,14 +31,17 @@ final class WorkerRunner */ private const RESULT = 'result'; + /** + * @param FileProcessorInterface[] $fileProcessors + */ public function __construct( private readonly ArrayParametersMerger $arrayParametersMerger, private readonly CurrentFileProvider $currentFileProvider, - private readonly PhpFileProcessor $phpFileProcessor, private readonly NodeScopeResolver $nodeScopeResolver, private readonly DynamicSourceLocatorDecorator $dynamicSourceLocatorDecorator, private readonly RectorConsoleOutputStyle $rectorConsoleOutputStyle, - private readonly RemovedAndAddedFilesProcessor $removedAndAddedFilesProcessor + private readonly RemovedAndAddedFilesProcessor $removedAndAddedFilesProcessor, + private readonly array $fileProcessors = [] ) { } @@ -85,16 +89,7 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura $file = new File($filePath, FileSystem::read($filePath)); $this->currentFileProvider->setFile($file); - if (! $this->phpFileProcessor->supports($file, $configuration)) { - continue; - } - - $currentErrorsAndFileDiffs = $this->phpFileProcessor->process($file, $configuration); - - $errorAndFileDiffs = $this->arrayParametersMerger->merge( - $errorAndFileDiffs, - $currentErrorsAndFileDiffs - ); + $errorAndFileDiffs = $this->processFiles($file, $configuration, $errorAndFileDiffs); // warn about deprecated @noRector annotation if (! str_ends_with($file->getFilePath(), 'WorkerRunner.php') @@ -113,8 +108,6 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura ++$systemErrorsCount; $systemErrors = $this->collectSystemErrors($systemErrors, $throwable, $filePath); } - - $this->removedAndAddedFilesProcessor->run($configuration); } /** @@ -134,6 +127,28 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura $decoder->on(ReactEvent::ERROR, $handleErrorCallback); } + /** + * @param array{system_errors: SystemError[], file_diffs: FileDiff[]}|mixed[] $errorAndFileDiffs + * @return array{system_errors: SystemError[], file_diffs: FileDiff[]} + */ + public function processFiles(File $file, Configuration $configuration, array $errorAndFileDiffs): array + { + foreach ($this->fileProcessors as $fileProcessor) { + if (! $fileProcessor->supports($file, $configuration)) { + continue; + } + + $currentErrorsAndFileDiffs = $fileProcessor->process($file, $configuration); + $errorAndFileDiffs = $this->arrayParametersMerger->merge( + $errorAndFileDiffs, + $currentErrorsAndFileDiffs + ); + } + + $this->removedAndAddedFilesProcessor->run($configuration); + return $errorAndFileDiffs; + } + /** * @param SystemError[] $systemErrors * @return SystemError[] diff --git a/phpstan.neon b/phpstan.neon index 7e94c30062a..617d08701dd 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -329,6 +329,7 @@ parameters: - packages/BetterPhpDocParser/ValueObject/Parser/BetterTokenIterator.php # reported only in non-CI, for some reason; keep it there even if it's reported as fixed error - packages/Parallel/Application/ParallelFileProcessor.php + - packages/Parallel/WorkerRunner.php # skipped on purpose, as ctor overrie - '#Rector\\StaticTypeMapper\\ValueObject\\Type\\SimpleStaticType\:\:__construct\(\) does not call parent constructor from PHPStan\\Type\\StaticType#'