Skip to content

Commit

Permalink
Mark all methods as pure
Browse files Browse the repository at this point in the history
This means every function can be called from an immutable / pure context

Note: ToArray will fail psalm on this commit as (rightfully) there is an impure psalm issue

This commit is a bit of a lie to psalm: Psalm is correct that our object is not actually immutable / pure.

But is if you remove caching.

This lie seems tolerable (despite how bad a practice overriding the typechecker is) because the mutation is entirely memoization.
  • Loading branch information
jarstelfox committed Feb 12, 2020
1 parent 27aa6b1 commit 3d5c198
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ abstract class Enum implements \JsonSerializable
/**
* Store existing constants in a static cache per object.
*
* @psalm-pure
*
* @var array
* @psalm-var array<class-string, array<string, mixed>>
*/
Expand All @@ -39,6 +41,7 @@ abstract class Enum implements \JsonSerializable
/**
* Creates a new value of some type
*
* @psalm-pure
* @param mixed $value
*
* @psalm-param static<T>|T $value
Expand All @@ -61,6 +64,7 @@ public function __construct($value)
}

/**
* @psalm-pure
* @return mixed
* @psalm-return T
*/
Expand All @@ -72,7 +76,7 @@ public function getValue()
/**
* Returns the enum key (i.e. the constant name).
*
* @psalm-external-mutation-free
* @psalm-pure
* @return mixed
*/
public function getKey()
Expand All @@ -81,6 +85,7 @@ public function getKey()
}

/**
* @psalm-pure
* @psalm-suppress InvalidCast
* @return string
*/
Expand All @@ -95,6 +100,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 @@ -108,7 +114,7 @@ final public function equals($variable = null): bool
/**
* Returns the names (keys) of all constants in the Enum class
*
* @psalm-external-mutation-free
* @psalm-pure
* @psalm-return list<string>
* @return array
*/
Expand All @@ -120,7 +126,7 @@ public static function keys()
/**
* Returns instances of the Enum class of all Enum constants
*
* @psalm-external-mutation-free
* @psalm-pure
* @psalm-return array<string, static>
* @return static[] Constant name in key, Enum instance in value
*/
Expand All @@ -139,7 +145,8 @@ public static function values()
/**
* Returns all possible values as an array
*
* @psalm-external-mutation-free
* @psalm-pure
*
* @psalm-return array<string, mixed>
* @return array Constant name in key, constant value in value
*/
Expand All @@ -160,7 +167,7 @@ public static function toArray()
*
* @param $value
* @psalm-param mixed $value
* @psalm-external-mutation-free
* @psalm-pure
* @return bool
*/
public static function isValid($value)
Expand All @@ -173,7 +180,7 @@ public static function isValid($value)
*
* @param $key
* @psalm-param string $key
* @psalm-external-mutation-free
* @psalm-pure
* @return bool
*/
public static function isValidKey($key)
Expand All @@ -189,7 +196,7 @@ public static function isValidKey($key)
* @param $value
*
* @psalm-param mixed $value
* @psalm-external-mutation-free
* @psalm-pure
* @return mixed
*/
public static function search($value)
Expand All @@ -204,7 +211,7 @@ public static function search($value)
* @param array $arguments
*
* @return static
* @psalm-external-mutation-free
* @psalm-pure
* @throws \BadMethodCallException
*/
public static function __callStatic($name, $arguments)
Expand All @@ -223,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 3d5c198

Please sign in to comment.