-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFileFinder.php
More file actions
94 lines (76 loc) · 3.14 KB
/
Copy pathFileFinder.php
File metadata and controls
94 lines (76 loc) · 3.14 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
declare(strict_types=1);
namespace Pest\Mutate\Support;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
class FileFinder
{
/**
* @param array<int, string> $paths
* @param array<int, string> $pathsToIgnore
*/
public static function files(array $paths, array $pathsToIgnore): Finder
{
['directories' => $dirs, 'files' => $files] = self::separateDirectoriesAndFiles($paths);
$allPathsToIgnore = self::buildPathsToIgnore($pathsToIgnore, $dirs);
return Finder::create()
->in($dirs)
->name('*.php')
->append($files)
->files()
->filter(fn (SplFileInfo $file): bool => array_filter($allPathsToIgnore, fn (string $pathToIgnore): bool => preg_match($pathToIgnore, $file->getRealPath()) === 1) === []);
}
/**
* @param array<int, string> $paths
* @return array{directories: array<int, string>, files: array<int, SplFileInfo>}
*/
private static function separateDirectoriesAndFiles(array $paths): array
{
$dirs = [];
$files = [];
foreach ($paths as $path) {
if (! str_starts_with($path, DIRECTORY_SEPARATOR)) {
$path = getcwd().DIRECTORY_SEPARATOR.$path;
}
if (str_contains($path, '*')) {
$expanded = glob($path, GLOB_ONLYDIR);
if ($expanded !== false) {
$dirs = [...$dirs, ...$expanded];
}
continue;
}
if (is_dir($path)) {
$dirs[] = $path;
} elseif (is_file($path)) {
$file = new \SplFileInfo($path);
$files[] = new SplFileInfo($file->getPathname(), $file->getPath(), $file->getFilename());
}
}
return ['directories' => $dirs, 'files' => $files];
}
/**
* @param array<int, string> $pathsToIgnore
* @param array<int, string> $dirs
* @return array<int, string>
*/
private static function buildPathsToIgnore(array $pathsToIgnore, array $dirs): array
{
$allPathsToIgnore = [];
foreach ($pathsToIgnore as $pathToIgnore) {
if (! str_starts_with($pathToIgnore, DIRECTORY_SEPARATOR)) {
foreach ($dirs as $dir) {
$allPathsToIgnore[] = $dir.DIRECTORY_SEPARATOR.$pathToIgnore;
}
}
$allPathsToIgnore[] = (str_starts_with($pathToIgnore, getcwd()) ? '' : getcwd()).DIRECTORY_SEPARATOR.ltrim($pathToIgnore, DIRECTORY_SEPARATOR); // @phpstan-ignore-line
}
return array_map(function (string $pathToIgnore): string {
if (! str_ends_with($pathToIgnore, '.php') && ! str_ends_with($pathToIgnore, DIRECTORY_SEPARATOR) && ! str_ends_with($pathToIgnore, '*')) {
$pathToIgnore .= DIRECTORY_SEPARATOR;
}
$pattern = '/^'.preg_quote($pathToIgnore, '/').'/';
$pattern = str_replace('\*\*', '.*', $pattern);
return str_replace('\*', '[^'.preg_quote(DIRECTORY_SEPARATOR, '/').']*', $pattern);
}, $allPathsToIgnore);
}
}