Completed
Pull Request — master (#5)
by Pavel
05:35
created

Enum::getEnumReflection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Paillechat\Enum;
4
5
use Paillechat\Enum\Exception\EnumException;
6
7
abstract class Enum
8
{
9
    /** @var Enum[] */
10
    private static $instances = [];
11
    /** @var \ReflectionClassConstant[] */
12
    private static $constReflections = [];
13
    /** @var \ReflectionClass[] */
14
    private static $reflections = [];
15
    /** @var mixed */
16
    private $value;
17
    /** @var string */
18
    private $name;
19
20
    /**
21
     * @param string $name
22
     * @param mixed $value
23
     */
24 3
    final private function __construct(string $name, $value)
25
    {
26 3
        $this->name = $name;
27 3
        $this->value = $value;
28 3
    }
29
30
    /**
31
     * Creates enum instance with short static constructor
32
     *
33
     * @param string $name
34
     * @param array $arguments
35
     *
36
     * @return static
37
     *
38
     * @throws \BadMethodCallException
39
     * @throws EnumException
40
     */
41 6
    final public static function __callStatic(string $name, array $arguments)
42
    {
43 6
        $const = static::getConstList();
0 ignored issues
show
Bug introduced by
Since getConstList() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getConstList() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
44
45 6
        if (!array_key_exists($name, $const)) {
46 1
            throw EnumException::becauseUnknownMember(static::class, $name);
47
        }
48
49 5
        return static::createNamedInstance($name, $const[$name]);
0 ignored issues
show
Bug introduced by
Since createNamedInstance() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createNamedInstance() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
50
    }
51
52 5 View Code Duplication
    private static function getConstantReflection(string $class, string $name): \ReflectionClassConstant
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
53
    {
54 5
        $key = self::getConstKey($class, $name);
55 5
        if (!array_key_exists($key, self::$constReflections)) {
56 4
            $refl = self::getEnumReflection(static::class);
57
58 4
            self::$constReflections[$key] = $refl->getReflectionConstant($name);
59
        }
60
61 5
        return self::$constReflections[$key];
62
    }
63
64 5
    private static function getConstKey(string $class, string $name): string
65
    {
66 5
        return $class.'::'.$name;
67
    }
68
69 5
    private static function findParentClassForConst(string $name): string
70
    {
71 5
        return self::getConstantReflection(static::class, $name)->getDeclaringClass()->getName();
72
    }
73
74 6
    private static function getConstList(): array
75
    {
76 6
        return self::getEnumReflection(static::class)->getConstants();
77
    }
78
79 6
    private static function getEnumReflection(string $class): \ReflectionClass
80
    {
81 6
        if (!array_key_exists($class, self::$reflections)) {
82 3
            self::$reflections[$class] = new \ReflectionClass($class);
83
        }
84
85 6
        return self::$reflections[$class];
86
    }
87
88
    /**
89
     * Create named enum instance
90
     *
91
     * @param string $name
92
     * @param mixed $value
93
     *
94
     * @return static
95
     */
96 5 View Code Duplication
    private static function createNamedInstance(string $name, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
97
    {
98 5
        $class = self::findParentClassForConst($name);
99
100 5
        $key = self::getConstKey($class, $name);
101
102 5
        if (!array_key_exists($key, self::$instances)) {
103 3
            self::$instances[$key] = new static($name, $value);
104
        }
105
106 5
        return self::$instances[$key];
107
    }
108
109
    /**
110
     * @return mixed
111
     */
112 1
    final public function getValue()
113
    {
114 1
        return $this->value;
115
    }
116
117 1
    final public function getName(): string
118
    {
119 1
        return $this->name;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 1
    public function __toString(): string
126
    {
127 1
        return (string)$this->getValue();
128
    }
129
}
130