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 ( 52d4c8...30de9c )
by Mohammad Abdoli
03:41
created

Config   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 279
Duplicated Lines 5.38 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 31
c 4
b 1
f 0
lcom 2
cbo 4
dl 15
loc 279
rs 9.8

16 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 14 3
A dump() 0 8 2
A set() 8 16 4
A get() 0 10 2
A has() 0 4 1
A registerEngine() 0 4 1
A getEngine() 0 11 3
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A getInternal() 7 17 4
A mergeConfig() 0 14 4
A serialize() 0 4 1
A unserialize() 0 4 1
A jsonSerialize() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Rad\Configure;
4
5
use ArrayAccess;
6
use JsonSerializable;
7
use Rad\Configure\Engine\PhpConfig;
8
use Rad\Core\SingletonTrait;
9
use Serializable;
10
11
/**
12
 * RadPHP Config
13
 *
14
 * Config is designed to simplify the access to, and the use of, configuration data within applications.
15
 *
16
 * @package Rad\Configure
17
 */
18
class Config implements ArrayAccess, Serializable, JsonSerializable
19
{
20
    use SingletonTrait;
21
22
    /**
23
     * Store config
24
     *
25
     * @var array
26
     */
27
    protected static $container = [];
28
29
    /**
30
     * Store registered engine
31
     *
32
     * @var EngineInterface[]
33
     */
34
    protected static $engines = [];
35
36
    /**
37
     * Load config
38
     *
39
     * @param mixed  $config     Config data for passed to engine load method
40
     * @param string $engineName Engine registered name
41
     * @param bool   $merge      Is config merged or overwrite
42
     *
43
     * @return bool
44
     */
45
    public static function load($config, $engineName = 'default', $merge = true)
46
    {
47
        if ($engine = self::getEngine($engineName)) {
48
            if ($merge) {
49
                self::mergeConfig($engine->load($config), self::$container);
50
            } else {
51
                self::$container = $engine->load($config);
52
            }
53
54
            return true;
55
        } else {
56
            return false;
57
        }
58
    }
59
60
    /**
61
     * Dump config
62
     *
63
     * @param  string $file       File path for save config
64
     * @param string  $engineName Engine name for use in dump
65
     *
66
     * @return bool
67
     */
68
    public static function dump($file, $engineName = 'default')
69
    {
70
        if ($engine = self::getEngine($engineName)) {
71
            return $engine->dump($file, self::$container);
72
        }
73
74
        return false;
75
    }
76
77
    /**
78
     * Set config
79
     *
80
     * @param string $identifier Parameter name.
81
     * @param mixed  $value      Value to set
82
     */
83
    public static function set($identifier, $value)
84
    {
85
        $ids = explode('.', $identifier);
86
        $base = &self::$container;
87
88 View Code Duplication
        while ($current = array_shift($ids)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89
            if (is_array($base) && array_key_exists($current, $base)) {
90
                $base = &$base[$current];
91
            } else {
92
                $base[$current] = [];
93
                $base = &$base[$current];
94
            }
95
        }
96
97
        $base = $value;
98
    }
99
100
    /**
101
     * Get config
102
     *
103
     * @param string $identifier Parameter name.
104
     * @param null   $default    Default value
105
     *
106
     * @return array|null
107
     */
108
    public static function get($identifier, $default = null)
109
    {
110
        $value = self::getInternal($identifier);
111
112
        if (is_null($value)) {
113
            return $default;
114
        }
115
116
        return $value;
117
    }
118
119
    /**
120
     * Indicates whether parameter exists or not
121
     *
122
     * @param string $identifier Parameter name.
123
     *
124
     * @return bool
125
     */
126
    public static function has($identifier)
127
    {
128
        return self::getInternal($identifier) !== null;
129
    }
130
131
    /**
132
     * Register engine
133
     *
134
     * @param string          $name   Engine name
135
     * @param EngineInterface $engine Engine instance
136
     */
137
    public static function registerEngine($name, EngineInterface $engine)
138
    {
139
        self::$engines[$name] = $engine;
140
    }
141
142
    /**
143
     * Get engine
144
     *
145
     * @param string $name Engine name
146
     *
147
     * @return bool|EngineInterface
148
     */
149
    protected static function getEngine($name)
150
    {
151
        if (!isset(self::$engines[$name])) {
152
            if ($name !== 'default') {
153
                return false;
154
            }
155
            self::registerEngine($name, new PhpConfig());
156
        }
157
158
        return self::$engines[$name];
159
    }
160
161
    /**
162
     * Whether a offset exists
163
     *
164
     * @param mixed $offset An offset to check for.
165
     *
166
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
167
     * @return boolean true on success or false on failure.
168
     * The return value will be casted to boolean if non-boolean was returned.
169
     */
170
    public function offsetExists($offset)
171
    {
172
        return $this->has($offset);
173
    }
174
175
    /**
176
     * Offset to retrieve
177
     *
178
     * @param mixed $offset The offset to retrieve.
179
     *
180
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
181
     * @return mixed Can return all value types.
182
     */
183
    public function offsetGet($offset)
184
    {
185
        return $this->getInternal($offset);
186
    }
187
188
    /**
189
     * Offset to set
190
     *
191
     * @param mixed $offset The offset to assign the value to.
192
     * @param mixed $value  The value to set.
193
     *
194
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
195
     * @return void
196
     */
197
    public function offsetSet($offset, $value)
198
    {
199
        self::set($offset, $value);
200
    }
201
202
    /**
203
     * Offset to unset
204
     *
205
     * @param mixed $offset The offset to unset.
206
     *
207
     * @throws Exception
208
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
209
     */
210
    public function offsetUnset($offset)
211
    {
212
        throw new Exception('Can not unset value');
213
    }
214
215
    /**
216
     * Retrieves parameter
217
     *
218
     * @param string $identifier Parameter name.
219
     *
220
     * @return array|null
221
     */
222
    protected static function getInternal($identifier)
223
    {
224
        $ids = explode('.', $identifier);
225
        $base = &self::$container;
226
227 View Code Duplication
        while ($current = array_shift($ids)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
228
            if (is_array($base) && array_key_exists($current, $base)) {
229
                $base = &$base[$current];
230
            } else {
231
                return null;
232
            }
233
        }
234
235
        $result = $base;
236
237
        return $result;
238
    }
239
240
    /**
241
     * Merge config
242
     *
243
     * @param mixed $newData
244
     * @param array $baseConfig
245
     */
246
    protected static function mergeConfig($newData, &$baseConfig)
247
    {
248
        if (is_array($newData)) {
249
            foreach ($newData as $key => $value) {
250
                if (isset($baseConfig[$key])) {
251
                    self::mergeConfig($value, $baseConfig[$key]);
252
                } else {
253
                    $baseConfig[$key] = $value;
254
                }
255
            }
256
        } else {
257
            $baseConfig = $newData;
258
        }
259
    }
260
261
    /**
262
     * String representation of object
263
     *
264
     * @link http://php.net/manual/en/serializable.serialize.php
265
     * @return string the string representation of the object or null
266
     */
267
    public function serialize()
268
    {
269
        return serialize(self::$container);
270
    }
271
272
    /**
273
     * Constructs the object
274
     *
275
     * @param string $serialized The string representation of the object.
276
     *
277
     * @link http://php.net/manual/en/serializable.unserialize.php
278
     * @return void
279
     */
280
    public function unserialize($serialized)
281
    {
282
        self::$container = unserialize($serialized);
283
    }
284
285
    /**
286
     * Specify data which should be serialized to JSON
287
     *
288
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
289
     * @return mixed data which can be serialized by "json_encode",
290
     *       which is a value of any type other than a resource.
291
     */
292
    public function jsonSerialize()
293
    {
294
        return self::$container;
295
    }
296
}
297