Skip to content

Commit

Permalink
Merge pull request #112 from iFixit/clarify-psalm-types
Browse files Browse the repository at this point in the history
Clarify psalm types
  • Loading branch information
mnapoli authored Feb 13, 2020
2 parents c3f3b36 + fdb1bd5 commit aef7b9f
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ abstract class Enum implements \JsonSerializable
/**
* Store existing constants in a static cache per object.
*
*
* @var array
* @psalm-var array<class-string, array<string, mixed>>
*/
Expand All @@ -39,26 +40,30 @@ abstract class Enum implements \JsonSerializable
/**
* Creates a new value of some type
*
* @psalm-pure
* @param mixed $value
*
* @psalm-param T $value
* @psalm-suppress InvalidCast
* @psalm-param static<T>|T $value
* @throws \UnexpectedValueException if incompatible type is given.
*/
public function __construct($value)
{
if ($value instanceof static) {
/** @psalm-var T */
$value = $value->getValue();
}

if (!$this->isValid($value)) {
/** @psalm-suppress InvalidCast */
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . static::class);
}

/** @psalm-var T */
$this->value = $value;
}

/**
* @psalm-pure
* @return mixed
* @psalm-return T
*/
Expand All @@ -79,6 +84,7 @@ public function getKey()
}

/**
* @psalm-pure
* @psalm-suppress InvalidCast
* @return string
*/
Expand All @@ -93,6 +99,7 @@ public function __toString()
*
* This method is final, for more information read https://github.com/myclabs/php-enum/issues/4
*
* @psalm-pure
* @psalm-param mixed $variable
* @return bool
*/
Expand All @@ -106,6 +113,8 @@ final public function equals($variable = null): bool
/**
* Returns the names (keys) of all constants in the Enum class
*
* @psalm-pure
* @psalm-return list<string>
* @return array
*/
public static function keys()
Expand All @@ -116,12 +125,15 @@ public static function keys()
/**
* Returns instances of the Enum class of all Enum constants
*
* @psalm-pure
* @psalm-return array<string, static>
* @return static[] Constant name in key, Enum instance in value
*/
public static function values()
{
$values = array();

/** @psalm-var T $value */
foreach (static::toArray() as $key => $value) {
$values[$key] = new static($value);
}
Expand All @@ -133,6 +145,8 @@ public static function values()
* Returns all possible values as an array
*
* @psalm-pure
* @psalm-suppress ImpureStaticProperty
*
* @psalm-return array<string, mixed>
* @return array Constant name in key, constant value in value
*/
Expand All @@ -153,7 +167,7 @@ public static function toArray()
*
* @param $value
* @psalm-param mixed $value
*
* @psalm-pure
* @return bool
*/
public static function isValid($value)
Expand All @@ -166,7 +180,7 @@ public static function isValid($value)
*
* @param $key
* @psalm-param string $key
*
* @psalm-pure
* @return bool
*/
public static function isValidKey($key)
Expand Down Expand Up @@ -197,6 +211,7 @@ public static function search($value)
* @param array $arguments
*
* @return static
* @psalm-pure
* @throws \BadMethodCallException
*/
public static function __callStatic($name, $arguments)
Expand All @@ -215,6 +230,7 @@ public static function __callStatic($name, $arguments)
*
* @return mixed
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @psalm-pure
*/
public function jsonSerialize()
{
Expand Down

0 comments on commit aef7b9f

Please sign in to comment.