Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,13 @@ public function processFiles(
}
}

return new ProcessResult($systemErrors, $fileDiffs, $totalChanged, $this->skipper->provideUsedSkips());
return new ProcessResult(
$systemErrors,
$fileDiffs,
$totalChanged,
$this->skipper->provideUsedSkips(),
$this->skipper->provideSkippedFilePathsByRule()
);
}

private function processFile(File $file, Configuration $configuration): FileProcessResult
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->missConfigurationReporter->reportUnusedSkips($processResult);

// the verification re-run is forced to a dry run, so it is safe on an apply run too
$this->missConfigurationReporter->reportUnnecessarySkips();
$this->missConfigurationReporter->reportUnnecessarySkips($processResult);

return $this->resolveReturnCode($processResult, $configuration);
}
Expand Down
1 change: 1 addition & 0 deletions src/Console/Command/WorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ private function runWorker(
Bridge::SYSTEM_ERRORS_COUNT => count($processResult->getSystemErrors()),
Bridge::TOTAL_CHANGED => $processResult->getTotalChanged(),
Bridge::USED_SKIPS => $processResult->getUsedSkips(),
Bridge::SKIPPED_FILE_PATHS_BY_RULE => $processResult->getSkippedFilePathsByRule(),
],
]);
});
Expand Down
3 changes: 2 additions & 1 deletion src/Parallel/Application/ParallelFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ function (array $json) use (
* file_diffs: array<string, mixed>,
* files_count: int,
* system_errors_count: int,
* used_skips: array<string, string[]>
* used_skips: array<string, string[]>,
* skipped_file_paths_by_rule: array<string, string[]>
* } $json */
$filesCount = $parallelResultCollector->collectWorkerResult($json);
$postFileCallback($filesCount);
Expand Down
31 changes: 29 additions & 2 deletions src/Parallel/Application/ParallelResultCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* who gets which files.
*
* Holds mutable per-run state - create one per process() call, never share it.
* @see \Rector\Tests\Parallel\Application\ParallelResultCollectorTest
*/
final class ParallelResultCollector
{
Expand All @@ -34,6 +35,13 @@ final class ParallelResultCollector
*/
private array $usedSkips = [];

/**
* Paths are kept as keys while collecting, to deduplicate them per rule
*
* @var array<string, array<string, true>>
*/
private array $skippedFilePathsByRule = [];

private int $totalChanged = 0;

/**
Expand All @@ -43,7 +51,8 @@ final class ParallelResultCollector
* file_diffs: array<string, mixed>,
* files_count: int,
* system_errors_count: int,
* used_skips: array<string, string[]>
* used_skips: array<string, string[]>,
* skipped_file_paths_by_rule: array<string, string[]>
* } $json
* @return int files covered by this result, to advance the progress bar
*/
Expand All @@ -58,6 +67,13 @@ public function collectWorkerResult(array $json): int
}
}

foreach ($json[Bridge::SKIPPED_FILE_PATHS_BY_RULE] as $rectorClass => $filePaths) {
$this->skippedFilePathsByRule[$rectorClass] ??= [];
foreach ($filePaths as $filePath) {
$this->skippedFilePathsByRule[$rectorClass][$filePath] = true;
}
}

// decode arrays to objects
foreach ($json[Bridge::SYSTEM_ERRORS] as $jsonError) {
if (is_string($jsonError)) {
Expand Down Expand Up @@ -87,6 +103,17 @@ public function createProcessResult(): ProcessResult
$mergedUsedSkips[$skip] = array_keys($paths);
}

return new ProcessResult($this->systemErrors, $this->fileDiffs, $this->totalChanged, $mergedUsedSkips);
$mergedSkippedFilePathsByRule = [];
foreach ($this->skippedFilePathsByRule as $rectorClass => $filePaths) {
$mergedSkippedFilePathsByRule[$rectorClass] = array_keys($filePaths);
}

return new ProcessResult(
$this->systemErrors,
$this->fileDiffs,
$this->totalChanged,
$mergedUsedSkips,
$mergedSkippedFilePathsByRule
);
}
}
2 changes: 2 additions & 0 deletions src/Parallel/ValueObject/Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ final class Bridge
public const string TOTAL_CHANGED = 'total_changed';

