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 ( a07f0a...79e180 )
by Miles
05:56
created

ResourceProvider::getSupportedFilesInDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4286
cc 1
eloc 5
nc 1
nop 0
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.1.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\Resource;
20
21
use M1\Vars\Loader\DirectoryLoader;
22
use M1\Vars\Vars;
23
24
/**
25
 * Vars Resource Provider to separate the different formats of config (array/file/dir) into one resource
26
 *
27
 * @since 0.1.0
28
 */
29
class ResourceProvider extends AbstractResource
30
{
31
32
    /**
33
     * The configuration entity -- could be a file, array or dir
34
     *
35
     * @var array|string
36
     */
37
    private $entity;
38
39
    /**
40
     * If the import was not relative then the content will be stored in this array
41
     *
42
     * @var array
43
     */
44
    private $parent_content = array();
45
46
    /**
47
     * Is the import relative
48
     *
49
     * @var bool
50
     */
51
    private $relative;
52
53
    /**
54
     * The parent Vars class
55
     *
56
     * @var \M1\Vars\Vars
57
     */
58
    public $vars;
59
60
    /**
61
     * The ResourceProvider constructor creates the content from the entity
62
     *
63
     * @param \M1\Vars\Vars $vars     The calling Vars class
64
     * @param string|array  $entity   The configuration entity
65
     * @param bool          $relative Is the entity relative to the calling entity or class
66
     *
67
     * @throws \InvalidArgumentException If the entity passed is not a string or array
68
     */
69 62
    public function __construct(Vars $vars, $entity, $relative = true)
70
    {
71 62
        if (!is_string($entity) && !is_array($entity)) {
72 1
            throw new \InvalidArgumentException('You can only pass strings or arrays as Resources');
73
        }
74
75 61
        $this->vars = $vars;
76 61
        $this->entity = $entity;
77 61
        $this->relative = $relative;
78
79 61
        $this->createContent($entity);
80 50
    }
81
82
    /**
83
     * Creates the content from the entity
84
     *
85
     * @param string|array $entity The configuration entity
86
     */
87 61
    private function createContent($entity)
88
    {
89 61
        $type = gettype($entity);
90 61
        $resources = $this->processEntity($entity, $type);
91
92 58
        if ($resources && !empty($resources)) {
93 57
            foreach ($resources as $resource) {
0 ignored issues
show
Bug introduced by
The expression $resources of type array|boolean|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
94 57
                if ($type === "string") {
95 57
                    $this->vars->pathsLoadedCheck($resource);
96
97 57
                    if ($this->vars->cache->checkCache()) {
98 1
                        return;
99
                    }
100
101 57
                    if ($this->vars->resourceImported($resource)) {
102 1
                        continue;
103
                    }
104 57
                    $pos = $this->vars->addResource($resource);
105 57
                    $resource = new FileResource($this, $resource);
106 49
                    $this->vars->updateResource($resource, $pos);
107 49
                } else {
108 1
                    $resource = new ResourceProvider($this->vars, $resource);
109
                }
110
111 49
                $this->addContent($resource->getContent());
112 49
            }
113 49
        }
114 50
    }
115
116
    /**
117
     * Creates the content from the entity
118
     *
119
     * @param string|array $entity The configuration entity
120
     * @param string       $type   The type of entity
121
     *
122
     * @throws \InvalidArgumentException If the entity is not array|file, is readable or exists
123
     *
124
     * @returns array The array of resources
125
     */
126 61
    private function processEntity($entity, $type)
127
    {
128 61
        $resources = $entity;
129
130 61
        if ($type === 'string') {
131 61
            if (is_file($entity)) {
132 57
                $resources = array($entity);
133 61
            } elseif (is_dir($entity)) {
134 4
                $resources = $this->getSupportedFilesInDir();
135 4
            } else {
136 4
                throw new \InvalidArgumentException(sprintf("'%s' does not exist or is not readable", $entity));
137
            }
138 58
        }
139
140 58
        return $resources;
141
    }
142
143
    /**
144
     * Adds content to the parent contents
145
     *
146
     * @param array $content The content from the resource
147
     */
148 49
    private function addContent($content)
149
    {
150 49
        if ($this->relative) {
151 49
            $this->content = $this->mergeContents($this->content, $content);
152 49
        } else {
153 7
            $this->parent_content = $this->mergeContents($this->parent_content, $content);
154
        }
155 49
    }
156
157
    /**
158
     * Returns the supported files using the extensions from the loaders in the entity which is a directory
159
     *
160
     * @see \M1\Vars\Loader\LoaderProvider::getExtensions() \M1\Vars\Loader\LoaderProvider::getExtensions()
161
     * @see \M1\Vars\Loader\LoaderProvider::makeLoaders() \M1\Vars\Loader\LoaderProvider::makeLoaders()
162
     *
163
     * @return array|bool Returns the supported files or false if no files were found
164
     */
165 4
    private function getSupportedFilesInDir()
166
    {
167 4
        $dir_loader = new DirectoryLoader($this->entity);
0 ignored issues
show
Bug introduced by
It seems like $this->entity can also be of type array; however, M1\Vars\Loader\AbstractLoader::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
168 4
        $dir_loader->setSupports($this->vars->loader->getExtensions());
169 4
        $dir_loader->load();
170
171 4
        return $dir_loader->getContent();
172
    }
173
174
    /**
175
     * Merges various configuration contents into one array
176
     *
177
     * @param mixed $contents,... Optional number of arrays to merge
0 ignored issues
show
Bug introduced by
There is no parameter named $contents,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
178
     *
179
     * @return array The merged contents
180
     */
181 50
    private function mergeContents()
182
    {
183 50
        $contents = func_get_args();
184 50
        return call_user_func_array('array_replace_recursive', $contents);
185
    }
186
187
    /**
188
     * Adds content to the parent content array
189
     *
190
     * @param array $content The content to add
191
     */
192 7
    public function addParentContent(array $content)
193
    {
194 7
        $this->parent_content = array_merge_recursive($this->parent_content, $content);
195 7
    }
196
197
    /**
198
     * Merges the content and the parent content together
199
     *
200
     * @return \M1\Vars\Resource\ResourceProvider
201
     */
202 50
    public function mergeParentContent()
203
    {
204 50
        $this->content = $this->mergeContents($this->content, $this->parent_content);
205
206 50
        return $this;
207
    }
208
209
    /**
210
     * Returns the parent content
211
     *
212
     * @return mixed The parent content
213
     */
214 38
    public function getParentContent()
215
    {
216 38
        return $this->parent_content;
217
    }
218
}
219