diff --git a/packages-tests/Caching/Detector/ChangedFilesDetectorTest.php b/packages-tests/Caching/Detector/ChangedFilesDetectorTest.php index 63c9c0c217f..3198bd74b61 100644 --- a/packages-tests/Caching/Detector/ChangedFilesDetectorTest.php +++ b/packages-tests/Caching/Detector/ChangedFilesDetectorTest.php @@ -32,7 +32,7 @@ public function testHasFileChanged(): void $filePath = __DIR__ . '/Source/file.php'; $this->assertTrue($this->changedFilesDetector->hasFileChanged($filePath)); - $this->changedFilesDetector->addFileWithDependencies($filePath); + $this->changedFilesDetector->addFileWithDependencies($filePath, []); $this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath)); $this->changedFilesDetector->invalidateFile($filePath); @@ -46,9 +46,7 @@ public function testHasFileChanged(): void #[DataProvider('provideData')] public function testGetDependentFileInfos(string $filePath, array $dependantFiles): void { - $this->changedFilesDetector->addFileDependentFiles($filePath, $dependantFiles); - $this->changedFilesDetector->addFileWithDependencies($filePath); - + $this->changedFilesDetector->addFileWithDependencies($filePath, $dependantFiles); $dependantFilePaths = $this->changedFilesDetector->getDependentFilePaths($filePath); $dependantFilesCount = count($dependantFiles); diff --git a/packages/Caching/Detector/ChangedFilesDetector.php b/packages/Caching/Detector/ChangedFilesDetector.php index 0c2ab35975b..a630024def6 100644 --- a/packages/Caching/Detector/ChangedFilesDetector.php +++ b/packages/Caching/Detector/ChangedFilesDetector.php @@ -15,11 +15,6 @@ */ final class ChangedFilesDetector { - /** - * @var array - */ - private array $dependentFiles = []; - public function __construct( private readonly FileHashComputer $fileHashComputer, private readonly Cache $cache @@ -29,23 +24,13 @@ public function __construct( /** * @param string[] $dependentFiles */ - public function addFileDependentFiles(string $filePath, array $dependentFiles): void - { - $filePathCacheKey = $this->getFilePathCacheKey($filePath); - $this->dependentFiles[$filePathCacheKey] = $dependentFiles; - } - - public function addFileWithDependencies(string $filePath): void + public function addFileWithDependencies(string $filePath, array $dependentFiles): void { $filePathCacheKey = $this->getFilePathCacheKey($filePath); $hash = $this->hashFile($filePath); $this->cache->save($filePathCacheKey, CacheKey::FILE_HASH_KEY, $hash); - $this->cache->save( - $filePathCacheKey . '_files', - CacheKey::DEPENDENT_FILES_KEY, - $this->dependentFiles[$filePathCacheKey], - ); + $this->cache->save($filePathCacheKey . '_files', CacheKey::DEPENDENT_FILES_KEY, $dependentFiles); } public function hasFileChanged(string $filePath): bool diff --git a/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index 29ea413ac5a..d19463719a5 100644 --- a/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php +++ b/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php @@ -431,6 +431,6 @@ private function resolveAndSaveDependentFiles( } } - $this->changedFilesDetector->addFileDependentFiles($filePath, $dependentFiles); + $this->changedFilesDetector->addFileWithDependencies($filePath, $dependentFiles); } } diff --git a/packages/Parallel/WorkerRunner.php b/packages/Parallel/WorkerRunner.php index 650fa2b13e6..4b177fc2643 100644 --- a/packages/Parallel/WorkerRunner.php +++ b/packages/Parallel/WorkerRunner.php @@ -7,7 +7,6 @@ use Clue\React\NDJson\Decoder; use Clue\React\NDJson\Encoder; use Nette\Utils\FileSystem; -use Rector\Caching\Detector\ChangedFilesDetector; use Rector\Core\Application\ApplicationFileProcessor; use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor; use Rector\Core\Console\Style\RectorConsoleOutputStyle; @@ -42,8 +41,7 @@ public function __construct( private readonly RectorConsoleOutputStyle $rectorConsoleOutputStyle, private readonly RemovedAndAddedFilesProcessor $removedAndAddedFilesProcessor, private readonly ApplicationFileProcessor $applicationFileProcessor, - private readonly ChangedFilesDetector $changedFilesDetector, - private readonly array $fileProcessors = [], + private readonly array $fileProcessors = [] ) { } @@ -87,24 +85,14 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura $this->applicationFileProcessor->configurePHPStanNodeScopeResolver($filePaths, $configuration); foreach ($filePaths as $filePath) { - $file = null; - try { $file = new File($filePath, FileSystem::read($filePath)); $this->currentFileProvider->setFile($file); $errorAndFileDiffs = $this->processFiles($file, $configuration, $errorAndFileDiffs); - - if ($errorAndFileDiffs[Bridge::SYSTEM_ERRORS] !== []) { - $this->changedFilesDetector->invalidateFile($file->getFilePath()); - } else { - $this->changedFilesDetector->addFileWithDependencies($file->getFilePath()); - } } catch (Throwable $throwable) { ++$systemErrorsCount; $systemErrors = $this->collectSystemErrors($systemErrors, $throwable, $filePath); - - $this->invalidateFile($file); } } @@ -170,13 +158,4 @@ private function collectSystemErrors(array $systemErrors, Throwable $throwable, return $systemErrors; } - - private function invalidateFile(?File $file): void - { - if ($file === null) { - return; - } - - $this->changedFilesDetector->invalidateFile($file->getFilePath()); - } } diff --git a/src/Application/ApplicationFileProcessor.php b/src/Application/ApplicationFileProcessor.php index 4b1d6084f5c..01b48a456a2 100644 --- a/src/Application/ApplicationFileProcessor.php +++ b/src/Application/ApplicationFileProcessor.php @@ -5,7 +5,6 @@ namespace Rector\Core\Application; use PHPStan\Analyser\NodeScopeResolver; -use Rector\Caching\Detector\ChangedFilesDetector; use Rector\Core\Application\FileDecorator\FileDiffFileDecorator; use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor; use Rector\Core\Configuration\Option; @@ -53,8 +52,7 @@ public function __construct( private readonly ParameterProvider $parameterProvider, private readonly ScheduleFactory $scheduleFactory, private readonly CpuCoreCountProvider $cpuCoreCountProvider, - private readonly ChangedFilesDetector $changedFilesDetector, - private readonly array $fileProcessors = [], + private readonly array $fileProcessors = [] ) { } @@ -130,12 +128,6 @@ public function processFiles(array $files, Configuration $configuration): array $systemErrorsAndFileDiffs = $this->arrayParametersMerger->merge($systemErrorsAndFileDiffs, $result); } - if ($systemErrorsAndFileDiffs[Bridge::SYSTEM_ERRORS] !== []) { - $this->changedFilesDetector->invalidateFile($file->getFilePath()); - } else { - $this->changedFilesDetector->addFileWithDependencies($file->getFilePath()); - } - // progress bar +1 if ($shouldShowProgressBar) { $this->rectorOutputStyle->progressAdvance(); diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index 68c3a599f1f..1fc50798475 100644 --- a/src/Console/Command/ProcessCommand.php +++ b/src/Console/Command/ProcessCommand.php @@ -89,6 +89,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int $processResult = $this->processResultFactory->create($systemErrorsAndFileDiffs); $outputFormatter->report($processResult, $configuration); + // invalidate affected files + $this->invalidateCacheForChangedAndErroredFiles($processResult); + return $this->resolveReturnCode($processResult, $configuration); } @@ -111,6 +114,22 @@ protected function initialize(InputInterface $input, OutputInterface $output): v } } + private function invalidateCacheForChangedAndErroredFiles(ProcessResult $processResult): void + { + foreach ($processResult->getChangedFilePaths() as $changedFilePath) { + $this->changedFilesDetector->invalidateFile($changedFilePath); + } + + foreach ($processResult->getErrors() as $systemError) { + $errorFile = $systemError->getFile(); + if (! is_string($errorFile)) { + continue; + } + + $this->changedFilesDetector->invalidateFile($errorFile); + } + } + /** * @return ExitCode::* */ diff --git a/src/ValueObject/ProcessResult.php b/src/ValueObject/ProcessResult.php index 9dad397920a..1127bd526ab 100644 --- a/src/ValueObject/ProcessResult.php +++ b/src/ValueObject/ProcessResult.php @@ -63,4 +63,17 @@ public function getRemovedNodeCount(): int { return $this->removedNodeCount; } + + /** + * @return string[] + */ + public function getChangedFilePaths(): array + { + $fileInfos = []; + foreach ($this->fileDiffs as $fileDiff) { + $fileInfos[] = $fileDiff->getRelativeFilePath(); + } + + return array_unique($fileInfos); + } }