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 ( 6a3c70...a07f0a )
by Miles
03:52
created

ResourceProvider::getParentContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
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\Vars;
22
23
/**
24
 * Vars Resource Provider to separate the different formats of config (array/file/dir) into one resource
25
 *
26
 * @since 0.1.0
27
 */
28
class ResourceProvider extends AbstractResource
29
{
30
31
    /**
32
     * The configuration entity -- could be a file, array or dir
33
     *
34
     * @var array|string
35
     */
36
    private $entity;
37
38
    /**
39
     * If the import was not relative then the content will be stored in this array
40
     *
41
     * @var array
42
     */
43
    private $parent_content = array();
44
45
    /**
46
     * Is the import relative
47
     *
48
     * @var bool
49
     */
50
    private $relative;
51
52
    /**
53
     * The parent Vars class
54
     *
55
     * @var \M1\Vars\Vars
56
     */
57
    public $vars;
58
59
    /**
60
     * The ResourceProvider constructor creates the content from the entity
61
     *
62
     * @param \M1\Vars\Vars $vars       The calling Vars class
63
     * @param string|array  $entity     The configuration entity
64
     * @param bool          $relative   Is the entity relative to the calling entity or class
65
     *
66
     * @throws \InvalidArgumentException If the entity passed is not a string or array
67
     */
68 62
    public function __construct(Vars $vars, $entity, $relative = true)
69
    {
70 62
        if (!is_string($entity) && !is_array($entity)) {
71 1
            throw new \InvalidArgumentException('You can only pass strings or arrays as Resources');
72
        }
73
74 61
        $this->vars = $vars;
75 61
        $this->entity = $entity;
76 61
        $this->relative = $relative;
77
78 61
        $this->createContent($entity);
79 50
    }
80
81
    /**
82
     * Creates the content from the entity
83
     *
84
     * @param string|array $entity The configuration entity
85
     */
86 61
    private function createContent($entity)
87
    {
88 61
        $type = gettype($entity);
89 61
        $resources = $this->processEntity($entity, $type);
90
91 58
        if ($resources && !empty($resources)) {
92 57
            foreach ($resources as $resource) {
0 ignored issues
show
Bug introduced by
The expression $resources of type array|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...
93 57
                if ($type === "string") {
94 57
                    $this->vars->pathsLoadedCheck($resource);
95
96 57
                    if ($this->vars->cache->checkCache()) {
97 1
                        return;
98
                    }
99
100 57
                    if ($this->vars->resourceImported($resource)) {
101 1
                        continue;
102
                    }
103 57
                    $pos = $this->vars->addResource($resource);
104 57
                    $resource = new FileResource($this, $resource);
105 49
                    $this->vars->updateResource($resource, $pos);
106 49
                } else {
107 1
                    $resource = new ResourceProvider($this->vars, $resource);
108
                }
109
110 49
                $this->addContent($resource->getContent());
111 49
            }
112 49
        }
113 50
    }
114
115
    /**
116
     * Creates the content from the entity
117
     *
118
     * @param string|array $entity The configuration entity
119
     * @param string       $type The type of entity
120
     *
121
     * @throws \InvalidArgumentException If the entity is not array|file, is readable or exists
122
     *
123
     * @returns array The array of resources
124
     */
125 61
    private function processEntity($entity, $type)
126
    {
127 61
        $resources = $entity;
128
129 61
        if ($type === 'string') {
130 61
            if (is_file($entity)) {
131 57
                $resources = array($entity);
132 61
            } elseif (is_dir($entity)) {
133 4
                $resources = $this->getSupportedFilesInDir();
134 4
            } else {
135 4
                throw new \InvalidArgumentException(sprintf("'%s' does not exist or is not readable", $entity));
136
            }
137 58
        }
138
139 58
        return $resources;
140
    }
141
142
    /**
143
     * Adds content to the parent contents
144
     *
145
     * @param array $content The content from the resource
146
     */
147 49
    private function addContent($content)
148
    {
149 49
        if ($this->relative) {
150 49
            $this->content = $this->mergeContents($this->content, $content);
151 49
        } else {
152 7
            $this->parent_content = $this->mergeContents($this->parent_content, $content);
153
        }
154 49
    }
155
156
    /**
157
     * Returns the supported files using the extensions from the loaders in the entity which is a directory
158
     *
159
     * @see \M1\Vars\Loader\LoaderProvider::getExtensions() \M1\Vars\Loader\LoaderProvider::getExtensions()
160
     * @see \M1\Vars\Loader\LoaderProvider::makeLoaders() \M1\Vars\Loader\LoaderProvider::makeLoaders()
161
     *
162
     * @return array|bool Returns the supported files or false if no files were found
163
     */
164 4
    private function getSupportedFilesInDir()
165
    {
166 4
        $dir_it = new \RecursiveDirectoryIterator($this->entity, \RecursiveDirectoryIterator::SKIP_DOTS);
167
168 4
        $dir_files = new \RecursiveIteratorIterator(
169 4
            $dir_it,
170 4
            \RecursiveIteratorIterator::LEAVES_ONLY,
171
            \RecursiveIteratorIterator::CATCH_GET_CHILD
172 4
        );
173
174 4
        $supported_files = new \RegexIterator(
175 4
            $dir_files,
176 4
            '/^.*\.('.implode('|', $this->vars->loader->getExtensions()).')$/i'
177 4
        );
178
179 4
        $paths = array();
180
181 4
        foreach ($supported_files as $path => $file) {
182 2
            if ($file->isFile()) {
183 2
                $paths[] = $path;
184 2
            }
185 4
        }
186
187 4
        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...
188 2
            $resources = array();
189
190 2
            foreach ($paths as $path) {
191 2
                $resources[] = $path;
192 2
            }
193
194 2
            return $resources;
195
        }
196
197 2
        return false;
198
    }
199
200
    /**
201
     * Merges various configuration contents into one array
202
     *
203
     * @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...
204
     *
205
     * @return array The merged contents
206
     */
207 50
    private function mergeContents()
208
    {
209 50
        $contents = func_get_args();
210 50
        return call_user_func_array('array_replace_recursive', $contents);
211
    }
212
213
    /**
214
     * Adds content to the parent content array
215
     *
216
     * @param array $content The content to add
217
     */
218 7
    public function addParentContent(array $content)
219
    {
220 7
        $this->parent_content = array_merge_recursive($this->parent_content, $content);
221 7
    }
222
223
    /**
224
     * Merges the content and the parent content together
225
     *
226
     * @return \M1\Vars\Resource\ResourceProvider
227
     */
228 50
    public function mergeParentContent()
229
    {
230 50
        $this->content = $this->mergeContents($this->content, $this->parent_content);
231
232 50
        return $this;
233
    }
234
235
    /**
236
     * Returns the parent content
237
     *
238
     * @return mixed The parent content
239
     */
240 38
    public function getParentContent()
241
    {
242 38
        return $this->parent_content;
243
    }
244
}
245