Skip to content

Commit ee8d9b0

Browse files
committed
add nested path support to unmatched skip
1 parent 3da7c72 commit ee8d9b0

4 files changed

Lines changed: 57 additions & 27 deletions

File tree

‎src/Reporting/MissConfigurationReporter.php‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,24 @@ public function reportUnusedSkips(ProcessResult $processResult): void
3636
return;
3737
}
3838

39-
// only path-scoped class skips are trackable at runtime; skip-everywhere rule skips
39+
// only concrete path-scoped skips are trackable at runtime; skip-everywhere rule skips
4040
// (null path) are forgotten from the container at boot, so they never reach the skipper
41-
$pathScopedClassSkips = array_keys(array_filter(
42-
$this->skippedClassResolver->resolve(),
43-
static fn (?array $paths): bool => $paths !== null
44-
));
41+
$skippedClassPaths = [];
42+
foreach ($this->skippedClassResolver->resolve() as $paths) {
43+
if ($paths === null) {
44+
continue;
45+
}
46+
47+
$skippedClassPaths = [...$skippedClassPaths, ...$paths];
48+
}
4549

46-
$configuredSkips = [...$pathScopedClassSkips, ...$this->skippedPathsResolver->resolve()];
50+
$configuredSkips = [...$skippedClassPaths, ...$this->skippedPathsResolver->resolve()];
51+
52+
// skip mask paths like "*/some/*"; they are hard to spot and report false positives
53+
$configuredSkips = array_filter(
54+
$configuredSkips,
55+
static fn (string $skip): bool => ! str_contains($skip, '*')
56+
);
4757

4858
$unusedSkips = array_values(array_diff($configuredSkips, $processResult->getUsedSkips()));
4959
if ($unusedSkips === []) {

‎src/Skipper/Matcher/FileInfoMatcher.php‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,26 @@ public function __construct(
2323
*/
2424
public function doesFileInfoMatchPatterns(string $filePath, array $filePatterns): bool
2525
{
26-
$filePath = PathNormalizer::normalize($filePath);
26+
return $this->matchPattern($filePath, $filePatterns) !== null;
27+
}
28+
29+
/**
30+
* Returns the original (un-normalized) pattern that matched, so callers can report the exact
31+
* configured path. Returns null when no pattern matches.
32+
*
33+
* @param string[] $filePatterns
34+
*/
35+
public function matchPattern(string $filePath, array $filePatterns): ?string
36+
{
37+
$normalizedFilePath = PathNormalizer::normalize($filePath);
2738
foreach ($filePatterns as $filePattern) {
28-
$filePattern = PathNormalizer::normalize($filePattern);
29-
if ($this->doesFileMatchPattern($filePath, $filePattern)) {
30-
return true;
39+
$normalizedFilePattern = PathNormalizer::normalize($filePattern);
40+
if ($this->doesFileMatchPattern($normalizedFilePath, $normalizedFilePattern)) {
41+
return $filePattern;
3142
}
3243
}
3344

34-
return false;
45+
return null;
3546
}
3647

3748
/**

‎src/Skipper/Skipper/SkipSkipper.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ public function doesMatchSkip(object | string $checker, string $filePath, array
3030
return true;
3131
}
3232

33-
if ($this->fileInfoMatcher->doesFileInfoMatchPatterns($filePath, $skippedFiles)) {
34-
$this->usedSkipCollector->markUsed($skippedClass);
33+
// mark the specific matched path used, so unused paths under the same rule are reported
34+
$matchedPath = $this->fileInfoMatcher->matchPattern($filePath, $skippedFiles);
35+
if ($matchedPath !== null) {
36+
$this->usedSkipCollector->markUsed($matchedPath);
3537
return true;
3638
}
3739
}

‎tests/Reporting/MissConfigurationReporterTest.php‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121

2222
final class MissConfigurationReporterTest extends AbstractLazyTestCase
2323
{
24-
private const string UNUSED_GLOB = '*/UnusedGlobMarker/*';
24+
private const string UNUSED_ENTITY_PATH = '/app/bundles/UserBundle/Entity';
25+
26+
private const string USED_ENTITY_PATH = '/app/bundles/AdminBundle/Entity';
27+
28+
private const string MASK_PATH = '*/SomeMask/*';
2529

2630
private BufferedOutput $bufferedOutput;
2731

@@ -44,12 +48,12 @@ protected function setUp(): void
4448
SimpleParameterProvider::setParameter(Option::SKIP, [
4549
// skip-everywhere rule skip - forgotten at boot, not trackable, must be excluded
4650
ThreeMan::class,
47-
// path-scoped class skip, never matched - must be reported
48-
FifthElement::class => ['*/some/*'],
49-
// path-scoped class skip, matched - must not be reported
50-
AnotherClassToSkip::class => ['*/other/*'],
51-
// glob path skip, never matched - must be reported
52-
self::UNUSED_GLOB,
51+
// concrete path-scoped class skip, never matched - must be reported
52+
FifthElement::class => [self::UNUSED_ENTITY_PATH],
53+
// concrete path-scoped class skip, matched - must not be reported
54+
AnotherClassToSkip::class => [self::USED_ENTITY_PATH],
55+
// mask path skip - hard to spot, false-positive prone, must not be reported
56+
self::MASK_PATH,
5357
]);
5458
}
5559

@@ -63,20 +67,23 @@ public function testReportsOnlyTrackableUnusedSkips(): void
6367
{
6468
SimpleParameterProvider::setParameter(Option::REPORT_UNUSED_SKIPS, true);
6569

66-
$processResult = new ProcessResult([], [], 0, [AnotherClassToSkip::class]);
70+
// matched path is marked used by its concrete path, not by the rule class
71+
$processResult = new ProcessResult([], [], 0, [self::USED_ENTITY_PATH]);
6772
$this->missConfigurationReporter->reportUnusedSkips($processResult);
6873

6974
$output = $this->bufferedOutput->fetch();
7075

71-
// unused, trackable skips are reported
72-
$this->assertStringContainsString('FifthElement', $output);
73-
$this->assertStringContainsString('UnusedGlobMarker', $output);
76+
// unused concrete path is reported by path, not rule class
77+
$this->assertStringContainsString('UserBundle', $output);
78+
79+
// matched concrete path is excluded
80+
$this->assertStringNotContainsString('AdminBundle', $output);
81+
82+
// mask path is excluded (hard to spot, false-positive prone)
83+
$this->assertStringNotContainsString('SomeMask', $output);
7484

7585
// skip-everywhere rule skip is excluded (not trackable at runtime)
7686
$this->assertStringNotContainsString('ThreeMan', $output);
77-
78-
// matched skip is excluded
79-
$this->assertStringNotContainsString('AnotherClassToSkip', $output);
8087
}
8188

8289
public function testReportsNothingWhenDisabled(): void

0 commit comments

Comments
 (0)