Completed
Push — master ( c132c8...42afef )
by Dmitry
06:08
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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
ccs 12
cts 13
cp 0.9231
rs 6.7272
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
10 10
    function __construct(Application $app, $root)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
11
    {
12 10
        $this->app = $app;
13 10
        $this->root = $root;
14 10
    }
15
16 8
    function exists()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
17
    {
18 8
        $path = call_user_func_array([$this, 'getPath'], func_get_args());
19 8
        return is_dir($path) || file_exists($path);
20
    }
21
22 8
    function getPath()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
    {
24 8
        if (func_get_args()) {
25 8
            $chain = func_get_args();
26 8
            array_unshift($chain, $this->root);
27 8
            foreach ($chain as $k => $v) {
28 8
                if (!strlen($v)) {
29 8
                    unset($chain[$k]);
30
                }
31
            }
32 8
            return implode(DIRECTORY_SEPARATOR, array_values($chain));
33
        }
34
35 8
        return $this->root;
36
    }
37
38 1
    function listClasses($namespace = '')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39
    {
40 1
        $location = "src/php";
41
42 1
        if($namespace) {
43 1
            $location .= '/'.str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
44
        }
45
46 1
        $files = $this->listFiles($location);
47 1
        $classes = [];
48
49 1
        $namespace = $this->completeClassName($namespace);
50
51 1
        foreach($files as $file) {
52 1
            $class = str_replace(['\\', '/'], '\\', $file);
53 1
            $class = substr($class, 0, -4);
54 1
            if($namespace) {
55 1
                $class = $namespace.'\\'.$class;
56
            }
57 1
            $classes[] = $class;
58
        }
59
60 1
        return $classes;
61
    }
62
63 7
    function listFiles($location)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
64
    {
65 7
        $absolute = $this->getPath($location);
66 7
        if (!is_dir($absolute)) {
67
            return [];
68
        }
69
70 7
        $result = [];
71 7
        $relative = substr($absolute, strlen($this->getPath()));
72 7
        foreach (scandir($absolute) as $file) {
73 7
            if ($file != '.' && $file != '..') {
74 7
                if (is_file("$absolute/$file")) {
75 7
                    $result[] = $file;
76
                } else {
77 2
                    foreach ($this->listFiles("$relative/$file") as $child) {
78 7
                        $result[] = "$file/$child";
79
                    }
80
                }
81
            }
82
        }
83
84 7
        return $result;
85
    }
86
87 5
    function completeClassName($namespace)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
88
    {
89 5
        $config = $this->app->get(Config::class);
90 5
        if($config['app.namespace']) {
91 5
            $namespace = $config['app.namespace'].($namespace ? '\\'.$namespace:'');
92
        }
93 5
        return $namespace;
94
    }
95
96
}
97