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 ( 3c112e...7b66c6 )
by Hong
02:53
created

Config::setError()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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