public const string USED_SKIPS = 'used_skips';

public const string SKIPPED_FILE_PATHS_BY_RULE = 'skipped_file_paths_by_rule';
}
4 changes: 2 additions & 2 deletions src/Reporting/MissConfigurationReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function reportUnusedSkips(ProcessResult $processResult): void
* skip is unnecessary. Enabled with "->reportUnusedSkips()". The re-run is forced to a dry run,
* so it is safe on an apply run too.
*/
public function reportUnnecessarySkips(): void
public function reportUnnecessarySkips(ProcessResult $processResult): void
{
$unnecessarySkips = $this->neededSkipVerifier->verify();
$unnecessarySkips = $this->neededSkipVerifier->verify($processResult);
if ($unnecessarySkips === []) {
return;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Reporting/NeededSkipVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Rector\Skipper\Skipper\Skipper;
use Rector\ValueObject\Application\File;
use Rector\ValueObject\Configuration;
use Rector\ValueObject\ProcessResult;

/**
* Re-runs each rule-scoped skip on its exact file with skipping off. A skip whose rule still does
Expand All @@ -34,9 +35,10 @@ public function __construct(
*
* @return string[]
*/
public function verify(): array
public function verify(ProcessResult $processResult): array
{
$skippedFilePathsByRule = $this->skipper->provideSkippedFilePathsByRule();
// collected in the process that handled the files - the workers in a parallel run
$skippedFilePathsByRule = $processResult->getSkippedFilePathsByRule();
if ($skippedFilePathsByRule === []) {
return [];
}
Expand Down Expand Up @@ -72,7 +74,7 @@ public function verify(): array
}

/**
* @param array<class-string, string[]> $skippedFilePathsByRule
* @param array<string, string[]> $skippedFilePathsByRule
* @return array<string, string[]> real file path => rule classes that changed it
*/
private function resolveChangedRulesByFilePath(array $skippedFilePathsByRule): array
Expand Down
19 changes: 18 additions & 1 deletion src/ValueObject/ProcessResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ final class ProcessResult
* @param SystemError[] $systemErrors
* @param FileDiff[] $fileDiffs
* @param array<string, string[]> $usedSkips
* @param array<string, string[]> $skippedFilePathsByRule
*/
public function __construct(
private array $systemErrors,
private readonly array $fileDiffs,
private readonly int $totalChanged,
private array $usedSkips = []
private array $usedSkips = [],
private readonly array $skippedFilePathsByRule = []
) {
Assert::allIsInstanceOf($systemErrors, SystemError::class);
Assert::allIsInstanceOf($fileDiffs, FileDiff::class);
Expand All @@ -29,6 +31,11 @@ public function __construct(
foreach ($usedSkips as $usedSkip) {
Assert::allString($usedSkip);
}

Assert::allString(array_keys($skippedFilePathsByRule));
foreach ($skippedFilePathsByRule as $skippedFilePaths) {
Assert::allString($skippedFilePaths);
}
}

/**
Expand Down Expand Up @@ -91,6 +98,16 @@ public function getUsedSkips(): array
return $this->usedSkips;
}

/**
* Real file paths skipped per rule-scoped path skip, to verify later whether the skip is needed
*
* @return array<string, string[]>
*/
public function getSkippedFilePathsByRule(): array
{
return $this->skippedFilePathsByRule;
}

/**
* @return array<class-string<RectorInterface>, int>
*/
Expand Down
59 changes: 59 additions & 0 deletions tests/Parallel/Application/ParallelResultCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Parallel\Application;

use PHPUnit\Framework\TestCase;
use Rector\Parallel\Application\ParallelResultCollector;
use Rector\Parallel\ValueObject\Bridge;

final class ParallelResultCollectorTest extends TestCase
{
public function testMergesSkippedFilePathsByRuleFromWorkers(): void
{
$parallelResultCollector = new ParallelResultCollector();

$parallelResultCollector->collectWorkerResult($this->createWorkerResult([
'SomeRector' => ['/src/first.php', '/src/second.php'],
]));

// a worker reports its whole cumulative state, so the same path can arrive twice
$parallelResultCollector->collectWorkerResult($this->createWorkerResult([
'SomeRector' => ['/src/second.php', '/src/third.php'],
'AnotherRector' => ['/src/first.php'],
]));

$processResult = $parallelResultCollector->createProcessResult();

$this->assertSame([
'SomeRector' => ['/src/first.php', '/src/second.php', '/src/third.php'],
'AnotherRector' => ['/src/first.php'],
], $processResult->getSkippedFilePathsByRule());
}

/**
* @param array<string, string[]> $skippedFilePathsByRule
* @return array{
* total_changed: int,
* system_errors: mixed[],
* file_diffs: array<string, mixed>,
* files_count: int,
* system_errors_count: int,
* used_skips: array<string, string[]>,
* skipped_file_paths_by_rule: array<string, string[]>
* }
*/
private function createWorkerResult(array $skippedFilePathsByRule): array
{
return [
Bridge::TOTAL_CHANGED => 0,
Bridge::SYSTEM_ERRORS => [],
Bridge::FILE_DIFFS => [],
Bridge::FILES_COUNT => 0,
Bridge::SYSTEM_ERRORS_COUNT => 0,
Bridge::USED_SKIPS => [],
Bridge::SKIPPED_FILE_PATHS_BY_RULE => $skippedFilePathsByRule,
];
}
}
32 changes: 30 additions & 2 deletions tests/Reporting/NeededSkipVerifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\Skipper\Skipper\Skipper;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\Tests\Reporting\Source\MakeClassFinalRector;
use Rector\ValueObject\ProcessResult;

final class NeededSkipVerifierTest extends AbstractLazyTestCase
{
Expand Down Expand Up @@ -56,8 +57,10 @@ public function testReportsSkipThatRuleWouldNotHaveChanged(): void
$this->assertTrue($skipper->shouldSkipRectorAndFile($rule, $neededFilePath));
$this->assertTrue($skipper->shouldSkipRectorAndFile($rule, $unnecessaryFilePath));

$processResult = new ProcessResult([], [], 0, [], $skipper->provideSkippedFilePathsByRule());

$neededSkipVerifier = $rectorConfig->make(NeededSkipVerifier::class);
$unnecessarySkips = $neededSkipVerifier->verify();
$unnecessarySkips = $neededSkipVerifier->verify($processResult);

// one rule group, listing only the unnecessary file
$this->assertCount(1, $unnecessarySkips);
Expand All @@ -69,12 +72,37 @@ public function testReportsSkipThatRuleWouldNotHaveChanged(): void
$this->assertSame('<?php class SomeClass {}', FileSystem::read($neededFilePath));
}

public function testReportsSkipCollectedByWorkers(): void
{
// already final, so the rule changes nothing - skipping it is unnecessary
$unnecessaryFilePath = $this->tempDirectory . '/unnecessary_skip.php';
FileSystem::write($unnecessaryFilePath, '<?php final class AlreadyFinal {}', null);

SimpleParameterProvider::setParameter(Option::REPORT_UNUSED_SKIPS, true);
SimpleParameterProvider::setParameter(Option::SKIP, [
MakeClassFinalRector::class => [$unnecessaryFilePath],
]);

$rectorConfig = $this->createContainerWithRule();

// in a parallel run, the skipped files are captured by workers and only reach the main process via the result
$processResult = new ProcessResult([], [], 0, [], [
MakeClassFinalRector::class => [$unnecessaryFilePath],
]);

$neededSkipVerifier = $rectorConfig->make(NeededSkipVerifier::class);
$unnecessarySkips = $neededSkipVerifier->verify($processResult);

$this->assertCount(1, $unnecessarySkips);
$this->assertStringContainsString('unnecessary_skip.php', $unnecessarySkips[0]);
}

public function testReportsNothingWithoutCapturedSkips(): void
{
$rectorConfig = $this->createContainerWithRule();

$neededSkipVerifier = $rectorConfig->make(NeededSkipVerifier::class);
$this->assertSame([], $neededSkipVerifier->verify());
$this->assertSame([], $neededSkipVerifier->verify(new ProcessResult([], [], 0)));
}

private function createContainerWithRule(): RectorConfig
Expand Down
Loading