Skip to content

Commit

Permalink
[ApplicationFileProcessor] Refactor ApplicationFileProcessor to filte…
Browse files Browse the repository at this point in the history
…r file paths early before run both parallel and non-parallel (#4519)
  • Loading branch information
samsonasik committed Jul 15, 2023
1 parent 9217e0d commit e77f6d2
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 38 deletions.
12 changes: 2 additions & 10 deletions packages/ChangesReporting/Output/ConsoleOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,13 @@ public function __construct(

public function report(ProcessResult $processResult, Configuration $configuration): void
{
$errors = $processResult->getErrors();

// only show 100% when no errors
if ($errors === [] && $configuration->shouldShowProgressBar()) {
$this->rectorOutputStyle->progressFinish();
}

// show diff after progress bar
if ($configuration->shouldShowDiffs()) {
$this->reportFileDiffs($processResult->getFileDiffs());
}

$this->reportErrors($errors);
$this->reportErrors($processResult->getErrors());

if ($errors !== []) {
if ($processResult->getErrors() !== []) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/Parallel/WorkerRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
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\Application\ApplicationFileProcessor;
use Rector\Core\Console\Style\RectorConsoleOutputStyle;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Provider\CurrentFileProvider;
Expand Down Expand Up @@ -39,7 +39,7 @@ public function __construct(
private readonly CurrentFileProvider $currentFileProvider,
private readonly DynamicSourceLocatorDecorator $dynamicSourceLocatorDecorator,
private readonly RectorConsoleOutputStyle $rectorConsoleOutputStyle,
private readonly ApplicationFileProcessor $applicationFileProcessor,
private readonly NodeScopeResolver $nodeScopeResolver,
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly iterable $fileProcessors = [],
) {
Expand Down Expand Up @@ -82,7 +82,7 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura
$systemErrors = [];

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

foreach ($filePaths as $filePath) {
$file = null;
Expand Down
13 changes: 6 additions & 7 deletions src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ public function run(Configuration $configuration, InputInterface $input): array

$this->configureCustomErrorHandler();

$filePaths = $this->resolveFilePathsByConfigurationFileExtensions($filePaths, $configuration);

if ($configuration->isParallel()) {
$systemErrorsAndFileDiffs = $this->runParallel($filePaths, $configuration, $input);
} else {
// 1. PHPStan has to know about all files too
$filePaths = $this->configurePHPStanNodeScopeResolver($filePaths, $configuration);
// 1. allow PHPStan to work with static reflection on provided files
$this->nodeScopeResolver->setAnalysedFiles($filePaths);

// 2. collect all files from files+dirs provided filtered paths
$files = $this->fileFactory->createFromPaths($filePaths);
Expand Down Expand Up @@ -149,18 +151,15 @@ public function processFiles(array $files, Configuration $configuration): array
* @param string[] $filePaths
* @return string[]
*/
public function configurePHPStanNodeScopeResolver(array $filePaths, Configuration $configuration): array
public function resolveFilePathsByConfigurationFileExtensions(array $filePaths, Configuration $configuration): array
{
$fileExtensions = $configuration->getFileExtensions();
$fileWithExtensionsFilter = static function (string $filePath) use ($fileExtensions): bool {
$filePathExtension = pathinfo($filePath, PATHINFO_EXTENSION);
return in_array($filePathExtension, $fileExtensions, true);
};

$filePaths = array_filter($filePaths, $fileWithExtensionsFilter);
$this->nodeScopeResolver->setAnalysedFiles($filePaths);

return $filePaths;
return array_filter($filePaths, $fileWithExtensionsFilter);
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/Console/Output/RectorOutputStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ public function progressAdvance(int $step = 1): void
$this->rectorConsoleOutputStyle->progressAdvance($step);
}

public function progressFinish(): void
{
$this->rectorConsoleOutputStyle->progressFinish();
}

public function error(string $message): void
{
$this->rectorConsoleOutputStyle->error($message);
Expand Down
11 changes: 0 additions & 11 deletions src/Console/Style/RectorConsoleOutputStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,4 @@ private function getProgressBar(): ProgressBar
{
return $this->progressBar ?? throw new RuntimeException('The ProgressBar is not started.');
}

public function progressFinish(): void
{
// hide progress bar in tests
if (defined('PHPUNIT_COMPOSER_INSTALL')) {
return;
}

$progressBar = $this->getProgressBar();
$progressBar->finish();
}
}
2 changes: 0 additions & 2 deletions src/Contract/Console/OutputStyleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,4 @@ public function setVerbosity(int $level): void;
public function progressStart(int $fileCount): void;

public function progressAdvance(int $step = 1): void;

public function progressFinish(): void;
}

0 comments on commit e77f6d2

Please sign in to comment.