Skip to content

Commit

Permalink
use FileProcessorInterface to apply all FileProcessor classes
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Dec 19, 2022
1 parent 19f5442 commit b0d9731
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
45 changes: 30 additions & 15 deletions packages/Parallel/WorkerRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
use Clue\React\NDJson\Encoder;
use Nette\Utils\FileSystem;
use PHPStan\Analyser\NodeScopeResolver;
use Rector\Core\Application\FileProcessor\PhpFileProcessor;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor;
use Rector\Core\Console\Style\RectorConsoleOutputStyle;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Provider\CurrentFileProvider;
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 @@ -30,14 +31,17 @@ final class WorkerRunner
*/
private const RESULT = 'result';

/**
* @param FileProcessorInterface[] $fileProcessors
*/
public function __construct(
private readonly ArrayParametersMerger $arrayParametersMerger,
private readonly CurrentFileProvider $currentFileProvider,
private readonly PhpFileProcessor $phpFileProcessor,
private readonly NodeScopeResolver $nodeScopeResolver,
private readonly DynamicSourceLocatorDecorator $dynamicSourceLocatorDecorator,
private readonly RectorConsoleOutputStyle $rectorConsoleOutputStyle,
private readonly RemovedAndAddedFilesProcessor $removedAndAddedFilesProcessor
private readonly RemovedAndAddedFilesProcessor $removedAndAddedFilesProcessor,
private readonly array $fileProcessors = []
) {
}

Expand Down Expand Up @@ -85,16 +89,7 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura
$file = new File($filePath, FileSystem::read($filePath));
$this->currentFileProvider->setFile($file);

if (! $this->phpFileProcessor->supports($file, $configuration)) {
continue;
}

$currentErrorsAndFileDiffs = $this->phpFileProcessor->process($file, $configuration);

$errorAndFileDiffs = $this->arrayParametersMerger->merge(
$errorAndFileDiffs,
$currentErrorsAndFileDiffs
);
$errorAndFileDiffs = $this->processFiles($file, $configuration, $errorAndFileDiffs);

// warn about deprecated @noRector annotation
if (! str_ends_with($file->getFilePath(), 'WorkerRunner.php')
Expand All @@ -113,8 +108,6 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura
++$systemErrorsCount;
$systemErrors = $this->collectSystemErrors($systemErrors, $throwable, $filePath);
}

$this->removedAndAddedFilesProcessor->run($configuration);
}

/**
Expand All @@ -134,6 +127,28 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura
$decoder->on(ReactEvent::ERROR, $handleErrorCallback);
}

/**
* @param array{system_errors: SystemError[], file_diffs: FileDiff[]}|mixed[] $errorAndFileDiffs
* @return array{system_errors: SystemError[], file_diffs: FileDiff[]}
*/
public function processFiles(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
);
}

$this->removedAndAddedFilesProcessor->run($configuration);
return $errorAndFileDiffs;
}

/**
* @param SystemError[] $systemErrors
* @return SystemError[]
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ parameters:
- packages/BetterPhpDocParser/ValueObject/Parser/BetterTokenIterator.php
# reported only in non-CI, for some reason; keep it there even if it's reported as fixed error
- packages/Parallel/Application/ParallelFileProcessor.php
- packages/Parallel/WorkerRunner.php

# skipped on purpose, as ctor overrie
- '#Rector\\StaticTypeMapper\\ValueObject\\Type\\SimpleStaticType\:\:__construct\(\) does not call parent constructor from PHPStan\\Type\\StaticType#'
Expand Down

0 comments on commit b0d9731

Please sign in to comment.