1414 * @author Matthieu Napoli <matthieu@mnapoli.fr>
1515 * @author Daniel Costa <danielcosta@gmail.com>
1616 * @author Mirosław Filip <mirfilip@gmail.com>
17+ *
18+ * @template T
19+ * @psalm-immutable
1720 */
1821abstract class Enum implements \JsonSerializable
1922{
2023 /**
2124 * Enum value
2225 *
2326 * @var mixed
27+ * @psalm-var T
2428 */
2529 protected $ value ;
2630
2731 /**
2832 * Store existing constants in a static cache per object.
2933 *
3034 * @var array
35+ * @psalm-var array<class-string, array<string, mixed>>
3136 */
3237 protected static $ cache = [];
3338
@@ -36,6 +41,8 @@ abstract class Enum implements \JsonSerializable
3641 *
3742 * @param mixed $value
3843 *
44+ * @psalm-param T $value
45+ * @psalm-suppress InvalidCast
3946 * @throws \UnexpectedValueException if incompatible type is given.
4047 */
4148 public function __construct ($ value )
@@ -45,14 +52,15 @@ public function __construct($value)
4552 }
4653
4754 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 );
4956 }
5057
5158 $ this ->value = $ value ;
5259 }
5360
5461 /**
5562 * @return mixed
63+ * @psalm-return T
5664 */
5765 public function getValue ()
5866 {
@@ -62,6 +70,7 @@ public function getValue()
6270 /**
6371 * Returns the enum key (i.e. the constant name).
6472 *
73+ * @psalm-pure
6574 * @return mixed
6675 */
6776 public function getKey ()
@@ -70,6 +79,7 @@ public function getKey()
7079 }
7180
7281 /**
82+ * @psalm-suppress InvalidCast
7383 * @return string
7484 */
7585 public function __toString ()
@@ -83,13 +93,14 @@ public function __toString()
8393 *
8494 * This method is final, for more information read https://github.com/myclabs/php-enum/issues/4
8595 *
96+ * @psalm-param mixed $variable
8697 * @return bool
8798 */
8899 final public function equals ($ variable = null ): bool
89100 {
90101 return $ variable instanceof self
91102 && $ this ->getValue () === $ variable ->getValue ()
92- && \get_called_class () === \get_class ($ variable );
103+ && static ::class === \get_class ($ variable );
93104 }
94105
95106 /**
@@ -121,11 +132,14 @@ public static function values()
121132 /**
122133 * Returns all possible values as an array
123134 *
135+ * @psalm-pure
136+ * @psalm-return array<string, mixed>
124137 * @return array Constant name in key, constant value in value
125138 */
126139 public static function toArray ()
127140 {
128- $ class = \get_called_class ();
141+ $ class = static ::class;
142+
129143 if (!isset (static ::$ cache [$ class ])) {
130144 $ reflection = new \ReflectionClass ($ class );
131145 static ::$ cache [$ class ] = $ reflection ->getConstants ();
@@ -138,6 +152,7 @@ public static function toArray()
138152 * Check if is valid enum value
139153 *
140154 * @param $value
155+ * @psalm-param mixed $value
141156 *
142157 * @return bool
143158 */
@@ -150,6 +165,7 @@ public static function isValid($value)
150165 * Check if is valid enum key
151166 *
152167 * @param $key
168+ * @psalm-param string $key
153169 *
154170 * @return bool
155171 */
@@ -165,6 +181,8 @@ public static function isValidKey($key)
165181 *
166182 * @param $value
167183 *
184+ * @psalm-param mixed $value
185+ * @psalm-pure
168186 * @return mixed
169187 */
170188 public static function search ($ value )
@@ -188,7 +206,7 @@ public static function __callStatic($name, $arguments)
188206 return new static ($ array [$ name ]);
189207 }
190208
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 );
192210 }
193211
194212 /**
0 commit comments