Passed
Pull Request — master (#38)
by Teye
05:43
created

Enum::getConstants()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 17
ccs 0
cts 0
cp 0
rs 9.9332
cc 4
nc 8
nop 0
crap 20
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Types;
5
6
use ReflectionClass;
7
use ReflectionException;
8
9
abstract class Enum
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_ENUM, expecting T_STRING on line 9 at column 15
Loading history...
10
{
11
    /**
12
     * @var array<string,array<string,scalar>>
13
     */
14
    protected static array $constCacheArray = [];
15
16
    /**
17
     * @return array<string,scalar>
18
     * @codeCoverageIgnore
19
     */
20
    protected static function getConstants(): array
21
    {
22
        $calledClass = get_called_class();
23
        if (!array_key_exists($calledClass, self::$constCacheArray)) {
24
            try {
25
                $reflect = new ReflectionClass($calledClass);
26
27
                /** @var array<string,scalar> $values */
28
                $values = $reflect->getConstants();
29
30
                self::$constCacheArray[$calledClass] = $values;
31
            } catch (ReflectionException $e) {
32
                return [];
33
            }
34
        }
35
36
        return self::$constCacheArray[$calledClass];
37
    }
38
39
    /**
40
     * @param string $value
41
     *
42
     * @return bool
43
     */
44 510
    public static function isValidValue(string $value): bool
45
    {
46 510
        $values = array_values(self::getConstants());
47
48 510
        return in_array($value, $values, true);
49
    }
50
51
    /**
52
     * Return all values
53
     *
54
     * @return array<scalar>
55
     */
56 24
    public static function values(): array
57
    {
58 24
        return array_values(self::getConstants());
59
    }
60
}
61