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
9 changes: 8 additions & 1 deletion src/Skipper/Matcher/FileInfoMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
*/
final class FileInfoMatcher
{
/**
* PHP does not cache a failed realpath() lookup, and a wildcard pattern fails it for every file
*
* @var array<string, string|false>
*/
private array $realpathByPattern = [];

/**
* Returns the original (un-normalized) pattern that matched, so callers can report the exact
* configured path. Returns null when no pattern matches.
Expand Down Expand Up @@ -90,7 +97,7 @@ private function matchFnmatch(string $matchingPath, string $filePath): bool

private function matchRealpath(string $matchingPath, string $filePath): bool
{
$realPathMatchingPath = realpath($matchingPath);
$realPathMatchingPath = $this->realpathByPattern[$matchingPath] ??= realpath($matchingPath);
if ($realPathMatchingPath === false) {
return false;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Skipper/Matcher/FileInfoMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ public function testMatchPatternReturnsNullWhenNoPatternMatches(): void
$this->assertNull($matchedPattern);
}

public function testRealPathOfEachPatternIsResolvedSeparatelyOnRepeatedCalls(): void
{
// the file paths are not real paths, so only the realpath() comparison can match them
$pathInPattern = __DIR__ . '/Fixture/path/in';
$inPattern = __DIR__ . '/Fixture/in';
$patterns = [$pathInPattern, $inPattern];

$pathInFile = __DIR__ . '/Fixture/path/with/../in/it/KeepThisFile.txt';
$inFile = __DIR__ . '/Fixture/path/../in/it/KeepThisFile.txt';

for ($i = 0; $i < 2; ++$i) {
$this->assertSame($pathInPattern, $this->fileInfoMatcher->matchPattern($pathInFile, $patterns));
$this->assertSame($inPattern, $this->fileInfoMatcher->matchPattern($inFile, $patterns));
}
}

#[DataProvider('providePatterns')]
public function testPatternNormalization(string $filePath, string $filePattern, bool $shouldMatch): void
{
Expand Down
Loading