FileHelpers   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 33
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A recursiveFileSearch() 0 26 5
1
<?php
2
/**
3
 * Livia
4
 * Copyright 2017-2019 Charlotte Dunois, All Rights Reserved
5
 *
6
 * Website: https://charuru.moe
7
 * License: https://github.com/CharlotteDunois/Livia/blob/master/LICENSE
8
*/
9
10
namespace CharlotteDunois\Livia\Utils;
11
12
/**
13
 * File orientated helpers.
14
 */
15
class FileHelpers {
16
    /**
17
     * Performs a recursive file search in the specified path, using the specified search mask.
18
     * @param string        $path
19
     * @param string|array  $searchmask
20
     * @return string[]
21
     */
22
    static function recursiveFileSearch(string $path, $searchmask = '*') {
23
        $path = \rtrim($path, '/');
24
        
25
        $files = array();
26
        if(is_array($searchmask)) {
27
            $csearchmask = \count($searchmask);
28
            
29
            for($i = 0; $i < $csearchmask; $i++) {
30
                $files = \array_merge($files, \glob($path.'/'.$searchmask[$i]));
31
            }
32
            
33
            \sort($files);
34
        } else {
35
            $files = \glob($path.'/'.$searchmask);
36
        }
37
        
38
        
39
        $dirs = \glob($path.'/*', GLOB_ONLYDIR);
40
        foreach($dirs as $dir) {
41
            if(\is_dir($dir)) {
42
                $files = \array_merge($files, self::recursiveFileSearch($dir, $searchmask));
43
            }
44
        }
45
        
46
        \sort($files);
47
        return $files;
48
    }
49
}
50