-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathFilesystemTweaker.php
More file actions
61 lines (51 loc) · 1.47 KB
/
FilesystemTweaker.php
File metadata and controls
61 lines (51 loc) · 1.47 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
<?php
declare(strict_types=1);
namespace Rector\FileSystem;
final class FilesystemTweaker
{
/**
* This will turn paths like "src/Symfony/Component/*\/Tests" to existing directory paths
*
* @param string[] $paths
*
* @return string[]
*/
public function resolveWithFnmatch(array $paths): array
{
$absolutePathsFound = [];
foreach ($paths as $path) {
if (\str_contains($path, '*')) {
$foundPaths = $this->foundInGlob($path);
$absolutePathsFound = $this->appendPaths($foundPaths, $absolutePathsFound);
} else {
$absolutePathsFound = $this->appendPaths([$path], $absolutePathsFound);
}
}
return $absolutePathsFound;
}
/**
* @param string[] $foundPaths
* @param string[] $absolutePathsFound
* @return string[]
*/
private function appendPaths(array $foundPaths, array $absolutePathsFound): array
{
foreach ($foundPaths as $foundPath) {
$foundPath = realpath($foundPath);
if ($foundPath === false) {
continue;
}
$absolutePathsFound[] = $foundPath;
}
return $absolutePathsFound;
}
/**
* @return string[]
*/
private function foundInGlob(string $path): array
{
/** @var string[] $paths */
$paths = (array) glob($path);
return array_filter($paths, file_exists(...));
}
}