@@ -37,6 +37,14 @@ abstract class Enum implements \JsonSerializable
37
37
*/
38
38
protected static $ cache = [];
39
39
40
+ /**
41
+ * Cache of instances of the Enum class
42
+ *
43
+ * @var array
44
+ * @psalm-var array<class-string, array<string, static>>
45
+ */
46
+ protected static $ instances = [];
47
+
40
48
/**
41
49
* Creates a new value of some type
42
50
*
@@ -211,17 +219,20 @@ public static function search($value)
211
219
* @param array $arguments
212
220
*
213
221
* @return static
214
- * @psalm-pure
215
222
* @throws \BadMethodCallException
216
223
*/
217
224
public static function __callStatic ($ name , $ arguments )
218
225
{
219
- $ array = static ::toArray ();
220
- if (isset ($ array [$ name ]) || \array_key_exists ($ name , $ array )) {
221
- return new static ($ array [$ name ]);
226
+ $ class = static ::class;
227
+ if (!isset (self ::$ instances [$ class ][$ name ])) {
228
+ $ array = static ::toArray ();
229
+ if (!isset ($ array [$ name ]) && !\array_key_exists ($ name , $ array )) {
230
+ $ message = "No static method or enum constant ' $ name' in class " . static ::class;
231
+ throw new \BadMethodCallException ($ message );
232
+ }
233
+ return self ::$ instances [$ class ][$ name ] = new static ($ array [$ name ]);
222
234
}
223
-
224
- throw new \BadMethodCallException ("No static method or enum constant ' $ name' in class " . static ::class);
235
+ return clone self ::$ instances [$ class ][$ name ];
225
236
}
226
237
227
238
/**
0 commit comments