SplReflectionEnum::getCases()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

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
ccs 0
cts 2
cp 0
crap 2
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\SplEnumerable;
17
18
/**
19
 * The ReflectionEnum class reports information about an Enum.
20
 *
21
 * @template T of SplEnumerable
22
 * @extends \ReflectionClass<\Ducks\Component\SplTypes\SplEnumerable>
23
 *
24
 * @psalm-api
25
 *
26
 * @link https://php.net/manual/en/class.reflectionenum.php
27
 */
28
class SplReflectionEnum extends \ReflectionClass
29
{
30
    /**
31
     * Array of proxy
32
     *
33
     * @var SplReflectionEnumProxy[]
34
     *
35
     * @phpstan-var array<string, SplReflectionEnumProxy>
36
     */
37
    private static array $instances = [];
38
39
    /**
40
     * Return the ReflectionEnumProxy for class parsing
41
     *
42
     * @return SplReflectionEnumProxy
43
     *
44
     * @codeCoverageIgnore
45
     */
46
    private function getProxy(): SplReflectionEnumProxy
47
    {
48
        if (!isset(self::$instances[$this->name])) {
49
            self::$instances[$this->name] = new SplReflectionEnumProxy($this);
50
        }
51
52
        return self::$instances[$this->name];
53
    }
54
55
    /**
56
     * Instantiates a ReflectionEnum object
57
     *
58
     * @param object|string $objectOrClass
59
     *
60
     * @throws \ReflectionException if objectOrClass is not a SplEnumerable
61
     *
62
     * @phpstan-param T|class-string<T> $objectOrClass
63
     *
64
     * @psalm-pure
65
     *
66
     * @link https://www.php.net/manual/en/reflectionenum.construct.php
67
     */
68
    public function __construct($objectOrClass)
69
    {
70
        // Fast check
71
        if (!\is_a($objectOrClass, SplEnumerable::class, true)) {
72
            // @phpstan-ignore ternary.elseUnreachable
73
            $classname = \is_object($objectOrClass) ? \get_class($objectOrClass) : $objectOrClass;
74
            throw new \ReflectionException("Class \"$classname\" is not an enum");
75
        }
76
77
        parent::__construct($objectOrClass);
78
    }
79
80
    /**
81
     * Gets the backing type of an Enum, if any
82
     *
83
     * @return \ReflectionNamedType|null An instance of ReflectionNamedType, or null if the Enum has no backing type.
84
     *
85
     * @link https://www.php.net/manual/en/reflectionenum.getbackingtype.php
86
     */
87
    public function getBackingType(): ?\ReflectionNamedType
88
    {
89
        return $this->getProxy()->getBackingType();
90
    }
91
92
    /**
93
     * Returns a specific case of an Enum
94
     *
95
     * @param string $name
96
     *
97
     * @return SplReflectionEnumUnitCase|SplReflectionEnumBackedCase
98
     *
99
     * @throws \ReflectionException If the requested case is not defined
100
     *
101
     * @link https://www.php.net/manual/en/reflectionenum.getcase.php
102
     */
103
    public function getCase(string $name): SplReflectionEnumUnitCase
104
    {
105
        return $this->getProxy()->getCase($name);
106
    }
107
108
    /**
109
     * Returns a list of all cases on an Enum
110
     *
111
     * @return (SplReflectionEnumUnitCase|SplReflectionEnumBackedCase)[]
112
     *
113
     * @phpstan-return list<SplReflectionEnumUnitCase|SplReflectionEnumBackedCase>
114
     *
115
     * @link https://www.php.net/manual/en/reflectionenum.getcases.php
116
     */
117
    public function getCases(): array
118
    {
119
        return \array_values($this->getProxy()->getCases());
120
    }
121
122
    /**
123
     * Checks for a case on an Enum
124
     *
125
     * @param string $name The case to check for.
126
     *
127
     * @return boolean
128
     *
129
     * @link https://www.php.net/manual/en/reflectionenum.hascase.php
130
     */
131
    public function hasCase(string $name): bool
132
    {
133
        return $this->getProxy()->hasCase($name);
134
    }
135
136
    /**
137
     * Determines if an Enum is a Backed Enum
138
     *
139
     * @return boolean
140
     */
141
    public function isBacked(): bool
142
    {
143
        return $this->getProxy()->isBacked();
144
    }
145
}
146