Skip to content

Clarify psalm types #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn't seem to match the title of the commit.

Copy link
Contributor

@jarstelfox jarstelfox Feb 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, somehow I messed these commit messages up! Fix incoming

Fixed

* @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