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 — dev-master ( 842215...468912 )
by Vijay
03:13
created

Enum::fixKeys()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 14
nc 7
nop 0
1
<?php
2
3
namespace vijinho\Enums;
4
5
/**
6
 * Abstract class for implementing ENUMs in PHP7
7
 *
8
 * @author Vijay Mahrra <[email protected]>
9
 * @copyright (c) Copyright 2016 Vijay Mahrra
10
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
11
 */
12
class Enum implements \Serializable
13
{
14
    /**
15
     * base enum values
16
     *
17
     * @var array $values
18
     */
19
    protected static $values = [];
20
21
    /**
22
     * allow keys to be unset/deleted?
23
     * cannot be over-ridden once defined in a class
24
     *
25
     * @var boolean
26
     */
27
    protected static $delete = false;
28
29
    /**
30
     * allow keys to be over-written when adding?
31
     * cannot be over-ridden once defined in a class
32
     *
33
     * @var boolean
34
     */
35
    protected static $overwrite = false;
36
37
    /**
38
     * always capitalize enum keys?
39
     * can be over-ridden once defined in a class
40
     * with capitalize(boolean) method
41
     *
42
     * @var boolean
43
     */
44
    protected static $capitalize = false;
45
46
    /**
47
     * case-sensitive check when searching by key for a value?
48
     * can be over-ridden once defined in a class
49
     * with caseSensitive(boolean) method
50
     *
51
     * @var boolean
52
     */
53
    protected static $caseSensitive = false;
54
55
56
    /**
57
     * initialize with array of additional values not existing already
58
     *
59
     * @param array static::$values
60
     */
61
    public function __construct(array $newValues = [])
62
    {
63
        static::fixKeys();
64
        static::add($newValues);
65
    }
66
67
68
    /**
69
     * Set case-sensitivity when searching
70
     *
71
     * @param boolean $bool
72
     */
73
    public static function caseSensitive($bool = false)
74
    {
75
        static::$caseSensitive = !empty($bool);
76
    }
77
78
79
    /**
80
     * Set capitalization for enum keys
81
     *
82
     * @param boolean $bool
83
     */
84
    public static function capitalize($bool = false)
85
    {
86
        static::$capitalize = !empty($bool);
87
        if (static::$capitalize) {
88
            static::$values = array_change_key_case(static::$values, CASE_UPPER);
89
        }
90
    }
91
92
93
    /**
94
     * Clear all data - use with care!
95
     *
96
     * @param boolean $caseSensitive
97
     * @param boolean $capitalize
98
     */
99
    public static function reset($caseSensitive = false, $capitalize = false)
100
    {
101
        static::$values = [];
102
        static::$caseSensitive = $caseSensitive;
103
        static::$capitalize = $capitalize;
104
    }
105
106
107
    /**
108
     * Remove an enum value - use with care!
109
     *
110
     */
111
    public static function delete($key)
112
    {
113
        $allowed = !empty(static::$delete);
114
        if (empty($allowed)) {
115
            throw new \LogicException('Method not allowed.');
116
        }
117
        $values = & static::$values;
118
        if (array_key_exists($key, $values)) {
119
            unset($values[$key]);
120
        } else {
121
            return false;
122
        }
123
        return true;
124
    }
125
126
127
    /**
128
     * Make sure that the enum keys have no SPACEs
129
     * Make sure the keys are strings
130
     * Set the case according to the settings
131
     *
132
     * @param return $values
0 ignored issues
show
Bug introduced by
There is no parameter named $values. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
133
     */
134
    public static function fixKeys()
135
    {
136
        $values = static::$values;
137
        foreach ($values as $k => $v) {
138
            unset($values[$k]);
139
            if (!is_string($k)) {
140
                if (!is_string($v)) {
141
                    throw new \UnexpectedValueException(sprintf("Key '%s' for value '%s' is not a string!", print_r($k,1), print_r($v,1)));
142
                } else {
143
                    // if the key is not a string, use the value if it is a string
144
                    $k = $v;
145
                }
146
            }
147
            $k = str_replace(' ', '_', $k); // space converted for magic calls
148
            $values[$k] = $v;
149
        }
150
151
        static::$values = empty(static::$capitalize) ?
152
            $values : array_change_key_case($values, CASE_UPPER);
153
154
        return static::$values;
155
    }
156
157
158
    /**
159
     * add array of extra values not existing already
160
     *
161
     * @param array|string $newValues
162
     * @param null|boolean $overwrite allow over-write of values?
163
     * @return array static::$values
164
     */
165
    public static function add($newValues, $overwrite = null): array
166
    {
167
        if (empty(static::$overwrite) && !empty($overwrite)) {
168
            throw new \LogicException('Overwrite not allowed.');
169
        }
170
171
        $values = & static::$values;
172
173
        // if it's a string, convert to array
174
        if (is_string($newValues)) {
175
            $newValues = [$newValues => $newValues];
176
        }
177
178
        foreach ($newValues as $k => $v) {
179
            $overwrite = (null == $overwrite) ? static::$overwrite : $overwrite;
180
            if (!empty($overwrite)) {
181
                $values[$k] = $v;
182
            } elseif (!array_key_exists($k, $values)) {
183
                $values[$k] = $v;
184
            }
185
        }
186
        return static::fixKeys();
187
    }
188
189
190
    /**
191
     * get array of keys
192
     *
193
     * @return array keys of static::$values
194
     */
195
    public static function keys(): array
196
    {
197
        return array_keys(static::$values);
198
    }
199
200
201
    /**
202
     * get key for the given value
203
     *
204
     * @param mixed $value
205
     * @param null|bool $caseSensitive search is case sensitive?
206
     * @return string|array key(s)
207
     */
208
    public static function key($value, $caseSensitive = null)
209
    {
210
        $values = static::$values;
211
        if (!is_array($value) && !is_object($value)) {
212
            if (is_string($value)) {
213
                $value = strtoupper($value);
214
                // if case-sensitivity is not specified use the class boolean value
215
                if (null === $caseSensitive) {
216
                    $caseSensitive = static::$caseSensitive;
217
                }
218
                if (empty($caseSensitive)) {
219
                    $values = array_map(function($value) {
220
                        return strtoupper($value);
221
                    }, $values);
222
                }
223
            }
224
            $keys = array_keys($values, $value);
225
            $count = count($keys);
226
            if (0 === $count) {
227
                throw new \InvalidArgumentException(sprintf("Key for value '%s' does not exist.", print_r($value, 1)));
228
            }
229
            return count($keys) > 1 ? $keys : $keys[0];
230
        } elseif (is_array($value)) {
231
            $search = array_search($value, $values);
232
            if (false === $search) {
233
                throw new \InvalidArgumentException(sprintf("Key for value '%s' does not exist.", print_r($value, 1)));
234
            }
235
            return $search;
236
        }
237
    }
238
239
240
    /**
241
     * count values
242
     *
243
     * @return int number of values
244
     */
245
    public static function count(): int
246
    {
247
        return count(static::$values);
248
    }
249
250
251
    /**
252
     * count values
253
     *
254
     * @return int number of values
255
     */
256
    public static function sizeof(): int
257
    {
258
        return static::count();
259
    }
260
261
262
    /**
263
     * get existing values
264
     *
265
     * @return array static::$values
266
     */
267
    public static function values(): array
268
    {
269
        return static::$values;
270
    }
271
272
273
    /**
274
     * get value for the given key
275
     *
276
     * @param string $key
277
     * @param null|bool $caseSensitive search is case sensitive?
278
     * @return mixed static::$values[$key]
279
     */
280
    public static function value($key, $caseSensitive = null)
281
    {
282
        $values = static::$values;
283
        // if case-sensitivity is not specified use the class boolean value
284
        if (null === $caseSensitive) {
285
            $caseSensitive = static::$caseSensitive;
286
        }
287
        if (empty($caseSensitive)) {
288
            $key = strtoupper($key);
289
            $values = array_change_key_case($values, CASE_UPPER);
290
        }
291
        if (false === array_key_exists($key, $values)) {
292
            throw new \InvalidArgumentException(sprintf("Value for key '%s' does not exist.", print_r($key, 1)));
293
        }
294
        return $values[$key];
295
    }
296
297
298
    /**
299
     * get value for the given key
300
     * method allows getting value from an object with $object->key
301
     *
302
     * @return mixed static::$values[$key]
303
     * @link http://php.net/manual/en/language.oop5.overloading.php#object.get
304
     */
305
    public function __get(string $key) {
306
        return static::value($key);
307
    }
308
309
310
    /**
311
     * get value named the same as the method called
312
     * method allows getting value from an object with $object->key()
313
     *
314
     * @param string $key the method called is used as the key
315
     * @param array $args
316
     * @return mixed static::$values[$key]
317
     * @link http://php.net/manual/en/language.oop5.overloading.php#object.call
318
     */
319
    public function __call(string $key, array $args = []) {
320
        return static::value($key);
321
    }
322
323
324
    /**
325
     * get value named the same as the method called statically
326
     * method allows getting value from an object with $object::key()
327
     *
328
     * @param string $key the method called is used as the key
329
     * @param array $args
330
     * @return mixed static::$values[$key]
331
     * @link http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
332
     */
333
    public static function __callStatic(string $key, array $args = []) {
334
        return static::value($key);
335
    }
336
337
338
    /**
339
     * check if the given key exists via call to $object of isset($object->key)
340
     *
341
     * @param string $key
342
     * @return boolean
343
     * @link http://php.net/manual/en/language.oop5.overloading.php#object.isset
344
     */
345
    public function __isset($key)
346
    {
347
        return empty(static::$caseSensitive) ?
348
            array_key_exists(strtoupper($key), array_change_key_case(static::$values, CASE_UPPER)) : array_key_exists($key, static::$values);
349
    }
350
351
352
    /**
353
     * when called as a function this class will add new values and return the result
354
     *
355
     * @param array $newValues
356
     * @return boolean
357
     * @link http://php.net/manual/en/language.oop5.overloading.php#object.invoke
358
     */
359
    public function __invoke($newValues)
360
    {
361
        return static::add($newValues);
362
    }
363
364
365
    /**
366
     * return the values as a string
367
     * method allows outputting values as a string
368
     *
369
     * @return string json_encode(static::$values)
370
     * @link http://php.net/manual/en/language.oop5.magic.php#object.tostring
371
     */
372
    public function __toString(): string
373
    {
374
        return json_encode(static::$values, JSON_PRETTY_PRINT);
375
    }
376
377
378
    /**
379
     * return the values as a string
380
     * method allows outputting values as a string when called statically
381
     *
382
     * @return string json_encode(static::$values)
383
     */
384
    public static function toString(): string
385
    {
386
        return json_encode(static::$values, JSON_PRETTY_PRINT);
387
    }
388
389
390
    /**
391
     * serialize enum values
392
     *
393
     * @return string enum values serialized
394
     * @link http://php.net/manual/en/class.serializable.php
395
     */
396
    public function serialize(): string
397
    {
398
        return serialize(static::$values);
399
    }
400
401
    /**
402
     * unserialize serialized values to object
403
     *
404
     * @return string enum values serialized
405
     * @link http://php.net/manual/en/class.serializable.php
406
     * @return void
407
     */
408
    public function unserialize($data) {
409
        $data = unserialize($data);
410
        static::$values = $data;
411
    }
412
413
414
    /**
415
     * returned dump values as array
416
     *
417
     * @return array
418
     */
419
    public static function var_dump(): array
420
    {
421
        $delete = static::$delete;
422
        $overwrite = static::$overwrite;
423
        $capitalize = static::$capitalize;
424
        $caseSensitive = static::$caseSensitive;
425
        $values = static::$values;
426
        return [
427
            'overwrite' => $overwrite,
428
            'delete' => $delete,
429
            'capitalise' => $capitalize,
430
            'caseSensitive' => $caseSensitive,
431
            'values' => $values
432
        ];
433
    }
434
435
436
    /**
437
     * returned values when called with var_dump()
438
     *
439
     * @return array debug info
440
     * @link http://php.net/manual/en/language.oop5.magic.php#object.debuginfo
441
     */
442
    public function __debugInfo(): array
443
    {
444
        return static::var_dump();
445
    }
446
}
447