Passed
Pull Request — master (#74)
by Peter
07:08
created

FilesPathService::getPathToFiles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.9666
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace YamlStandards\Command\Service;
6
7
class FilesPathService
8
{
9
    /**
10
     * @param string[] $patterns
11
     * @return string[]
12
     */
13 14
    public static function getPathToFiles(array $patterns): array
14
    {
15 14
        $pathToFiles = [[]];
16 14
        foreach ($patterns as $pattern) {
17 14
            if (is_file($pattern)) {
18 10
                $pathToFiles[] = [$pattern];
19
            } else {
20 12
                $pathToFiles[] = self::globRecursive($pattern);
21
            }
22
        }
23
24 14
        $pathToFiles = array_merge(...$pathToFiles);
25 14
        $pathToFiles = str_replace('\\', '/', $pathToFiles);
26
27 14
        return array_unique($pathToFiles);
28
    }
29
30
    /**
31
     * @param string $pattern
32
     * @return string[]
33
     *
34
     * @link https://www.php.net/manual/en/function.glob.php#106595
35
     */
36 12
    private static function globRecursive(string $pattern): array
37
    {
38 12
        $pathNames = glob($pattern, GLOB_BRACE);
39 12
        $files = array_filter($pathNames, 'is_file');
40
41 12
        foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
42
            $files = array_merge($files, self::globRecursive($dir . '/' . basename($pattern)));
43
        }
44
45 12
        return $files;
46
    }
47
}
48