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 ( 913970...1487d1 )
by Hong
04:43
created

Config::referenceLookup()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Config
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Config;
16
17
use Phossa2\Shared\Tree\Tree;
18
use Phossa2\Config\Message\Message;
19
use Phossa2\Config\Loader\DummyLoader;
20
use Phossa2\Shared\Tree\TreeInterface;
21
use Phossa2\Shared\Base\ObjectAbstract;
22
use Phossa2\Config\Traits\WritableTrait;
23
use Phossa2\Config\Traits\ArrayAccessTrait;
24
use Phossa2\Shared\Reference\ReferenceTrait;
25
use Phossa2\Config\Exception\LogicException;
26
use Phossa2\Config\Interfaces\ConfigInterface;
27
use Phossa2\Config\Loader\ConfigLoaderInterface;
28
use Phossa2\Shared\Reference\ReferenceInterface;
29
use Phossa2\Config\Interfaces\WritableInterface;
30
use Phossa2\Config\Interfaces\ChainingInterface;
31
use Phossa2\Shared\Delegator\DelegatorAwareTrait;
32
use Phossa2\Shared\Delegator\DelegatorAwareInterface;
33
34
/**
35
 * Config
36
 *
37
 * @package Phossa2\Config
38
 * @author  Hong Zhang <[email protected]>
39
 * @see     ObjectAbstract
40
 * @see     ConfigInterface
41
 * @see     WritableInterface
42
 * @see     \ArrayAccess
43
 * @see     ReferenceInterface
44
 * @see     DelegatorAwareInterface
45
 * @version 2.0.7
46
 * @since   2.0.0 added
47
 * @since   2.0.7 changed DelegatorAware* stuff
48
 */
