Repository navigation
Expand file tree
/
Copy pathFileInfoMatcher.php
More file actions
77 lines (64 loc) · 2.19 KB
/
Copy pathFileInfoMatcher.php
File metadata and controls
77 lines (64 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types=1);
namespace Rector\Skipper\Matcher;
use Rector\Skipper\FileSystem\FnMatchPathNormalizer;
use Rector\Skipper\FileSystem\PathNormalizer;
use Rector\Skipper\Fnmatcher;
use Rector\Skipper\RealpathMatcher;
final readonly class FileInfoMatcher
{
public function __construct(
private FnMatchPathNormalizer $fnMatchPathNormalizer,
private Fnmatcher $fnmatcher,
private RealpathMatcher $realpathMatcher
) {
}
/**
* @param string[] $filePatterns
*/
public function doesFileInfoMatchPatterns(string $filePath, array $filePatterns): bool
{
return $this->matchPattern($filePath, $filePatterns) !== null;
}
/**
* Returns the original (un-normalized) pattern that matched, so callers can report the exact
* configured path. Returns null when no pattern matches.
*
* @param string[] $filePatterns
*/
public function matchPattern(string $filePath, array $filePatterns): ?string
{
$normalizedFilePath = PathNormalizer::normalize($filePath);
foreach ($filePatterns as $filePattern) {
$normalizedFilePattern = PathNormalizer::normalize($filePattern);
if ($this->doesFileMatchPattern($normalizedFilePath, $normalizedFilePattern)) {
return $filePattern;
}
}
return null;
}
/**
* Supports both relative and absolute $file path. They differ for PHP-CS-Fixer and PHP_CodeSniffer.
*/
private function doesFileMatchPattern(string $filePath, string $ignoredPath): bool
{
// in rector.php, the path can be absolute
if ($filePath === $ignoredPath) {
return true;
}
$ignoredPath = $this->fnMatchPathNormalizer->normalizeForFnmatch($ignoredPath);
if ($ignoredPath === '') {
return false;
}
if (str_starts_with($filePath, $ignoredPath)) {
return true;
}
if (str_ends_with($filePath, $ignoredPath)) {
return true;
}
if ($this->fnmatcher->match($ignoredPath, $filePath)) {
return true;
}
return $this->realpathMatcher->match($ignoredPath, $filePath);
}
}