arrayParametersMerger = $arrayParametersMerger; $this->currentFileProvider = $currentFileProvider; $this->dynamicSourceLocatorDecorator = $dynamicSourceLocatorDecorator; $this->rectorConsoleOutputStyle = $rectorConsoleOutputStyle; $this->removedAndAddedFilesProcessor = $removedAndAddedFilesProcessor; $this->applicationFileProcessor = $applicationFileProcessor; $this->changedFilesDetector = $changedFilesDetector; $this->fileProcessors = $fileProcessors; } public function run(Encoder $encoder, Decoder $decoder, Configuration $configuration) : void { $this->dynamicSourceLocatorDecorator->addPaths($configuration->getPaths()); // 1. handle system error $handleErrorCallback = static function (Throwable $throwable) use($encoder) : void { $systemErrors = new SystemError($throwable->getMessage(), $throwable->getFile(), $throwable->getLine()); $encoder->write([ReactCommand::ACTION => Action::RESULT, self::RESULT => [Bridge::SYSTEM_ERRORS => [$systemErrors], Bridge::FILES_COUNT => 0, Bridge::SYSTEM_ERRORS_COUNT => 1]]); $encoder->end(); }; $encoder->on(ReactEvent::ERROR, $handleErrorCallback); // 2. collect diffs + errors from file processor $decoder->on(ReactEvent::DATA, function (array $json) use($encoder, $configuration) : void { $action = $json[ReactCommand::ACTION]; if ($action !== Action::MAIN) { return; } $systemErrorsCount = 0; /** @var string[] $filePaths */ $filePaths = $json[Bridge::FILES] ?? []; $errorAndFileDiffs = []; $systemErrors = []; // 1. allow PHPStan to work with static reflection on provided files $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->processFile($file, $configuration, $errorAndFileDiffs); if ($errorAndFileDiffs[Bridge::SYSTEM_ERRORS] !== []) { $this->invalidateFile($file); } elseif (!$configuration->isDryRun()) { $this->changedFilesDetector->cacheFileWithDependencies($file->getFilePath()); } } catch (Throwable $throwable) { ++$systemErrorsCount; $systemErrors = $this->collectSystemErrors($systemErrors, $throwable, $filePath); $this->invalidateFile($file); } } $this->removedAndAddedFilesProcessor->run($configuration); /** * this invokes all listeners listening $decoder->on(...) @see \Symplify\EasyParallel\Enum\ReactEvent::DATA */ $encoder->write([ReactCommand::ACTION => Action::RESULT, self::RESULT => [Bridge::FILE_DIFFS => $errorAndFileDiffs[Bridge::FILE_DIFFS] ?? [], Bridge::FILES_COUNT => \count($filePaths), Bridge::SYSTEM_ERRORS => $systemErrors, Bridge::SYSTEM_ERRORS_COUNT => $systemErrorsCount]]); }); $decoder->on(ReactEvent::ERROR, $handleErrorCallback); } /** * @param array{system_errors: SystemError[], file_diffs: FileDiff[]}|mixed[] $errorAndFileDiffs * @return array{system_errors: SystemError[], file_diffs: FileDiff[]} */ private function processFile(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); } return $errorAndFileDiffs; } /** * @param SystemError[] $systemErrors * @return SystemError[] */ private function collectSystemErrors(array $systemErrors, Throwable $throwable, string $filePath) : array { $errorMessage = \sprintf('System error: "%s"', $throwable->getMessage()) . \PHP_EOL; if ($this->rectorConsoleOutputStyle->isDebug()) { $systemErrors[] = new SystemError($errorMessage . \PHP_EOL . 'Stack trace:' . \PHP_EOL . $throwable->getTraceAsString(), $filePath, $throwable->getLine()); return $systemErrors; } $errorMessage .= 'Run Rector with "--debug" option and post the report here: https://github.com/rectorphp/rector/issues/new'; $systemErrors[] = new SystemError($errorMessage, $filePath, $throwable->getLine()); return $systemErrors; } private function invalidateFile(?File $file) : void { if (!$file instanceof File) { return; } $this->changedFilesDetector->invalidateFile($file->getFilePath()); } }