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.
Completed
Push — master ( 3094b9...9c33be )
by Miles
06:34
created

DirectoryLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This file is part of the m1\vars library
5
 *
6
 * (c) m1 <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @package     m1/vars
12
 * @version     0.2.0
13
 * @author      Miles Croxford <[email protected]>
14
 * @copyright   Copyright (c) Miles Croxford <[email protected]>
15
 * @license     http://github.com/m1/vars/blob/master/LICENSE
16
 * @link        http://github.com/m1/vars/blob/master/README.MD Documentation
17
 */
18
19
namespace M1\Vars\Loader;
20
21
/**
22
 * This file provides dir loading support for Vars
23
 *
24
 * @since 0.1.1
25
 */
26
class DirectoryLoader extends AbstractLoader
27
{
28
    /**
29
     * Construct the loader with the passed entity
30
     *
31
     * @param string $entity    The passed entity
32
     * @param bool   $recursive Search the directories   recursively
33
     */
34 5
    public function __construct($entity, $recursive)
35
    {
36 5
        parent::__construct($entity);
37 5
        $this->recursive = $recursive;
0 ignored issues
show
Bug introduced by
The property recursive 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...
38 5
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 5
    public function load()
44
    {
45 5
        $paths = array();
46 5
        $files = ($this->recursive) ? $this->getSupportedFilesRecursively() : $this->getSupportedFiles();
47
48 5
        foreach ($files as $path => $file) {
49 3
            if ($file->isFile()) {
50 3
                $paths[] = $path;
51 3
            }
52 5
        }
53 5
        $this->content = $this->makeResources($paths);
54
55 5
        return $this;
56
    }
57
58
    /**
59
     * Returns the supported files in the directory recursively
60
     *
61
     * @return array The supported files in the directories
62
     */
63 4
    private function getSupportedFilesRecursively()
64
    {
65 4
        $dir_it = new \RecursiveDirectoryIterator($this->entity, \RecursiveDirectoryIterator::SKIP_DOTS);
66
67 4
        $files = new \RecursiveIteratorIterator(
68 4
            $dir_it,
69 4
            \RecursiveIteratorIterator::LEAVES_ONLY,
70
            \RecursiveIteratorIterator::CATCH_GET_CHILD
71 4
        );
72
73 4
        return $this->createRegexIterator($files);
74
    }
75
76
    /**
77
     * Returns the supported files in the directory
78
     *
79
     * @return array The supported files in the directory
80
     */
81 2
    private function getSupportedFiles()
82
    {
83
84 2
        $files = new \FilesystemIterator($this->entity);
85
86 2
        return $this->createRegexIterator($files);
87
    }
88
89
    /**
90
     * Returns the supported files in the directory
91
     *
92
     * @param \FilesystemIterator|\RecursiveIteratorIterator $files The found files in the directory/ies
93
     *
94
     * @return \RegexIterator The supported files in the directory using the regexiterator
95
     */
96 5
    private function createRegexIterator($files)
97
    {
98 5
        return new \RegexIterator(
99 5
            $files,
100 5
            '/^.*\.(' . implode('|', static::$supported) . ')$/i'
101 5
        );
102
    }
103
104
    /**
105
     * Makes usable resource paths from path strings
106
     *
107
     * @param array $paths The path strings
108
     *
109
     * @return array|bool  The usable resources if any, else false
110
     */
111 5
    private function makeResources($paths)
112
    {
113 5
        if ($paths && !empty($paths)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $paths of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
114 3
            $resources = array();
115
116 3
            foreach ($paths as $path) {
117 3
                $resources[] = $path;
118 3
            }
119
120 3
            return $resources;
121
        }
122
123 2
        return false;
124
    }
125
}
126