49
class Config extends ObjectAbstract implements ConfigInterface, WritableInterface, \ArrayAccess, ReferenceInterface, DelegatorAwareInterface
50
{
51
    use ReferenceTrait, DelegatorAwareTrait, ArrayAccessTrait, WritableTrait;
52
53
    /**
54
     * error type
55
     *
56
     * @var    int
57
     */
58
    const ERROR_IGNORE    = 0;
59
    const ERROR_WARNING   = 1;
60
    const ERROR_EXCEPTION = 2;
61
62
    /**
63
     * the config loader
64
     *
65
     * @var    ConfigLoaderInterface
66
     * @access protected
67
     */
68
    protected $loader;
69
70
    /**
71
     * the config tree
72
     *
73
     * @var    TreeInterface
74
     * @access protected
75
     */
76
    protected $config;
77
78
    /**
79
     * cache loaded group names
80
     *
81
     * @var    array
82
     * @access protected
83
     */
84
    protected $loaded = [];
85
86
    /**
87
     * How to dealing with error, ignore/trigger_error/exception etc.
88
     *
89
     * @var    int
90
     * @access protected
91
     */
92
    protected $error_type = self::ERROR_WARNING;
93
94
    /**
95
     * Constructor
96
     *
97
     * @param  ConfigLoaderInterface $loader config loader if any
98
     * @param  TreeInterface $configTree config tree if any
99
     * @param  array $configData config data for the tree
100
     * @access public
101
     * @api
102
     */
103
    public function __construct(
104
        ConfigLoaderInterface $loader = null,
105
        TreeInterface $configTree = null,
106
        array $configData = []
107
    ) {
108
        $this->loader = $loader ?: new DummyLoader();
109
        $this->config = $configTree ?: new Tree($configData);
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115
    public function get(/*# string */ $id, $default = null)
116
    {
117
        try {
118
            // lazy load
119
            $this->loadConfig((string) $id);
120
121
            //  get value
122
            $val = $this->config->getNode((string) $id);
123
124
            // dereference
125
            $this->deReferenceArray($val);
126
127
            return null === $val ? $default : $val;
128
129
        // if dereference exception catched
130
        } catch (\Exception $e) {
131
            $this->throwError($e->getMessage(), $e->getCode());
132
            return $default;
133
        }
134
    }
135
136
    /**
137
     * {@inheritDoc}
138
     */
139
    public function has(/*# string */ $id)/*# : bool */
140
    {
141
        try {
142
            // update error type
143
            $err = $this->error_type;
144
            $this->setErrorType(self::ERROR_IGNORE);
145
146
            // lazy load
147
            $this->loadConfig((string) $id);
148
149
            //  get value
150
            $result = null !== $this->config->getNode((string) $id);
151
152
            // restore error type
153
            $this->setErrorType($err);
154
155
            return $result;
156
157
        } catch (\Exception $e) {
158
            return false;
159
        }
160
    }
161
162
    /**
163
     * {@inheritDoc}
164
     */
165
    public function set(/*# string */ $id, $value)
166
    {
167
        if ($this->isWritable()) {
168
            // lazy load, no dereference
169
            $this->loadConfig((string) $id);
170
171
            // replace the node
172
            $this->config->addNode($id, $value);
173
174
            return $this;
175
        } else {
176
            $this->throwError(
177
                Message::get(Message::CONFIG_NOT_WRITABLE),
178
                Message::CONFIG_NOT_WRITABLE
179
            );
180
        }
181
    }
182
183
    /**
184
     * Set error type
185
     *
186
     * @param  int $type
187
     * @return $this
188
     * @access public
189
     * @api
190
     */
191
    public function setErrorType(/*# int */ $type)
192
    {
193
        $this->error_type = (int) $type;
194
        return $this;
195
    }
196
197
    /**
198
     * Load config
199
     *
200
     * @param  string $id
201
     * @return $this
202
     * @throws LogicException if current $error_type is to throw exception
203
     * @access protected
204
     */
205
    protected function loadConfig(/*# string */ $id)
206
    {
207
        // get group name
208
        $group = $this->getGroupName($id);
209
210
        // $group loaded ?
211
        if (isset($this->loaded[$group])) {
212
            return $this;
213
        }
214
215
        // mark as loaded
216
        $this->loaded[$group] = true;
217
218
        // loading the group
219
        return $this->loadByGroup($group);
220
    }
221
222
    /**
223
     * Load one group config, force loading all groups if $group == ''
224
     *
225
     * @param  string $group
226
     * @return $this
227
     * @throws \Exception group loading issues
228
     * @access protected
229
     */
230
    protected function loadByGroup(/*# string */ $group)
231
    {
232
        // if super global
233
        if (substr($group, 0, 1) === '_') {
234
            return $this->loadGlobal($group);
235
        }
236
237
        // load from config file
238
        $conf = $this->loader->load($group);
239
240
        foreach ($conf as $grp => $data) {
241
            $this->config->addNode($grp, $data);
242
        }
243
244
        return $this;
245
    }
246
247
    /**
248
     * Load super globals
249
     *
250
     * @param  string $group
251
     * @return $this
252
     * @throws LogicException if global unknown
253
     * @access protected
254
     */
255
    protected function loadGlobal(/*# string */ $group)
256
    {
257
        if (!isset($GLOBALS[$group])) {
258
            throw new LogicException(
259
                Message::get(Message::CONFIG_GLOBAL_UNKNOWN, $group),
260
                Message::CONFIG_GLOBAL_UNKNOWN
261
            );
262
        }
263
264
        // load super global
265
        $this->config->addNode($group, $GLOBALS[$group]);
266
267
        return $this;
268
    }
269
270
    /**
271
     * Get group name
272
     *
273
     * @param  string $id
274
     * @return string
275
     * @access protected
276
     */
277
    protected function getGroupName(/*# string */ $id)/*# : string */
278
    {
279
        // first field of the $id
280
        return explode($this->config->getDelimiter(), $id)[0];
281
    }
282
283
    /**
284
     * Override 'referenceLookup()' in ReferenceTrait.
285
     *
286
     * Delegator support goes here
287
     *
288
     * {@inheritDoc}
289
     */
290
    protected function referenceLookup(/*# string */ $name)
291
    {
292
        if ($this->hasDelegator()) {
293
            $delegator =  $this->getDelegator();
294
            if ($delegator instanceof ChainingInterface) {
295
                $val = $delegator->delegatedGet($name);
296
            } else {
297
                $val = $delegator->get($name);
298
            }
299
        } else {
300
            $val = $this->getReference($name);
301
        }
302
        return $val;
303
    }
304
305
    /**
306
     * throw exception if current $error_type is to throw exception
307
     *
308
     * {@inheritDoc}
309
     */
310
    protected function resolveUnknown(/*# string */ $name)
311
    {
312
        // warn if reference unknown
313
        $this->throwError(
314
            Message::get(Message::CONFIG_REFERENCE_UNKNOWN, $name),
315
            Message::CONFIG_REFERENCE_UNKNOWN
316
        );
317
318
        return null;
319
    }
320
321
    /**
322
     * {@inheritDoc}
323
     */
324
    protected function getReference(/*# string */ $name)
325
    {
326
        return $this->get($name);
327
    }
328
329
    /**
330
     * Dealing errors
331
     *
332
     * @param  string $message
333
     * @param  int $code
334
     * @return $this
335
     * @throws LogicException if current $error_type is to throw exception
336
     * @access protected
337
     */
338
    protected function throwError(/*# string */ $message, /*# int */ $code)
339
    {
340
        switch ($this->error_type) {
341
            case self::ERROR_WARNING:
342
                trigger_error($message, \E_USER_WARNING);
343
                break;
344
            case self::ERROR_EXCEPTION:
345
                throw new LogicException($message, $code);
346
            default:
347
                break;
348
        }
349
        return $this;
350
    }
351
}
352