14
14
* @author Matthieu Napoli <[email protected] >
15
15
* @author Daniel Costa <[email protected] >
16
16
* @author Mirosław Filip <[email protected] >
17
+ *
18
+ * @template T
19
+ * @psalm-immutable
17
20
*/
18
21
abstract class Enum implements \JsonSerializable
19
22
{
20
23
/**
21
24
* Enum value
22
25
*
23
26
* @var mixed
27
+ * @psalm-var T
24
28
*/
25
29
protected $ value ;
26
30
27
31
/**
28
32
* Store existing constants in a static cache per object.
29
33
*
30
34
* @var array
35
+ * @psalm-var array<class-string, array<string, mixed>>
31
36
*/
32
37
protected static $ cache = [];
33
38
@@ -36,6 +41,8 @@ abstract class Enum implements \JsonSerializable
36
41
*
37
42
* @param mixed $value
38
43
*
44
+ * @psalm-param T $value
45
+ * @psalm-suppress InvalidCast
39
46
* @throws \UnexpectedValueException if incompatible type is given.
40
47
*/
41
48
public function __construct ($ value )
@@ -45,14 +52,15 @@ public function __construct($value)
45
52
}
46
53
47
54
if (!$ this ->isValid ($ value )) {
48
- throw new \UnexpectedValueException ("Value ' $ value' is not part of the enum " . \get_called_class () );
55
+ throw new \UnexpectedValueException ("Value ' $ value' is not part of the enum " . static ::class );
49
56
}
50
57
51
58
$ this ->value = $ value ;
52
59
}
53
60
54
61
/**
55
62
* @return mixed
63
+ * @psalm-return T
56
64
*/
57
65
public function getValue ()
58
66
{
@@ -62,6 +70,7 @@ public function getValue()
62
70
/**
63
71
* Returns the enum key (i.e. the constant name).
64
72
*
73
+ * @psalm-pure
65
74
* @return mixed
66
75
*/
67
76
public function getKey ()
@@ -70,6 +79,7 @@ public function getKey()
70
79
}
71
80
72
81
/**
82
+ * @psalm-suppress InvalidCast
73
83
* @return string
74
84
*/
75
85
public function __toString ()
@@ -83,13 +93,14 @@ public function __toString()
83
93
*
84
94
* This method is final, for more information read https://github.com/myclabs/php-enum/issues/4
85
95
*
96
+ * @psalm-param mixed $variable
86
97
* @return bool
87
98
*/
88
99
final public function equals ($ variable = null ): bool
89
100
{
90
101
return $ variable instanceof self
91
102
&& $ this ->getValue () === $ variable ->getValue ()
92
- && \get_called_class () === \get_class ($ variable );
103
+ && static ::class === \get_class ($ variable );
93
104
}
94
105
95
106
/**
@@ -121,11 +132,14 @@ public static function values()
121
132
/**
122
133
* Returns all possible values as an array
123
134
*
135
+ * @psalm-pure
136
+ * @psalm-return array<string, mixed>
124
137
* @return array Constant name in key, constant value in value
125
138
*/
126
139
public static function toArray ()
127
140
{
128
- $ class = \get_called_class ();
141
+ $ class = static ::class;
142
+
129
143
if (!isset (static ::$ cache [$ class ])) {
130
144
$ reflection = new \ReflectionClass ($ class );
131
145
static ::$ cache [$ class ] = $ reflection ->getConstants ();
@@ -138,6 +152,7 @@ public static function toArray()
138
152
* Check if is valid enum value
139
153
*
140
154
* @param $value
155
+ * @psalm-param mixed $value
141
156
*
142
157
* @return bool
143
158
*/
@@ -150,6 +165,7 @@ public static function isValid($value)
150
165
* Check if is valid enum key
151
166
*
152
167
* @param $key
168
+ * @psalm-param string $key
153
169
*
154
170
* @return bool
155
171
*/
@@ -165,6 +181,8 @@ public static function isValidKey($key)
165
181
*
166
182
* @param $value
167
183
*
184
+ * @psalm-param mixed $value
185
+ * @psalm-pure
168
186
* @return mixed
169
187
*/
170
188
public static function search ($ value )
@@ -188,7 +206,7 @@ public static function __callStatic($name, $arguments)
188
206
return new static ($ array [$ name ]);
189
207
}
190
208
191
- throw new \BadMethodCallException ("No static method or enum constant ' $ name' in class " . \get_called_class () );
209
+ throw new \BadMethodCallException ("No static method or enum constant ' $ name' in class " . static ::class );
192
210
}
193
211
194
212
/**
0 commit comments