Skip to content

Commit

Permalink
[Application] Merge process files on both parallel and non-parallel p…
Browse files Browse the repository at this point in the history
…rocess (#4523)

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 19, 2023
1 parent 6e12886 commit 972d760
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 214 deletions.
4 changes: 0 additions & 4 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocator\IntermediateSourceLocator;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
use Rector\Parallel\WorkerRunner;
use Rector\PhpAttribute\AnnotationToAttributeMapper;
use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
Expand Down Expand Up @@ -319,9 +318,6 @@
$services->set(FileFactory::class)
->arg('$fileProcessors', tagged_iterator(FileProcessorInterface::class));

$services->set(WorkerRunner::class)
->arg('$fileProcessors', tagged_iterator(FileProcessorInterface::class));

$services->set(AnnotationToAttributeMapper::class)
->arg('$annotationToAttributeMappers', tagged_iterator(AnnotationToAttributeMapperInterface::class));
};
110 changes: 9 additions & 101 deletions packages/Parallel/WorkerRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@

use Clue\React\NDJson\Decoder;
use Clue\React\NDJson\Encoder;
use Nette\Utils\FileSystem;
use PHPStan\Analyser\NodeScopeResolver;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Core\Console\Style\RectorConsoleOutputStyle;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\Application\ApplicationFileProcessor;
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;
Expand All @@ -31,17 +24,10 @@ final class WorkerRunner
*/
private const RESULT = 'result';

/**
* @param FileProcessorInterface[] $fileProcessors
*/
public function __construct(
private readonly ArrayParametersMerger $arrayParametersMerger,
private readonly CurrentFileProvider $currentFileProvider,
private readonly DynamicSourceLocatorDecorator $dynamicSourceLocatorDecorator,
private readonly RectorConsoleOutputStyle $rectorConsoleOutputStyle,
private readonly NodeScopeResolver $nodeScopeResolver,
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly iterable $fileProcessors = [],
private readonly ApplicationFileProcessor $applicationFileProcessor,
private readonly NodeScopeResolver $nodeScopeResolver
) {
}

Expand All @@ -51,12 +37,12 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura

// 1. handle system error
$handleErrorCallback = static function (Throwable $throwable) use ($encoder): void {
$systemErrors = new SystemError($throwable->getMessage(), $throwable->getFile(), $throwable->getLine());
$systemError = new SystemError($throwable->getMessage(), $throwable->getFile(), $throwable->getLine());

$encoder->write([
ReactCommand::ACTION => Action::RESULT,
self::RESULT => [
Bridge::SYSTEM_ERRORS => [$systemErrors],
Bridge::SYSTEM_ERRORS => [$systemError],
Bridge::FILES_COUNT => 0,
Bridge::SYSTEM_ERRORS_COUNT => 1,
],
Expand All @@ -73,106 +59,28 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura
return;
}

$systemErrorsCount = 0;

/** @var string[] $filePaths */
$filePaths = $json[Bridge::FILES] ?? [];

$errorAndFileDiffs = [];
$systemErrors = [];

// 1. allow PHPStan to work with static reflection on provided files
$this->nodeScopeResolver->setAnalysedFiles($filePaths);

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() || $errorAndFileDiffs[Bridge::FILE_DIFFS] === []) {
$this->changedFilesDetector->cacheFileWithDependencies($file->getFilePath());
}
} catch (Throwable $throwable) {
++$systemErrorsCount;
$systemErrors = $this->collectSystemErrors($systemErrors, $throwable, $filePath);

$this->invalidateFile($file);
}
}
$systemErrorsAndFileDiffs = $this->applicationFileProcessor->processFiles($filePaths, $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::FILE_DIFFS => $systemErrorsAndFileDiffs[Bridge::FILE_DIFFS],
Bridge::FILES_COUNT => count($filePaths),
Bridge::SYSTEM_ERRORS => $systemErrors,
Bridge::SYSTEM_ERRORS_COUNT => $systemErrorsCount,
Bridge::SYSTEM_ERRORS => $systemErrorsAndFileDiffs[Bridge::SYSTEM_ERRORS],
Bridge::SYSTEM_ERRORS_COUNT => $systemErrorsAndFileDiffs[Bridge::SYSTEM_ERRORS_COUNT],
],
]);
});

$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());
}
}

0 comments on commit 972d760

Please sign in to comment.