Completed
Push — master ( 692570...6b6f8f )
by Dmitry
03:35
created

Filesystem::listFiles()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.0222

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 12
cts 13
cp 0.9231
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 14
nc 5
nop 1
crap 7.0222
1
<?php
2
3
namespace Basis;
4
5
class Filesystem
6
{
7
    private $app;
8
    protected $root;
9
    protected $namespace;
10
11 14
    public function __construct(Application $app, $root)
12
    {
13 14
        $this->app = $app;
14 14
        $this->root = $root;
15 14
    }
16
17 1
    public function exists(string $path) : bool
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
18
    {
19 1
        $path = call_user_func_array([$this, 'getPath'], func_get_args());
20 1
        return is_dir($path) || file_exists($path);
21
    }
22
23 14
    public function getPath(string $path = null) : string
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
    {
25 14
        if (func_get_args()) {
26 14
            $chain = func_get_args();
27 14
            array_unshift($chain, $this->root);
28 14
            foreach ($chain as $k => $v) {
29 14
                if (!strlen($v)) {
30 14
                    unset($chain[$k]);
31
                }
32
            }
33 14
            return implode(DIRECTORY_SEPARATOR, array_values($chain));
34
        }
35
36 14
        return $this->root;
37
    }
38
39 14
    public function listClasses(string $namespace = '', string $location = 'php') : array
40
    {
41 14
        if ($namespace) {
42 14
            $location .= '/'.str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
43
        }
44
45 14
        $files = $this->listFiles($location);
46 14
        $classes = [];
47
48 14
        $namespace = $this->completeClassName($namespace);
49
50 14
        foreach ($files as $file) {
51 14
            $class = str_replace(['\\', '/'], '\\', $file);
52 14
            $class = substr($class, 0, -4);
53 14
            if ($namespace) {
54 14
                $class = $namespace.'\\'.$class;
55
            }
56 14
            $classes[] = $class;
57
        }
58
59 14
        return $classes;
60
    }
61
62 14
    public function listFiles(string $location) : array
63
    {
64 14
        $absolute = $this->getPath($location);
65 14
        if (!is_dir($absolute)) {
66
            return [];
67
        }
68
69 14
        $result = [];
70 14
        $relative = substr($absolute, strlen($this->getPath()));
71 14
        foreach (scandir($absolute) as $file) {
72 14
            if ($file != '.' && $file != '..') {
73 14
                if (is_file("$absolute/$file")) {
74 14
                    $result[] = $file;
75
                } else {
76 7
                    foreach ($this->listFiles("$relative/$file") as $child) {
77 14
                        $result[] = "$file/$child";
78
                    }
79
                }
80
            }
81
        }
82
83 14
        return $result;
84
    }
85
86 14
    public function completeClassName(string $classname) : string
87
    {
88 14
        if ($this->namespace && $classname) {
89 7
            return $this->namespace.'\\'.$classname;
90
        }
91 14
        return $classname;
92
    }
93
}
94