From c3eff2e3b42cefd9b42cddba01891f1fb8440b53 Mon Sep 17 00:00:00 2001 From: Ilya Tregubov Date: Tue, 19 Jan 2021 10:02:00 +0200 Subject: [PATCH] MDL-70319 core: Upgrade php-enum to 1.7.7 --- lib/php-enum/src/Enum.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/php-enum/src/Enum.php b/lib/php-enum/src/Enum.php index f5a59404a04..b8b93277e2c 100644 --- a/lib/php-enum/src/Enum.php +++ b/lib/php-enum/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 @@ abstract class Enum implements \JsonSerializable * @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]; } /**