Test Failed
Pull Request — master (#74)
by Peter
12:33 queued 05:48
created

FilesPathService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 15
c 0
b 0
f 0
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A globRecursive() 0 10 2
A getPathToFiles() 0 15 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
    public static function getPathToFiles(array $patterns): array
14
    {
15
        $pathToFiles = [[]];
16
        foreach ($patterns as $pattern) {
17
            if (is_file($pattern)) {
18
                $pathToFiles[] = [$pattern];
19
            } else {
20
                $pathToFiles[] = self::globRecursive($pattern);
21
            }
22
        }
23
24
        $pathToFiles = array_merge(...$pathToFiles);
25
        $pathToFiles = str_replace('\\', '/', $pathToFiles);
26
27
        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
    private static function globRecursive(string $pattern): array
37
    {
38
        $pathNames = glob($pattern, GLOB_BRACE);
39
        $files = array_filter($pathNames, 'is_file');
40
41
        foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
42
            $files = array_merge($files, self::globRecursive($dir . '/' . basename($pattern)));
43
        }
44
45
        return $files;
46
    }
47
}
48