-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathBaselineFinder.php
More file actions
75 lines (63 loc) · 2 KB
/
Copy pathBaselineFinder.php
File metadata and controls
75 lines (63 loc) · 2 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
<?php
namespace staabm\PHPStanBaselineAnalysis;
final class BaselineFinder
{
/**
* @param string[] $excludeFilenames
* @return Baseline[]
*/
static public function forGlob(string $glob, array $excludeFilenames = []): array
{
$baselines = [];
foreach (self::rglob($glob, 0, $excludeFilenames) as $baseline) {
if (!is_file($baseline)) {
continue;
}
if (!str_ends_with($baseline, '.neon') && !str_ends_with($baseline, '.php')) {
continue;
}
$baselines[] = Baseline::forFile($baseline);
}
return $baselines;
}
/**
* from https://stackoverflow.com/a/17161106
*
* @param string[] $excludeFilenames
* @return string[]
*/
static private function rglob(string $pattern, int $flags = 0, array $excludeFilenames = []): array
{
$files = glob($pattern, $flags);
if (!$files) {
return [];
}
// Filter out excluded filenames
if ($excludeFilenames !== []) {
$files = self::filerExcludedFiles($files, $excludeFilenames);
}
foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) ?: [] as $dir) {
if (basename($dir) == 'vendor') {
continue;
}
$files = array_merge($files, self::rglob($dir . '/' . basename($pattern), $flags, $excludeFilenames));
}
return $files;
}
/**
* @param string[] $files
* @param string[] $excludeFilenames
* @return string[]
*/
private static function filerExcludedFiles(array $files, array $excludeFilenames): array
{
return array_filter($files, function (string $file) use ($excludeFilenames) {
foreach ($excludeFilenames as $excludeFilename) {
if (str_ends_with($file, $excludeFilename)) {
return false;
}
}
return true;
});
}
}