AbstractEnum::values()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the tmilos/value package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Tmilos\Value;
13
14
abstract class AbstractEnum extends AbstractValue implements Enum
15
{
16
    /** @var array */
17
    private static $constantsCache = [];
18
19
    /** @var array */
20
    private static $reflectionClassCache = [];
21
22
    /**
23
     * Returns array of all Enum instances index by their values.
24
     *
25
     * @return Enum[] Constant name => Enum
26
     */
27
    public static function all()
28
    {
29
        $result = [];
30
        foreach (self::getConstants() as $k => $v) {
31
            if (static::isValid($v)) {
32
                /** @var Value $object */
33
                $object = new static($v);
34
                $result[$object->getValue()] = $object;
35
            }
36
        }
37
38
        return $result;
39
    }
40
41
    /**
42
     * Returns array of constant values.
43
     *
44
     * @return array
45
     */
46
    public static function values()
47
    {
48
        return array_values(self::getConstants());
49
    }
50
51
    /**
52
     * Check if is valid enum value.
53
     *
54
     * @param $value
55
     *
56
     * @return bool
57
     */
58
    public static function isValid($value)
59
    {
60
        return in_array($value, self::getConstants());
61
    }
62
63
    /**
64
     * Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant.
65
     *
66
     * @param string $name
67
     * @param array  $arguments
68
     *
69
     * @return static
70
     *
71
     * @throws \BadMethodCallException
72
     */
73
    public static function __callStatic($name, array $arguments = [])
74
    {
75
        $array = self::getConstants();
76
        if (array_key_exists($name, $array)) {
77
            $reflectionClass = self::getReflectionClass();
78
            array_unshift($arguments, $array[$name]);
79
80
            return $reflectionClass->newInstanceArgs($arguments);
81
        }
82
83
        throw new \BadMethodCallException(sprintf('No static method or enum constant "%s" in class "%s"', $name, get_called_class()));
84
    }
85
86
    /**
87
     * @return \ReflectionClass
88
     */
89 View Code Duplication
    private static function getReflectionClass()
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...
90
    {
91
        $class = get_called_class();
92
        if (!array_key_exists($class, self::$reflectionClassCache)) {
93
            self::inspect($class);
94
        }
95
96
        return self::$reflectionClassCache[$class];
97
    }
98
99 View Code Duplication
    private static function getConstants()
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...
100
    {
101
        $class = get_called_class();
102
        if (!array_key_exists($class, self::$constantsCache)) {
103
            self::inspect($class);
104
        }
105
106
        return self::$constantsCache[$class];
107
    }
108
109
    private static function inspect($class)
110
    {
111
        if (!array_key_exists($class, self::$reflectionClassCache)) {
112
            $reflection = new \ReflectionClass($class);
113
            self::$reflectionClassCache[$class] = $reflection;
114
            self::$constantsCache[$class] = $reflection->getConstants();
115
        }
116
    }
117
}
118