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 ( 2e4472...8d7757 )
by Miles
07:32
created

Vars::getGlobals()   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 1
Bugs 0 Features 0
Metric Value
c 1
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     1.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\Loader\LoaderProvider;
23
use M1\Vars\Resource\AbstractResource;
24
use M1\Vars\Resource\ResourceProvider;
25
use M1\Vars\Traits\PathTrait;
26
use M1\Vars\Traits\TransformerTrait;
27
use M1\Vars\Variables\VariableProvider;
28
29
/**
30
 * Vars core class
31
 *
32
 * @since 0.1.0
33
 */
34
class Vars extends AbstractResource
35
{
36
    /**
37
     * Used for path functions and variables
38
     */
39
    use PathTrait;
40
41
    /**
42
     * Used for to* functions
43
     */
44
    use TransformerTrait;
45
46
    /**
47
     * The cache object if the cache is wanted, else false
48
     *
49
     * @var \M1\Vars\Cache\CacheProvider $cache
50
     */
51
    public $cache;
52
53
    /**
54
     * The default options for Vars
55
     *
56
     * @var array $default_options
57
     */
58
    private $default_options = array(
59
        'path' => null,
60
        'cache' => true,
61
        'cache_path' => null,
62
        'cache_expire' => 300, // 5 minutes
63
        'loaders' => array('env', 'ini', 'json', 'php', 'toml', 'yaml', 'xml',),
64
        'merge_globals' => true,
65
    );
66
67
    /**
68
     * The global file variables
69
     *
70
     * @var array globals
71
     */
72
    private $globals = array();
73
74
    /**
75
     * The loaderProvider for Vars supplies the file loaders and the extensions that are supported
76
     *
77
     * @var \M1\Vars\Loader\LoaderProvider $loader
78
     */
79
    public $loader;
80
81
    /**
82
     * Have the base and cache paths been set
83
     *
84
     * @var bool $paths_loaded
85
     */
86
    private $paths_loaded = false;
87
88
    /**
89
     * The imported resources
90
     *
91
     * @var array $resources
92
     */
93
    private $resources = array();
94
95
    /**
96
     * The variable provider
97
     *
98
     * @var \M1\Vars\Variables\VariableProvider $variables
99
     */
100
    public $variables;
101
102
    /**
103
     * Creates a new instance of Vars
104
     *
105
     * @param string|array $resource The main configuration resource
106
     * @param array        $options  The options being used for Vars
107
     */
108 86
    public function __construct($resource, $options = array())
109
    {
110 86
        $options = $this->parseOptions($options);
111 86
        $this->makeCache($options, $resource);
112 85
        $this->makePaths($options);
113 84
        if (!$this->cache->checkCache()) {
114 84
            $this->makeLoader($options);
115 82
            $this->makeVariables($options);
116
117 81
            $resource = new ResourceProvider($this, $resource);
118 66
        }
119
120 66
        if ($this->cache->isHit()) {
121 3
            $this->loadFromCache();
122 3
        } else {
123 66
            $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...
124 66
            $this->content = $this->mergeGlobals($resource->getContent(), $options);
125 66
            $this->cache->setTime(time());
126 66
            $this->cache->makeCache($this);
127
        }
128 66
    }
129
130
    /**
131
     * Parses the options so Vars can use them
132
     *
133
     * @param array $options  The options being used for Vars
134
     *
135
     * @return array The parsed options
136
     */
137 86
    private function parseOptions(array $options)
138
    {
139 86
        $parsed_options = array_merge($this->default_options, $options);
140 86
        $parsed_options['loaders'] = (isset($options['loaders'])) ?
141 86
            $options['loaders'] : $this->default_options['loaders'];
142
143 86
        return $parsed_options;
144
    }
145
146
    /**
147
     * Makes the CacheProvider with the options
148
     *
149
     * @param array        $options  The options being used for Vars
150
     * @param array|string $resource The main configuration resource
151
     */
152 86
    private function makeCache($options, $resource)
153
    {
154 86
        $cache = new CacheProvider($resource, $options);
155 85
        $this->cache = $cache;
156 85
    }
157
158
    /**
159
     * Sets the base path if the options have been set and the cache path if the cache path has not been set but the
160
     * base path has
161
     *
162
     * @param array $options The options being used for Vars
163
     */
164 85
    private function makePaths($options)
165
    {
166 85
        $this->setPath($options['path']);
167
168 84
        if (is_null($options['cache_path']) && !is_null($options['path'])) {
169 1
            $this->cache->setPath($options['path']);
170 1
            $this->paths_loaded = true;
171 1
        }
172 84
    }
173
174
    /**
175
     * Makes the LoaderProvider with the options
176
     *
177
     * @param array $options  The options being used for Vars
178
     */
179 84
    private function makeLoader($options)
180
    {
181 84
        $loader = new LoaderProvider($options, $this->default_options['loaders']);
182 82
        $this->loader = $loader;
183 82
    }
184
185
    /**
186
     * Sets the replacement variables if the option has been set
187
     *
188
     * @param array|null $options The options being used for Vars
189
     */
190 82
    private function makeVariables($options)
191
    {
192 82
        $this->variables = new VariableProvider($this);
193
194 82
        if (isset($options['replacements'])) {
195 4
            $this->variables->rstore->load($options['replacements']);
196 3
        }
197 81
    }
198
199
    /**
200
     * Loads the cached file into the current class
201
     */
202 3
    private function loadFromCache()
203
    {
204 3
        $this->cache->load();
205
206
        $passed_keys = array(
207 3
            'path',
208 3
            'content',
209 3
            'extensions',
210 3
            'loaders',
211 3
            'resources',
212 3
            'replacements',
213 3
        );
214
215 3
        $loaded_vars = get_object_vars($this->cache->getLoadedVars());
216
217 3
        foreach ($loaded_vars as $key => $value) {
218 3
            if (in_array($key, $passed_keys)) {
219 3
                $this->$key = $value;
220 3
            }
221 3
        }
222
223 3
        $this->cache->setTime($loaded_vars['cache']->getTime());
224 3
    }
225
226
    /**
227
     * Checks if the base and cache paths have been set, if not set then will use the $resource as the base path
228
     *
229
     * @param string $resource The resource to use to set the paths if they haven't been set
230
     */
231 75
    public function pathsLoadedCheck($resource)
232
    {
233 75
        if (!$this->paths_loaded) {
234 74
            $path = $this->getPath();
235
236 74
            if (!$path) {
237 72
                $file = pathinfo(realpath($resource));
238 72
                $path = $file['dirname'];
239 72
                $this->setPath($path);
240 72
            }
241
242 74
            if ($this->cache->getProvide() && !$this->cache->getPath()) {
243 2
                $this->cache->setPath($path);
244 2
            }
245
246 74
            $this->paths_loaded = true;
247 74
        }
248 75
    }
249
250
251
    /**
252
     * Gets the _globals from the file and merges them if merge_globals is true
253
     *
254
     * @param array $content The unparsed content
255
     * @param array $options  The options being used for Vars
256
     *
257
     * @return array $content The parsed content
258
     */
259 66
    private function mergeGlobals($content, $options)
260
    {
261 66
        if (array_key_exists('_globals', $content)) {
262 3
            $this->globals = $content['_globals'];
263
264 3
            if ($options['merge_globals']) {
265 1
                $content = array_replace_recursive($content, $content['_globals']);
266 1
            }
267
268 3
            unset($content['_globals']);
269 3
        }
270
271 66
        return $content;
272
    }
273
274
    /**
275
     * Adds a resource to $this->resources
276
     *
277
     * @param string $resource Resource to add to the stack
278
     *
279
     * @return int The position of the added resource
280
     */
281 75
    public function addResource($resource)
282
    {
283 75
        $r = realpath($resource);
284 75
        $pos = count($this->resources);
285 75
        $this->resources[$pos] = $r;
286 75
        return $pos;
287
    }
288
289
    /**
290
     * Updates the string resource with the FileResource
291
     *
292
     * @param \M1\Vars\Resource\FileResource $resource The FileResource to add
293
     * @param int                            $pos      The position of the string resource
294
     *
295
     * @return \M1\Vars\Vars
296
     */
297 64
    public function updateResource($resource, $pos)
298
    {
299 64
        $this->resources[$pos] = $resource;
300 64
        return $this;
301
    }
302
303
    /**
304
     * Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop
305
     *
306
     * @param \M1\Vars\Resource\FileResource|string $resource Resource to check
307
     *
308
     * @return bool Has resource already been imported
309
     */
310 75
    public function resourceImported($resource)
311
    {
312 75
        $resource = realpath($resource);
313 75
        foreach ($this->getResources() as $r) {
314 49
            if ((is_a($r, 'M1\Vars\Resource\FileResource') && $resource === $r->getFile()) ||
315 49
                (is_string($r) && $resource === $r)) {
316 1
                return true;
317
            }
318 75
        }
319 75
        return false;
320
    }
321
322
    /**
323
     * Searches the resource stack for a certain resource
324
     *
325
     * @param string $resource The resource to search for
326
     *
327
     * @return \M1\Vars\Resource\FileResource|bool Returns the resource if found
328
     */
329 3
    public function getResource($resource)
330
    {
331 3
        foreach ($this->getResources() as $r) {
332 3
            if ($resource === $r->getFilename()) {
333 2
                return $r;
334
            }
335 3
        }
336
337 1
        return false;
338
    }
339
340
    /**
341
     * Returns the imported resources
342
     *
343
     * @return array The Vars imported resources
344
     */
345 75
    public function getResources()
346
    {
347 75
        return $this->resources;
348
    }
349
350
    /**
351
     * Returns the imported resources
352
     *
353
     * @return array The Vars imported resources
354
     */
355 2
    public function getGlobals()
356
    {
357 2
        return $this->globals;
358
    }
359
360
    /**
361
     * Returns the CacheProvider if set
362
     *
363
     * @return \M1\Vars\Cache\CacheProvider The CacheProvider
364
     */
365 7
    public function getCache()
366
    {
367 7
        return $this->cache;
368
    }
369
}
370