diff --git a/src/Enum.php b/src/Enum.php index f5a5940..b8b9327 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -37,6 +37,14 @@ abstract class Enum implements \JsonSerializable */ protected static $cache = []; + /** + * Cache of instances of the Enum class + * + * @var array + * @psalm-var array> + */ + protected static $instances = []; + /** * Creates a new value of some type * @@ -211,17 +219,20 @@ public static function search($value) * @param array $arguments * * @return static - * @psalm-pure * @throws \BadMethodCallException */ public static function __callStatic($name, $arguments) { - $array = static::toArray(); - if (isset($array[$name]) || \array_key_exists($name, $array)) { - return new static($array[$name]); + $class = static::class; + if (!isset(self::$instances[$class][$name])) { + $array = static::toArray(); + if (!isset($array[$name]) && !\array_key_exists($name, $array)) { + $message = "No static method or enum constant '$name' in class " . static::class; + throw new \BadMethodCallException($message); + } + return self::$instances[$class][$name] = new static($array[$name]); } - - throw new \BadMethodCallException("No static method or enum constant '$name' in class " . static::class); + return clone self::$instances[$class][$name]; } /** diff --git a/tests/EnumTest.php b/tests/EnumTest.php index 9fe9022..cb08d29 100755 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -143,6 +143,7 @@ public function testStaticAccess() $this->assertEquals(new EnumFixture(EnumFixture::FOO), EnumFixture::FOO()); $this->assertEquals(new EnumFixture(EnumFixture::BAR), EnumFixture::BAR()); $this->assertEquals(new EnumFixture(EnumFixture::NUMBER), EnumFixture::NUMBER()); + $this->assertNotSame(EnumFixture::NUMBER(), EnumFixture::NUMBER()); } /**