Passed
Push — master ( 7d1598...ca2272 )
by Allan
08:59 queued 06:38
created

FileSystemUtils   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 23
c 5
b 0
f 0
dl 0
loc 44
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A collectFilePathsRecursively() 0 6 1
A collectPathsRecursively() 0 33 3
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Utils;
7
8
class FileSystemUtils
9
{
10
    public function collectFilePathsRecursively($rootPath, $pattern)
11
    {
12
        $paths = $this->collectPathsRecursively($rootPath, $pattern);
13
14
        return array_filter($paths, function ($item) {
15
            return is_file($item);
16
        });
17
    }
18
19
    public function collectPathsRecursively($rootPath, $pattern)
20
    {
21
        if (!is_dir($rootPath)) {
22
            return array();
23
        }
24
        
25
        $directoryIterator = new \RecursiveDirectoryIterator(
26
            $rootPath,
27
            \RecursiveDirectoryIterator::FOLLOW_SYMLINKS
28
        );
29
        
30
        $recursiveIterator = new \RecursiveIteratorIterator($directoryIterator);
31
32
        $filesIterator = new \RegexIterator(
33
            $recursiveIterator,
34
            $pattern,
35
            \RecursiveRegexIterator::GET_MATCH
36
        );
37
38
        $files = array();
39
40
        foreach ($filesIterator as $info) {
41
            $path = reset($info);
42
            $files[substr($path, strlen($rootPath) + 1)] = $path;
43
        }
44
45
        $sequence = array_keys($files);
46
        
47
        natsort($sequence);
48
49
        return array_replace(
50
            array_flip($sequence),
51
            $files
52
        );
53
    }
54
}
55