From 0a43a7a7e70d6e5b8b8531b494874a35085f5f25 Mon Sep 17 00:00:00 2001 From: zingimmick Date: Sat, 1 Jan 2022 23:15:59 +0800 Subject: [PATCH 1/4] [Parallel] Fix system errors expected an instance of SystemError but got string --- .../Output/ConsoleOutputFormatter.php | 6 +++++- .../Output/JsonOutputFormatter.php | 14 +++++++++++--- src/ValueObject/ProcessResult.php | 5 ++--- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/ChangesReporting/Output/ConsoleOutputFormatter.php b/packages/ChangesReporting/Output/ConsoleOutputFormatter.php index 67529600731..f1e450906ed 100644 --- a/packages/ChangesReporting/Output/ConsoleOutputFormatter.php +++ b/packages/ChangesReporting/Output/ConsoleOutputFormatter.php @@ -101,11 +101,15 @@ private function reportFileDiffs(array $fileDiffs): void } /** - * @param SystemError[] $errors + * @param array $errors */ private function reportErrors(array $errors): void { foreach ($errors as $error) { + if (! $error instanceof SystemError) { + $this->outputStyle->error($error); + continue; + } $errorMessage = $error->getMessage(); $errorMessage = $this->normalizePathsToRelativeWithLine($errorMessage); diff --git a/packages/ChangesReporting/Output/JsonOutputFormatter.php b/packages/ChangesReporting/Output/JsonOutputFormatter.php index d39f9cb6a19..dd7065b8e5d 100644 --- a/packages/ChangesReporting/Output/JsonOutputFormatter.php +++ b/packages/ChangesReporting/Output/JsonOutputFormatter.php @@ -8,6 +8,7 @@ use Rector\ChangesReporting\Annotation\RectorsChangelogResolver; use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface; use Rector\Core\ValueObject\Configuration; +use Rector\Core\ValueObject\Error\SystemError; use Rector\Core\ValueObject\ProcessResult; use Rector\Parallel\ValueObject\Bridge; @@ -72,7 +73,7 @@ public function report(ProcessResult $processResult, Configuration $configuratio } /** - * @param mixed[] $errors + * @param array $errors * @return mixed[] */ private function createErrorsData(array $errors): array @@ -80,12 +81,19 @@ private function createErrorsData(array $errors): array $errorsData = []; foreach ($errors as $error) { + if (! $error instanceof SystemError) { + $errorDataJson = [ + 'message' => $error, + ]; + $errorsData[] = $errorDataJson; + continue; + } $errorDataJson = [ 'message' => $error->getMessage(), - 'file' => $error->getRelativeFilePath(), + 'file' => $error->getFile(), ]; - if ($error->getRectorClass()) { + if ($error->getRectorClass() !== null) { $errorDataJson['caused_by'] = $error->getRectorClass(); } diff --git a/src/ValueObject/ProcessResult.php b/src/ValueObject/ProcessResult.php index 8c17cdc0beb..c2c6e6bf105 100644 --- a/src/ValueObject/ProcessResult.php +++ b/src/ValueObject/ProcessResult.php @@ -16,7 +16,7 @@ final class ProcessResult { /** * @param FileDiff[] $fileDiffs - * @param SystemError[] $systemErrors + * @param array $systemErrors */ public function __construct( private readonly array $systemErrors, @@ -26,7 +26,6 @@ public function __construct( private readonly int $removedNodeCount ) { Assert::allIsAOf($fileDiffs, FileDiff::class); - Assert::allIsAOf($systemErrors, SystemError::class); } /** @@ -38,7 +37,7 @@ public function getFileDiffs(): array } /** - * @return SystemError[] + * @return array */ public function getErrors(): array { From dabb08f92040e22df74c934ac6f2b0e4566e9060 Mon Sep 17 00:00:00 2001 From: zingimmick Date: Sun, 2 Jan 2022 13:36:20 +0800 Subject: [PATCH 2/4] Revert "[Parallel] Fix system errors expected an instance of SystemError but got string" This reverts commit 0a43a7a7e70d6e5b8b8531b494874a35085f5f25. --- .../Output/ConsoleOutputFormatter.php | 6 +----- .../Output/JsonOutputFormatter.php | 14 +++----------- src/ValueObject/ProcessResult.php | 5 +++-- 3 files changed, 7 insertions(+), 18 deletions(-) diff --git a/packages/ChangesReporting/Output/ConsoleOutputFormatter.php b/packages/ChangesReporting/Output/ConsoleOutputFormatter.php index f1e450906ed..67529600731 100644 --- a/packages/ChangesReporting/Output/ConsoleOutputFormatter.php +++ b/packages/ChangesReporting/Output/ConsoleOutputFormatter.php @@ -101,15 +101,11 @@ private function reportFileDiffs(array $fileDiffs): void } /** - * @param array $errors + * @param SystemError[] $errors */ private function reportErrors(array $errors): void { foreach ($errors as $error) { - if (! $error instanceof SystemError) { - $this->outputStyle->error($error); - continue; - } $errorMessage = $error->getMessage(); $errorMessage = $this->normalizePathsToRelativeWithLine($errorMessage); diff --git a/packages/ChangesReporting/Output/JsonOutputFormatter.php b/packages/ChangesReporting/Output/JsonOutputFormatter.php index dd7065b8e5d..d39f9cb6a19 100644 --- a/packages/ChangesReporting/Output/JsonOutputFormatter.php +++ b/packages/ChangesReporting/Output/JsonOutputFormatter.php @@ -8,7 +8,6 @@ use Rector\ChangesReporting\Annotation\RectorsChangelogResolver; use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface; use Rector\Core\ValueObject\Configuration; -use Rector\Core\ValueObject\Error\SystemError; use Rector\Core\ValueObject\ProcessResult; use Rector\Parallel\ValueObject\Bridge; @@ -73,7 +72,7 @@ public function report(ProcessResult $processResult, Configuration $configuratio } /** - * @param array $errors + * @param mixed[] $errors * @return mixed[] */ private function createErrorsData(array $errors): array @@ -81,19 +80,12 @@ private function createErrorsData(array $errors): array $errorsData = []; foreach ($errors as $error) { - if (! $error instanceof SystemError) { - $errorDataJson = [ - 'message' => $error, - ]; - $errorsData[] = $errorDataJson; - continue; - } $errorDataJson = [ 'message' => $error->getMessage(), - 'file' => $error->getFile(), + 'file' => $error->getRelativeFilePath(), ]; - if ($error->getRectorClass() !== null) { + if ($error->getRectorClass()) { $errorDataJson['caused_by'] = $error->getRectorClass(); } diff --git a/src/ValueObject/ProcessResult.php b/src/ValueObject/ProcessResult.php index c2c6e6bf105..8c17cdc0beb 100644 --- a/src/ValueObject/ProcessResult.php +++ b/src/ValueObject/ProcessResult.php @@ -16,7 +16,7 @@ final class ProcessResult { /** * @param FileDiff[] $fileDiffs - * @param array $systemErrors + * @param SystemError[] $systemErrors */ public function __construct( private readonly array $systemErrors, @@ -26,6 +26,7 @@ public function __construct( private readonly int $removedNodeCount ) { Assert::allIsAOf($fileDiffs, FileDiff::class); + Assert::allIsAOf($systemErrors, SystemError::class); } /** @@ -37,7 +38,7 @@ public function getFileDiffs(): array } /** - * @return array + * @return SystemError[] */ public function getErrors(): array { From 880d359f888de37ddb4ec0212ae94335c55ab3c8 Mon Sep 17 00:00:00 2001 From: zingimmick Date: Sun, 2 Jan 2022 13:48:46 +0800 Subject: [PATCH 3/4] Make the relativeFilePath optional --- packages/Parallel/Application/ParallelFileProcessor.php | 8 ++++---- src/ValueObject/Error/SystemError.php | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/Parallel/Application/ParallelFileProcessor.php b/packages/Parallel/Application/ParallelFileProcessor.php index c1e2d01d4d4..b2be356e12f 100644 --- a/packages/Parallel/Application/ParallelFileProcessor.php +++ b/packages/Parallel/Application/ParallelFileProcessor.php @@ -166,7 +166,7 @@ function (array $json) use ( // decode arrays to objects foreach ($json[Bridge::SYSTEM_ERRORS] as $jsonError) { if (is_string($jsonError)) { - $systemErrors[] = 'System error: ' . $jsonError; + $systemErrors[] = new SystemError('System error: ' . $jsonError); continue; } @@ -211,7 +211,7 @@ function ($exitCode, string $stdErr) use (&$systemErrors, $processIdentifier): v return; } - $systemErrors[] = 'Child process error: ' . $stdErr; + $systemErrors[] = new SystemError('Child process error: ' . $stdErr); } ); @@ -221,10 +221,10 @@ function ($exitCode, string $stdErr) use (&$systemErrors, $processIdentifier): v $streamSelectLoop->run(); if ($reachedSystemErrorsCountLimit) { - $systemErrors[] = sprintf( + $systemErrors[] = new SystemError(sprintf( 'Reached system errors count limit of %d, exiting...', self::SYSTEM_ERROR_COUNT_LIMIT - ); + )); } return [ diff --git a/src/ValueObject/Error/SystemError.php b/src/ValueObject/Error/SystemError.php index e2af17e904f..5a11c57bc69 100644 --- a/src/ValueObject/Error/SystemError.php +++ b/src/ValueObject/Error/SystemError.php @@ -11,7 +11,7 @@ final class SystemError implements SerializableInterface { public function __construct( private readonly string $message, - private readonly string $relativeFilePath, + private readonly string|null $relativeFilePath = null, private readonly int|null $line = null, private readonly string|null $rectorClass = null ) { @@ -22,7 +22,7 @@ public function getMessage(): string return $this->message; } - public function getFile(): string + public function getFile(): string|null { return $this->relativeFilePath; } @@ -38,7 +38,7 @@ public function getFileWithLine(): string } /** - * @return array{message: string, relative_file_path: string, line: int|null, rector_class: string|null} + * @return array{message: string, relative_file_path: string|null, line: int|null, rector_class: string|null} */ public function jsonSerialize(): array { From 071b1245dcb19cdec1b50d240540aeb3ddd6a3a5 Mon Sep 17 00:00:00 2001 From: zingimmick Date: Mon, 3 Jan 2022 11:26:08 +0800 Subject: [PATCH 4/4] Add PHPDoc type for $systemErrors --- packages/Parallel/Application/ParallelFileProcessor.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/Parallel/Application/ParallelFileProcessor.php b/packages/Parallel/Application/ParallelFileProcessor.php index b2be356e12f..83d4ed2f971 100644 --- a/packages/Parallel/Application/ParallelFileProcessor.php +++ b/packages/Parallel/Application/ParallelFileProcessor.php @@ -74,6 +74,7 @@ public function process( // initial counters $fileDiffs = []; + /** @var SystemError[] $systemErrors */ $systemErrors = []; $tcpServer = new TcpServer('127.0.0.1:0', $streamSelectLoop);