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 ( 99b579...427409 )
by Miles
02:46
created

Vars::makeLoaders()   D

Complexity

Conditions 10
Paths 72

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 10.0268

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 45
ccs 29
cts 31
cp 0.9355
rs 4.8197
cc 10
eloc 27
nc 72
nop 1
crap 10.0268

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
20
21
use M1\Vars\Cache\CacheProvider;
22
use M1\Vars\Resource\AbstractResource;
23
use M1\Vars\Resource\ResourceProvider;
24
use M1\Vars\Resource\VariableResource;
25
26
/**
27
 * Vars core class
28
 *
29
 * @since 0.1.0
30
 */
31
class Vars extends AbstractResource
32
{
33
    /**
34
     * The base path for the Vars config and cache folders
35
     *
36
     * @var string $base_path
37
     */
38
    private $base_path;
39
40
    /**
41
     * The cache object if the cache is wanted, else false
42
     *
43
     * @var \M1\Vars\Cache\CacheProvider $cache
44
     */
45
    public $cache;
46
47
    /**
48
     * The available extensions
49
     *
50
     * @var array $extensions
51
     */
52
    private $extensions = array();
53
54
    /**
55
     * The default options for Vars
56
     *
57
     * @var array $default_options
58
     */
59
    private $default_options = array(
60
        'base_path' => null,
61
        'cache' => true,
62
        'cache_path' => null,
63
        'cache_expire' => 300, // 5 minutes
64
        'loaders' => array('ini', 'json', 'php', 'toml', 'yaml', 'xml',)
65
    );
66
67
    /**
68
     * The available loaders
69
     *
70
     * @var array $loaders
71
     */
72
    private $loaders = array();
73
74
    /**
75
     * Have the base and cache paths been set
76
     *
77
     * @var bool $paths_loaded
78
     */
79
    private $paths_loaded = false;
80
81
    /**
82
     * The imported resources
83
     *
84
     * @var array $resources
85
     */
86
    private $resources = array();
87
88
    /**
89
     * The words to be replaced in the config files
90
     *
91
     * @var array $variables
92
     */
93
    private $variables = array();
94
95
    /**
96
     * Creates a new instance of Vars
97
     *
98
     * @param string|array $resource The main configuration resource
99
     * @param array        $options  The options being used for Vars
100
     */
101 61
    public function __construct($resource, $options = null)
102
    {
103 61
        if (!$options) {
104 1
            $options = $this->default_options;
105 1
        } else {
106 60
            $options = array_merge($this->default_options, $options);
107
        }
108
109 61
        $this->makeCache($options, $resource);
110 60
        $this->makePaths($options);
111
112 59
        if (!$this->cache->checkCache()) {
113 59
            $this->makeLoaders($options);
114 57
            $this->makeVariables($options);
115
116 56
            $resource = new ResourceProvider($this, $resource);
117 45
        }
118
119 45
        if ($this->cache->isHit()) {
120 2
            $this->loadFromCache();
121 2
        } else {
122 45
            $resource->mergeParentContent();
0 ignored issues
show
Bug introduced by
It seems like $resource is not always an object, but can also be of type string|array. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
123 45
            $this->content = $resource->getContent();
124
125 45
            $this->cache->setTime(time());
126 45
            $this->cache->makeCache($this);
127
128
        }
129 45
    }
130
131
    /**
132
     * Makes the CacheProvider with the options
133
     *
134
     * @param array        $options  The options being used for Vars
135
     * @param array|string $resource The main configuration resource
136
     */
137 61
    private function makeCache($options, $resource)
138
    {
139 61
        $cache = new CacheProvider($resource, array_merge($this->default_options, $options));
140 60
        $this->cache = $cache;
141 60
    }
142
143
    /**
144
     * Sets the base path if the options have been set and the cache path if the cache path has not been set but the
145
     * base path has
146
     *
147
     * @param array $options The options being used for Vars
148
     */
149 60
    private function makePaths($options)
150
    {
151 60
        $this->setBasePath($options['base_path']);
152
153 59
        if (is_null($options['cache_path']) && !is_null($options['base_path'])) {
154 1
            $this->cache->setPath($options['base_path']);
155 1
            $this->paths_loaded = true;
156 1
        }
157 59
    }
158
159
    /**
160
     * Get loaders and make extensions for the loaders
161
     *
162
     * @param array|null $options The options being used for Vars
163
     *
164
     * @throws \InvalidArgumentException If a loader from options isn't found
165
     * @throws \InvalidArgumentException If no loaders were loaded
166
     */
167 59
    private function makeLoaders($options)
168
    {
169 59
        $loaders = array();
170 59
        $default_loaders = $this->default_options['loaders'];
171
172 59
        if (is_array($options['loaders']) && !empty($options['loaders'])) {
173 55
            $loaders = $options['loaders'];
174 59
        } elseif (is_string($options['loaders'])) {
175 4
            $loaders[] = $options['loaders'];
176 4
        } else {
177
            $loaders = $default_loaders;
178
        }
179
180 59
        $parsed_loaders = array();
181
182 59
        foreach ($loaders as $loader) {
183 59
            if ($loader === 'default') {
184 2
                $parsed_loaders = array_merge($parsed_loaders, $default_loaders);
185 2
            } else {
186 58
                $parsed_loaders[] = $loader;
187
            }
188 59
        }
189
190 59
        $parsed_loaders = array_unique($parsed_loaders);
191 59
        $namespaced_loaders = array();
192
193 59
        foreach ($parsed_loaders as $loader) {
194 59
            if (in_array($loader, $default_loaders)) {
195 56
                $loader = sprintf('%s\Loader\%sLoader', __NAMESPACE__, ucfirst(strtolower($loader)));
196 56
            }
197
198 59
            if (!class_exists($loader)) {
199 1
                throw new \InvalidArgumentException(sprintf("'%s' loader class does not exist", $loader));
200
            }
201
202 58
            $namespaced_loaders[] = $loader;
203 58
        }
204
205 58
        if (empty($namespaced_loaders)) {
206
            throw new \InvalidArgumentException('No loaders were loaded');
207
        }
208
209 58
        $this->loaders = $namespaced_loaders;
210 58
        $this->extensions = $this->makeExtensions($namespaced_loaders);
211 57
    }
212
213
    /**
214
     * Sets the replacement variables if the option has been set
215
     *
216
     * @param array|null $options The options being used for Vars
217
     */
218 57
    private function makeVariables($options)
219
    {
220 57
        if (isset($options['variables'])) {
221 4
            $variables = new VariableResource($this, $options['variables']);
222
223 3
            $v = array();
224
225 3
            foreach ($variables->getVariables() as $variable_key => $variable_value) {
226 3
                $v["%".$variable_key.'%'] = $variable_value;
227 3
            }
228
229 3
            $this->variables = $v;
230 3
        }
231 56
    }
232
233
    /**
234
     * Loads the cached file into the current class
235
     */
236 2
    private function loadFromCache()
237
    {
238 2
        $this->cache->load();
239
240
        $passed_keys = array(
241 2
            'base_path',
242 2
            'content',
243 2
            'extensions',
244 2
            'loaders',
245 2
            'resources',
246 2
            'variables',
247 2
        );
248
249 2
        $loaded_vars = get_object_vars($this->cache->getLoadedVars());
250
251 2
        foreach ($loaded_vars as $key => $value) {
252 2
            if (in_array($key, $passed_keys)) {
253 2
                $this->$key = $value;
254 2
            }
255 2
        }
256
257 2
        $this->cache->setTime($loaded_vars['cache']->getTime());
258 2
    }
259
260
    /**
261
     * Get and make extensions for loaders made from makeLoaders()
262
     *
263
     * @see \M1\Vars\Vars::makeLoaders() \M1\Vars\Vars::makeLoaders()
264
     *
265
     * @param  array $loaders File loaders
266
     *
267
     * @throws \RuntimeException If no loader extensions were found
268
     *
269
     * @return array File loader supported extensions
270
     */
271 58
    private function makeExtensions(array $loaders)
272
    {
273 58
        $extensions = array();
274
275 58
        foreach ($loaders as $loader) {
276 58
            $extensions = array_merge($extensions, $loader::$supported);
277 58
        }
278
279 58
        if (empty($extensions)) {
280 1
            throw new \RuntimeException('No loader extensions were found');
281
        }
282
283 57
        return $extensions;
284
    }
285
286
    /**
287
     * Checks if the base and cache paths have been set, if not set then will use the $resource as the base path
288
     *
289
     * @param string $resource The resource to use to set the paths if they haven't been set
290
     */
291 51
    public function pathsLoadedCheck($resource)
292
    {
293 51
        if (!$this->paths_loaded) {
294 50
            $base_path = $this->getBasePath();
295
296 50
            if (!$base_path) {
297 48
                $file = pathinfo(realpath($resource));
298 48
                $base_path = $file['dirname'];
299 48
                $this->setBasePath($base_path);
300 48
            }
301
302 50
            if ($this->cache->getProvide() && !$this->cache->getPath()) {
303
                $this->cache->setPath($base_path);
304
            }
305
306 50
            $this->paths_loaded = true;
307 50
        }
308 51
    }
309
310
    /**
311
     * Get the Vars file loaders
312
     *
313
     * @return array The Vars file loaders
314
     */
315 51
    public function getLoaders()
316
    {
317 51
        return $this->loaders;
318
    }
319
320
    /**
321
     * Get the Vars file loaders extensions
322
     *
323
     * @return array The Vars file loader extensions
324
     */
325 4
    public function getExtensions()
326
    {
327 4
        return $this->extensions;
328
    }
329
330
    /**
331
     * Get the Vars base path
332
     *
333
     * @return string The Vars base path
334
     */
335 51
    public function getBasePath()
336
    {
337 51
        return $this->base_path;
338
    }
339
340
    /**
341
     * Set the Vars base path
342
     *
343
     * @param string $base_path The base path to set
344
     *
345
     * @throws \InvalidArgumentException If the base path does not exist or is not writable
346
     *
347
     * @return \M1\Vars\Vars
348
     */
349 60 View Code Duplication
    public function setBasePath($base_path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
350
    {
351 60
        if (is_null($base_path)) {
352 56
            return;
353
        }
354
355 52
        if (!is_dir($base_path)) {
356 1
            throw new \InvalidArgumentException(sprintf(
357 1
                "'%s' base path does not exist or is not writable",
358
                $base_path
359 1
            ));
360
        }
361
362 51
        $this->base_path = realpath($base_path);
363 51
        return $this;
364
    }
365
    
366
    /**
367
     * Adds a resource to $this->resources
368
     *
369
     * @param string $resource Resource to add to the stack
370
     *
371
     * @return int The position of the added resource
372
     */
373 51
    public function addResource($resource)
374
    {
375 51
        $r = realpath($resource);
376 51
        $pos = count($this->resources);
377 51
        $this->resources[$pos] = $r;
378 51
        return $pos;
379
    }
380
381
    /**
382
     * Updates the string resource with the FileResource
383
     *
384
     * @param \M1\Vars\Resource\FileResource $resource The FileResource to add
385
     * @param int                            $pos      The position of the string resource
386
     *
387
     * @return \M1\Vars\Vars
388
     */
389 44
    public function updateResource($resource, $pos)
390
    {
391 44
        $this->resources[$pos] = $resource;
392 44
        return $this;
393
    }
394
395
    /**
396
     * Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop
397
     *
398
     * @param \M1\Vars\Resource\FileResource|string $resource Resource to check
399
     *
400
     * @return bool Has resource already been imported
401
     */
402 51
    public function resourceImported($resource)
403
    {
404 51
        $resource = realpath($resource);
405 51
        foreach ($this->getResources() as $r) {
406 34
            if ((is_a($r, 'M1\Vars\Resource\FileResource') && $resource === $r->getFile()) ||
407 34
                (is_string($r) && $resource === $r)) {
408 1
                return true;
409
            }
410 51
        }
411 51
        return false;
412
    }
413
414
    /**
415
     * Searches the resource stack for a certain resource
416
     *
417
     * @param string $resource The resource to search for
418
     *
419
     * @return bool Returns the resource if found
420
     */
421 3
    public function getResource($resource)
422
    {
423 3
        foreach ($this->getResources() as $r) {
424 3
            if ($resource === $r->getFilename()) {
425 2
                return $r;
426
            }
427 3
        }
428
429 1
        return false;
430
    }
431
432
    /**
433
     * Returns the imported resources
434
     *
435
     * @return array The Vars imported resources
436
     */
437 51
    public function getResources()
438
    {
439 51
        return $this->resources;
440
    }
441
442
    /**
443
     * Returns the Vars replacement variables
444
     *
445
     * @return array The Vars replacement variables
446
     */
447 40
    public function getVariables()
448
    {
449 40
        return $this->variables;
450
    }
451
452
    /**
453
     * Returns the CacheProvider if set, if not return false
454
     *
455
     * @return \M1\Vars\Cache\CacheProvider|false The CacheProvider or false
456
     */
457 5
    public function getCache()
458
    {
459 5
        return $this->cache;
460
    }
461
}
462