Skip to content

Commit

Permalink
Pass analysis errors from worker to the main runner and report them t…
Browse files Browse the repository at this point in the history
…o the error manager
  • Loading branch information
Wirone committed Jan 28, 2024
1 parent 90a26dd commit 6b5ff67
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
10 changes: 1 addition & 9 deletions src/Console/Command/WorkerCommand.php
Expand Up @@ -18,7 +18,6 @@
use Clue\React\NDJson\Encoder;
use PhpCsFixer\Config;
use PhpCsFixer\Console\ConfigurationResolver;
use PhpCsFixer\Error\Error;
use PhpCsFixer\Error\ErrorsManager;
use PhpCsFixer\FixerFileProcessedEvent;
use PhpCsFixer\Runner\Parallel\ParallelConfig;
Expand Down Expand Up @@ -177,14 +176,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
? $this->events[$i]->getStatus()
: null;
$result[$relativePath]['fixInfo'] = $analysisResult[$relativePath] ?? null;
// @TODO consider serialising whole Error, so it can be deserialised on server side and passed to error manager
$result[$relativePath]['errors'] = array_map(
static fn (Error $error): array => [
'type' => $error->getType(),
'error_message' => null !== $error->getSource() ? $error->getSource()->getMessage() : null,
],
$this->errorsManager->forPath($absolutePath)
);
$result[$relativePath]['errors'] = $this->errorsManager->forPath($absolutePath);
}

$out->write(['action' => 'result', 'result' => $result]);
Expand Down
30 changes: 29 additions & 1 deletion src/Error/Error.php
Expand Up @@ -21,7 +21,7 @@
*
* @internal
*/
final class Error
final class Error implements \JsonSerializable
{
/**
* Error which has occurred in linting phase, before applying any fixers.
Expand All @@ -38,6 +38,7 @@ final class Error
*/
public const TYPE_LINT = 3;

/** @var self::TYPE_* */
private int $type;

private string $filePath;
Expand Down Expand Up @@ -90,4 +91,31 @@ public function getDiff(): ?string
{
return $this->diff;
}

/**
* @return array{
* type: self::TYPE_*,
* filePath: string,
* source: null|array{message: string, code: int, file: string, line: int},
* appliedFixers: list<string>,
* diff: null|string
* }
*/
public function jsonSerialize(): array
{
return [
'type' => $this->type,
'filePath' => $this->filePath,
'source' => null !== $this->source
? [
'message' => $this->source->getMessage(),
'code' => $this->source->getCode(),
'file' => $this->source->getFile(),
'line' => $this->source->getLine(),
]
: null,
'appliedFixers' => $this->appliedFixers,
'diff' => $this->diff,
];
}
}
12 changes: 12 additions & 0 deletions src/Runner/Parallel/ParallelisationException.php
Expand Up @@ -27,4 +27,16 @@ public static function forUnknownIdentifier(ProcessIdentifier $identifier): self
{
return new self('Unknown process identifier: '.(string) $identifier);
}

/**
* @param array{message: string, code: int, file: string, line: int} $error
*/
public static function forWorkerError(array $error): self
{
$exception = new self($error['message'], $error['code']);
$exception->file = $error['file'];
$exception->line = $error['line'];

return $exception;
}
}
14 changes: 13 additions & 1 deletion src/Runner/Runner.php
Expand Up @@ -214,7 +214,19 @@ function (array $analysisResult) use ($processPool, $process, $identifier, $file
// Dispatch an event for each file processed and dispatch its status (required for progress output)
$this->dispatchEvent(FixerFileProcessedEvent::NAME, new FixerFileProcessedEvent($result['status']));

// @TODO Pass reported errors to the error manager
foreach ($result['errors'] ?? [] as $workerError) {
$error = new Error(
$workerError['type'],
$workerError['filePath'],
null !== $workerError['source']
? ParallelisationException::forWorkerError($workerError['source'])
: null,
$workerError['appliedFixers'],
$workerError['diff']
);

$this->errorsManager->report($error);
}
}

// Request another chunk of files, if still available
Expand Down

0 comments on commit 6b5ff67

Please sign in to comment.