GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

NestedRepository::getFilesRecursive()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
ccs 13
cts 14
cp 0.9286
cc 6
eloc 13
nc 5
nop 3
crap 6.0131
1
<?php
2
3
namespace JamesMoss\Flywheel;
4
5
/**
6
 * NestedRepository
7
 */
8
class NestedRepository extends Repository
9
{
10
    const SEPERATOR = '/';
11
12
    /**
13
     * @inherit
14
     */
15 4
    public function __construct($name, Config $config)
16
    {
17 4
        parent::__construct($name, $config);
18
19 4
        $this->deleteEmptyDirs = $config->getOption('delete_empty_dirs') === true;
0 ignored issues
show
Bug introduced by
The property deleteEmptyDirs does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20 4
    }
21
22
    /**
23
     * Get the filesystem path for a document based on it's ID.
24
     *
25
     * @param string $id The ID of the document.
26
     *
27
     * @return string The full filesystem path of the document.
28
     */
29 3
    public function getPathForDocument($id)
30
    {
31 3
        if(!$this->validateId($id)) {
32
            throw new \Exception(sprintf('`%s` is not a valid document ID.', $id));
33
        }
34
35 3
        if($this->isNestedId($id)) {
36 3
            $path = DIRECTORY_SEPARATOR . str_replace(self::SEPERATOR, DIRECTORY_SEPARATOR, dirname($id));
37 3
        } else {
38
            $path = '';
39
        }
40
41 3
        return  $this->path . $path . DIRECTORY_SEPARATOR . $this->getFilename($id);
42
    }
43
44
    /**
45
     * @inherit
46
     */
47 2
    public function delete($id)
48
    {
49 2
        $result = parent::delete($id);
50
51 2
        if ($id instanceof DocumentInterface) {
52
            $id = $id->getId();
53
        }
54
55 2
        if(!$result || !$this->deleteEmptyDirs || !$this->isNestedId($id)) {
56 1
            return $result;
57
        }
58
59 1
        $path = $this->getPathForDocument($id);
60 1
        $dir  = dirname($path);
61
62 1
        if(file_exists($dir) && count(glob($dir . '/*')) === 0) {
63 1
            rmdir($dir);
64 1
        }
65
66 1
        return $result;
67
    }
68
69
    /**
70
     * @inherit
71
     */
72 1
    protected function write($path, $contents)
73
    {
74
        // ensure path exists by making directories beforehand
75 1
        if(!file_exists(dirname($path))) {
76 1
            mkdir(dirname($path), 0777, true);
77 1
        }
78
79 1
        return parent::write($path, $contents);
80
    }
81
82
    /**
83
     * Gets just the filename for a document based on it's ID.
84
     *
85
     * @param string $id The ID of the document.
86
     *
87
     * @return string The filename of the document, including extension.
88
     */
89 3
    public function getFilename($id)
90
    {
91 3
        return basename($id) . '.' . $this->formatter->getFileExtension();
92
    }
93
94
    /**
95
     * Get an array containing the path of all files in this repository
96
     *
97
     * @return array An array, item is a file path.
98
     */
99 1
    public function getAllFiles()
100
    {
101 1
        $ext   = $this->formatter->getFileExtension();
102 1
        $files = array();
103 1
        $this->getFilesRecursive($this->path, $files, $ext);
104
105 1
        return $files;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $files; (array) is incompatible with the return type of the parent method JamesMoss\Flywheel\Repository::getAllFiles of type RegexIterator.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
106
    }
107
108 3
    protected function isNestedId($id)
109
    {
110 3
        return strpos($id, self::SEPERATOR) !== false;
111
    }
112
113
    /**
114
     * Checks to see if a document ID is valid
115
     *
116
     * @param  string $id The ID to check
117
     *
118
     * @return bool     True if valid, otherwise false
119
     */
120 3
    protected function validateId($id)
121
    {
122
        // Similar regex to the one in the parent method, this allows forward slashes
123
        // in the key name, except for at the start or end.
124 3
        return (boolean)preg_match('/^[^\\/]?[^\\?\\*:;{}\\\\\\n]+[^\\/]$/us', $id);
125
    }
126
127 1
    protected function getFilesRecursive($dir, array &$result, $ext)
128
    {
129 1
        $extensionLength = strlen($ext) + 1; // one is for the dot!
130 1
        $files           = scandir($dir);
131 1
        foreach($files as $file) {
132 1
            if($file === '.' || $file === '..') {
133 1
                continue;
134
            }
135
136 1
            if(is_dir($newDir = $dir . DIRECTORY_SEPARATOR . $file)) {
137 1
                $this->getFilesRecursive($newDir, $result, $ext);
138 1
                continue;
139
            }
140
141 1
            if(substr($file, -$extensionLength) !== '.' . $ext) {
142
                continue;
143
            }
144
145 1
            $result[] = $dir . DIRECTORY_SEPARATOR . $file;
146 1
        }
147
148 1
        return $result;
149
    }
150
151
    /**
152
     * @inherit
153
     */
154 1
    protected function getIdFromPath($path, $ext)
155
    {
156 1
        return substr($path, strlen($this->path) + 1, -strlen($ext) - 1);
157
    }
158
159
}
160