Passed
Pull Request — master (#38)
by Teye
07:11
created

Enum::isValidValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
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