Completed
Pull Request — master (#4)
by Pavel
04:03
created

StaticEnum::getConstantReflection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Paillechat\Enum;
4
5
abstract class StaticEnum extends Enum
6
{
7
    /** @var StaticEnum[][] */
8
    private static $instances = [];
9
10
    private static $constReflections = [];
11
12 4
    final protected static function createNamedInstance(string $name, $value)
13
    {
14 4
        $class = self::findParentClassForConst($name);
15
16 4
        $key = self::getConstKey($class, $name);
17
18 4
        if (!array_key_exists($key, self::$instances)) {
19 3
            self::$instances[$key] = parent::createNamedInstance($name, $value);
20
        }
21
22 4
        return self::$instances[$key];
0 ignored issues
show
Bug Compatibility introduced by
The expression self::$instances[$key]; of type Paillechat\Enum\StaticEn...llechat\Enum\StaticEnum adds the type Paillechat\Enum\StaticEnum[] to the return on line 22 which is incompatible with the return type of the parent method Paillechat\Enum\Enum::createNamedInstance of type Paillechat\Enum\Enum.
Loading history...
23
    }
24
25 4
    private static function getConstantReflection(string $class, string $name): \ReflectionClassConstant
26
    {
27 4
        $key = self::getConstKey($class, $name);
28 4
        if (!array_key_exists($key, self::$constReflections)) {
29 4
            $refl = self::getEnumReflection(static::class);
30
31 4
            self::$constReflections[$key] = $refl->getReflectionConstant($name);
32
        }
33
34 4
        return self::$constReflections[$key];
35
    }
36
37 4
    private static function getConstKey(string $class, string $name): string
38
    {
39 4
        return $class.'::'.$name;
40
    }
41
42 4
    private static function findParentClassForConst(string $name): string
43
    {
44 4
        return self::getConstantReflection(static::class, $name)->getDeclaringClass()->getName();
45
    }
46
}
47