Test Failed
Push — 7.x ( bea7e1...2b11c5 )
by Adrien
08:51
created

SplReflectionEnum::getCases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Part of SplTypes package.
5
 *
6
 * (c) Adrien Loyant <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Ducks\Component\SplTypes\Reflection;
15
16
use Ducks\Component\SplTypes\SplUnitEnum;
17
18
/**
19
 * The ReflectionEnum class reports information about an Enum.
20
 *
21
 * @link https://php.net/manual/en/class.reflectionenum.php
22
 */
23
class SplReflectionEnum extends \ReflectionClass
24
{
25
    /**
26
     * Array of proxy
27
     *
28
     * @var array<int, SplReflectionEnumProxy>
29
     */
30
    private static array $instances = [];
31
32
    /**
33
     * Return the ReflectionEnumProxy for class parsing
34
     *
35
     * @return SplReflectionEnumProxy
36
     *
37
     * @codeCoverageIgnore
38
     */
39
    private function getProxy(): SplReflectionEnumProxy
40
    {
41
        if (!isset(static::$instances[$this->name])) {
0 ignored issues
show
Bug introduced by
Since $instances is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $instances to at least protected.
Loading history...
42
            static::$instances[$this->name] = new SplReflectionEnumProxy($this);
43
        }
44
45
        return static::$instances[$this->name];
46
    }
47
48
    /**
49
     * Instantiates a ReflectionEnum object
50
     *
51
     * @param object|string $objectOrClass
52
     *
53
     * @throws ReflectionException if objectOrClass is not a SplUnitEnum
54
     *
55
     * @link https://www.php.net/manual/en/reflectionenum.construct.php
56
     */
57
    public function __construct($objectOrClass)
58
    {
59
        // Fast check
60
        if (!\is_a($objectOrClass, SplUnitEnum::class, true)) {
61
            $classname = \is_object($objectOrClass) ? \get_class($objectOrClass) : $objectOrClass;
62
            throw new \ReflectionException("Class \"$classname\" is not an enum");
63
        }
64
65
        parent::__construct($objectOrClass);
66
    }
67
68
    /**
69
     * Gets the backing type of an Enum, if any
70
     *
71
     * @return \ReflectionNamedType|null An instance of ReflectionNamedType, or null if the Enum has no backing type.
72
     *
73
     * @link https://www.php.net/manual/en/reflectionenum.getbackingtype.php
74
     */
75
    public function getBackingType(): ?\ReflectionNamedType
76
    {
77
        return $this->getProxy()->getBackingType();
78
    }
79
80
    /**
81
     * Returns a specific case of an Enum
82
     *
83
     * @param string $name
84
     *
85
     * @return SplReflectionEnumUnitCase|SplReflectionEnumBackedCase
86
     *
87
     * @throws \ReflectionException If the requested case is not defined
88
     *
89
     * @link https://www.php.net/manual/en/reflectionenum.getcase.php
90
     */
91
    public function getCase(string $name): SplReflectionEnumUnitCase
92
    {
93
        return $this->getProxy()->getCase($name);
94
    }
95
96
    /**
97
     * Returns a list of all cases on an Enum
98
     *
99
     * @return array<int, SplReflectionEnumUnitCase|SplReflectionEnumBackedCase>
100
     *
101
     * @link https://www.php.net/manual/en/reflectionenum.getcases.php
102
     */
103
    public function getCases(): array
104
    {
105
        return $this->getProxy()->getCases();
106
    }
107
108
    /**
109
     * Checks for a case on an Enum
110
     *
111
     * @param string $name The case to check for.
112
     *
113
     * @return boolean
114
     *
115
     * @link https://www.php.net/manual/en/reflectionenum.hascase.php
116
     */
117
    public function hasCase(string $name): bool
118
    {
119
        return $this->getProxy()->hasCase($name);
120
    }
121
122
    /**
123
     * Determines if an Enum is a Backed Enum
124
     *
125
     * @return boolean
126
     */
127
    public function isBacked(): bool
128
    {
129
        return $this->getProxy()->isBacked();
130
    }
131
}
132