-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from jarstelfox/master
Add static type checking!
- Loading branch information
Showing
5 changed files
with
47 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0"?> | ||
<psalm | ||
totallyTyped="true" | ||
resolveFromConfigFile="true" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="https://getpsalm.org/schema/config" | ||
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" | ||
> | ||
<projectFiles> | ||
<directory name="src" /> | ||
<ignoreFiles> | ||
<directory name="vendor" /> | ||
<directory name="src/PHPUnit" /> | ||
</ignoreFiles> | ||
</projectFiles> | ||
|
||
<issueHandlers> | ||
<MixedAssignment errorLevel="info" /> | ||
</issueHandlers> | ||
</psalm> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,20 +14,25 @@ | |
* @author Matthieu Napoli <[email protected]> | ||
* @author Daniel Costa <[email protected]> | ||
* @author Mirosław Filip <[email protected]> | ||
* | ||
* @template T | ||
* @psalm-immutable | ||
*/ | ||
abstract class Enum implements \JsonSerializable | ||
{ | ||
/** | ||
* Enum value | ||
* | ||
* @var mixed | ||
* @psalm-var T | ||
*/ | ||
protected $value; | ||
|
||
/** | ||
* Store existing constants in a static cache per object. | ||
* | ||
* @var array | ||
* @psalm-var array<class-string, array<string, mixed>> | ||
*/ | ||
protected static $cache = []; | ||
|
||
|
@@ -36,6 +41,8 @@ abstract class Enum implements \JsonSerializable | |
* | ||
* @param mixed $value | ||
* | ||
* @psalm-param T $value | ||
* @psalm-suppress InvalidCast | ||
* @throws \UnexpectedValueException if incompatible type is given. | ||
*/ | ||
public function __construct($value) | ||
|
@@ -45,14 +52,15 @@ public function __construct($value) | |
} | ||
|
||
if (!$this->isValid($value)) { | ||
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . \get_called_class()); | ||
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . static::class); | ||
} | ||
|
||
$this->value = $value; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
* @psalm-return T | ||
*/ | ||
public function getValue() | ||
{ | ||
|
@@ -62,6 +70,7 @@ public function getValue() | |
/** | ||
* Returns the enum key (i.e. the constant name). | ||
* | ||
* @psalm-pure | ||
* @return mixed | ||
*/ | ||
public function getKey() | ||
|
@@ -70,6 +79,7 @@ public function getKey() | |
} | ||
|
||
/** | ||
* @psalm-suppress InvalidCast | ||
* @return string | ||
*/ | ||
public function __toString() | ||
|
@@ -83,13 +93,14 @@ public function __toString() | |
* | ||
* This method is final, for more information read https://github.com/myclabs/php-enum/issues/4 | ||
* | ||
* @psalm-param mixed $variable | ||
* @return bool | ||
*/ | ||
final public function equals($variable = null): bool | ||
{ | ||
return $variable instanceof self | ||
&& $this->getValue() === $variable->getValue() | ||
&& \get_called_class() === \get_class($variable); | ||
&& static::class === \get_class($variable); | ||
} | ||
|
||
/** | ||
|
@@ -121,11 +132,14 @@ public static function values() | |
/** | ||
* Returns all possible values as an array | ||
* | ||
* @psalm-pure | ||
* @psalm-return array<string, mixed> | ||
* @return array Constant name in key, constant value in value | ||
*/ | ||
public static function toArray() | ||
{ | ||
$class = \get_called_class(); | ||
$class = static::class; | ||
|
||
if (!isset(static::$cache[$class])) { | ||
$reflection = new \ReflectionClass($class); | ||
static::$cache[$class] = $reflection->getConstants(); | ||
|
@@ -138,6 +152,7 @@ public static function toArray() | |
* Check if is valid enum value | ||
* | ||
* @param $value | ||
* @psalm-param mixed $value | ||
* | ||
* @return bool | ||
*/ | ||
|
@@ -150,6 +165,7 @@ public static function isValid($value) | |
* Check if is valid enum key | ||
* | ||
* @param $key | ||
* @psalm-param string $key | ||
* | ||
* @return bool | ||
*/ | ||
|
@@ -165,6 +181,8 @@ public static function isValidKey($key) | |
* | ||
* @param $value | ||
* | ||
* @psalm-param mixed $value | ||
* @psalm-pure | ||
* @return mixed | ||
*/ | ||
public static function search($value) | ||
|
@@ -188,7 +206,7 @@ public static function __callStatic($name, $arguments) | |
return new static($array[$name]); | ||
} | ||
|
||
throw new \BadMethodCallException("No static method or enum constant '$name' in class " . \get_called_class()); | ||
throw new \BadMethodCallException("No static method or enum constant '$name' in class " . static::class); | ||
} | ||
|
||
/** | ||
|