Completed
Push — master ( 772acc...7ea003 )
by Dmitry
11:15
created

Filesystem   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.83%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 0
dl 0
loc 89
ccs 45
cts 46
cp 0.9783
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B listClasses() 0 22 4
A completeClassName() 0 7 3
C listFiles() 0 23 7
A exists() 0 5 2
A getPath() 0 15 4
1
<?php
2
3
namespace Basis;
4
5
class Filesystem
6
{
7
    private $app;
8
    protected $root;
9
    protected $namespace;
10
11 11
    public function __construct(Application $app, $root)
12
    {
13 11
        $this->app = $app;
14 11
        $this->root = $root;
15 11
    }
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 11
    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 11
        if (func_get_args()) {
26 11
            $chain = func_get_args();
27 11
            array_unshift($chain, $this->root);
28 11
            foreach ($chain as $k => $v) {
29 11
                if (!strlen($v)) {
30 11
                    unset($chain[$k]);
31
                }
32
            }
33 11
            return implode(DIRECTORY_SEPARATOR, array_values($chain));
34
        }
35
36 11
        return $this->root;
37
    }
38
39 11
    public function listClasses(string $namespace = '', string $location = 'php') : array
40
    {
41 11
        if ($namespace) {
42 11
            $location .= '/'.str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
43
        }
44
45 11
        $files = $this->listFiles($location);
46 11
        $classes = [];
47
48 11
        $namespace = $this->completeClassName($namespace);
49
50 11
        foreach ($files as $file) {
51 11
            $class = str_replace(['\\', '/'], '\\', $file);
52 11
            $class = substr($class, 0, -4);
53 11
            if ($namespace) {
54 11
                $class = $namespace.'\\'.$class;
55
            }
56 11
            $classes[] = $class;
57
        }
58
59 11
        return $classes;
60
    }
61
62 11
    public function listFiles(string $location) : array
63
    {
64 11
        $absolute = $this->getPath($location);
65 11
        if (!is_dir($absolute)) {
66
            return [];
67
        }
68
69 11
        $result = [];
70 11
        $relative = substr($absolute, strlen($this->getPath()));
71 11
        foreach (scandir($absolute) as $file) {
72 11
            if ($file != '.' && $file != '..') {
73 11
                if (is_file("$absolute/$file")) {
74 11
                    $result[] = $file;
75
                } else {
76 6
                    foreach ($this->listFiles("$relative/$file") as $child) {
77 11
                        $result[] = "$file/$child";
78
                    }
79
                }
80
            }
81
        }
82
83 11
        return $result;
84
    }
85
86 11
    public function completeClassName(string $classname) : string
87
    {
88 11
        if ($this->namespace && $classname) {
89 6
            return $this->namespace.'\\'.$classname;
90
        }
91 11
        return $classname;
92
    }
93
}
94