RecursiveFileListing   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 55
rs 10
c 0
b 0
f 0
ccs 33
cts 33
cp 1
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addFilter() 0 2 1
A clearFilters() 0 2 1
B recursiveScan() 0 9 7
A scan() 0 5 1
A __construct() 0 6 2
A isFileOk() 0 12 4
1
<?php
2
3
namespace CSFCloud;
4
5
use Exception;
6
7
class RecursiveFileListing {
8
9
    private $root_dir;
10
    private $files;
11
    private $filters = [];
12
13 12
    public function __construct(string $dir) {
14 12
        if (!is_dir($dir)) {
15 3
            throw new Exception("Directory required");
16
        }
17
18 9
        $this->root_dir = $dir;
19 9
    }
20
21 6
    public function addFilter(string $regex) {
22 6
        $this->filters[] = $regex;
23 6
    }
24
25 3
    public function clearFilters() {
26 3
        $this->filters = [];
27 3
    }
28
29 9
    public function scan() : array {
30 9
        $this->files = [];
31 9
        $this->recursiveScan($this->root_dir);
32 9
        sort($this->files);
33 9
        return $this->files;
34
    }
35
36 9
    private function recursiveScan(string $dir) {
37 9
        $files = scandir($dir);
38 9
        foreach ($files as $filename) {
39 9
            if ($filename !== "." && $filename !== "..") {
40 9
                $file_path = $dir . "/" . $filename;
41 9
                if (is_file($file_path) && $this->isFileOk($file_path)) {
42 9
                    $this->files[] = $file_path;
43 9
                } else if (is_dir($file_path)) {
44 9
                    $this->recursiveScan($file_path);
45
                }
46
            }
47
        }
48 9
    }
49
50 9
    private function isFileOk(string $path) : bool {
51 9
        if (count($this->filters) == 0) {
52 6
            return true;
53
        }
54
55 3
        foreach ($this->filters as $filter) {
56 3
            if (preg_match($filter, $path)) {
57 3
                return true;
58
            }
59
        }
60
61 3
        return false;
62
    }
63
64
}
65