@@ -40,25 +40,25 @@ abstract class AbstractEnum
40
40
/**
41
41
* @var string The name of the enum constant
42
42
*/
43
- private $ name ;
43
+ private string $ name ;
44
44
45
45
/**
46
46
* @var array<string, array<string, string|int>> Cache for reflection data
47
47
*/
48
- private static $ cache = [];
48
+ private static array $ cache = [];
49
49
50
50
/**
51
51
* @var array<string, array<string, self>> Cache for enum instances
52
52
*/
53
- private static $ instances = [];
53
+ private static array $ instances = [];
54
54
55
55
/**
56
56
* Constructor is private to ensure instances are created through static methods
57
57
*
58
58
* @param string|int $value The enum value
59
59
* @param string $name The constant name
60
60
*/
61
- private function __construct ($ value , string $ name )
61
+ final private function __construct ($ value , string $ name )
62
62
{
63
63
$ this ->value = $ value ;
64
64
$ this ->name = $ name ;
@@ -71,7 +71,7 @@ private function __construct($value, string $name)
71
71
* @return mixed
72
72
* @throws BadMethodCallException If property doesn't exist
73
73
*/
74
- public function __get (string $ property )
74
+ final public function __get (string $ property )
75
75
{
76
76
if ($ property === 'value ' || $ property === 'name ' ) {
77
77
return $ this ->$ property ;
@@ -89,7 +89,7 @@ public function __get(string $property)
89
89
* @param mixed $value The value to set
90
90
* @throws BadMethodCallException Always, as enum properties are read-only
91
91
*/
92
- public function __set (string $ property , $ value ): void
92
+ final public function __set (string $ property , $ value ): void
93
93
{
94
94
throw new BadMethodCallException (
95
95
sprintf ('Cannot modify property %s::%s - enum properties are read-only ' , static ::class, $ property )
@@ -103,7 +103,7 @@ public function __set(string $property, $value): void
103
103
* @return static
104
104
* @throws InvalidArgumentException If the value is not valid
105
105
*/
106
- public static function from ($ value ): self
106
+ final public static function from ($ value ): self
107
107
{
108
108
$ instance = self ::tryFrom ($ value );
109
109
if ($ instance === null ) {
@@ -120,7 +120,7 @@ public static function from($value): self
120
120
* @param string|int $value The enum value
121
121
* @return static|null
122
122
*/
123
- public static function tryFrom ($ value ): ?self
123
+ final public static function tryFrom ($ value ): ?self
124
124
{
125
125
$ constants = self ::getConstants ();
126
126
foreach ($ constants as $ name => $ constantValue ) {
@@ -136,7 +136,7 @@ public static function tryFrom($value): ?self
136
136
*
137
137
* @return static[]
138
138
*/
139
- public static function cases (): array
139
+ final public static function cases (): array
140
140
{
141
141
$ cases = [];
142
142
$ constants = self ::getConstants ();
@@ -152,7 +152,7 @@ public static function cases(): array
152
152
* @param string|int|self $other The value or enum to compare
153
153
* @return bool
154
154
*/
155
- public function equals ($ other ): bool
155
+ final public function equals ($ other ): bool
156
156
{
157
157
if ($ other instanceof self) {
158
158
return $ this ->is ($ other );
@@ -167,7 +167,7 @@ public function equals($other): bool
167
167
* @param self $other The other enum to compare
168
168
* @return bool
169
169
*/
170
- public function is (self $ other ): bool
170
+ final public function is (self $ other ): bool
171
171
{
172
172
return $ this === $ other ; // Since we're using singletons, we can use identity comparison
173
173
}
@@ -177,7 +177,7 @@ public function is(self $other): bool
177
177
*
178
178
* @return array<string, string|int>
179
179
*/
180
- public static function getValues (): array
180
+ final public static function getValues (): array
181
181
{
182
182
return self ::getConstants ();
183
183
}
@@ -188,48 +188,33 @@ public static function getValues(): array
188
188
* @param string|int $value The value to check
189
189
* @return bool
190
190
*/
191
- public static function isValidValue ($ value ): bool
191
+ final public static function isValidValue ($ value ): bool
192
192
{
193
193
return in_array ($ value , self ::getValues (), true );
194
194
}
195
195
196
- /**
197
- * Create an enum instance from a value (deprecated, use from() instead)
198
- *
199
- * @param string|int $value The enum value
200
- * @return static
201
- * @throws InvalidArgumentException If the value is not valid
202
- * @deprecated Use from() method instead
203
- */
204
- public static function fromValue ($ value ): self
205
- {
206
- return self ::from ($ value );
207
- }
208
-
209
196
/**
210
197
* Get or create a singleton instance for the given value and name
211
198
*
212
199
* @param string|int $value The enum value
213
200
* @param string $name The constant name
214
201
* @return static
215
- * @phpstan-return static
216
202
*/
217
203
private static function getInstance ($ value , string $ name ): self
218
204
{
219
205
$ className = static ::class;
220
- $ key = $ name ;
221
206
222
207
if (!isset (self ::$ instances [$ className ])) {
223
208
self ::$ instances [$ className ] = [];
224
209
}
225
210
226
- if (!isset (self ::$ instances [$ className ][$ key ])) {
211
+ if (!isset (self ::$ instances [$ className ][$ name ])) {
227
212
$ instance = new $ className ($ value , $ name );
228
- self ::$ instances [$ className ][$ key ] = $ instance ;
213
+ self ::$ instances [$ className ][$ name ] = $ instance ;
229
214
}
230
215
231
216
/** @var static */
232
- return self ::$ instances [$ className ][$ key ];
217
+ return self ::$ instances [$ className ][$ name ];
233
218
}
234
219
235
220
/**
@@ -238,7 +223,7 @@ private static function getInstance($value, string $name): self
238
223
* @return array<string, string|int>
239
224
* @throws \RuntimeException If invalid constant found
240
225
*/
241
- protected static function getConstants (): array
226
+ final protected static function getConstants (): array
242
227
{
243
228
$ className = static ::class;
244
229
@@ -283,14 +268,14 @@ protected static function getConstants(): array
283
268
}
284
269
285
270
/**
286
- * Handle dynamic method calls for enum creation and checking
271
+ * Handle dynamic method calls for enum checking
287
272
*
288
273
* @param string $name The method name
289
274
* @param array<mixed> $arguments The method arguments
290
275
* @return bool
291
276
* @throws BadMethodCallException If the method doesn't exist
292
277
*/
293
- public function __call (string $ name , array $ arguments )
278
+ final public function __call (string $ name , array $ arguments ): bool
294
279
{
295
280
// Handle is* methods
296
281
if (strpos ($ name , 'is ' ) === 0 ) {
@@ -315,7 +300,7 @@ public function __call(string $name, array $arguments)
315
300
* @return static
316
301
* @throws BadMethodCallException If the method doesn't exist
317
302
*/
318
- public static function __callStatic (string $ name , array $ arguments )
303
+ final public static function __callStatic (string $ name , array $ arguments ): self
319
304
{
320
305
$ constantName = self ::camelCaseToConstant ($ name );
321
306
$ constants = self ::getConstants ();
@@ -349,7 +334,7 @@ private static function camelCaseToConstant(string $camelCase): string
349
334
*
350
335
* @return string
351
336
*/
352
- public function __toString (): string
337
+ final public function __toString (): string
353
338
{
354
339
return (string ) $ this ->value ;
355
340
}
0 commit comments