Skip to content

Add static type checking! #111

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 7 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ before_script:

script:
- vendor/bin/phpunit
- vendor/bin/psalm --shepherd

# Use Travis' new container-based infrastructure.
# See http://docs.travis-ci.com/user/migrating-from-legacy/#How-can-I-use-container-based-infrastructure%3F
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Build Status](https://travis-ci.org/myclabs/php-enum.png?branch=master)](https://travis-ci.org/myclabs/php-enum)
[![Latest Stable Version](https://poser.pugx.org/myclabs/php-enum/version.png)](https://packagist.org/packages/myclabs/php-enum)
[![Total Downloads](https://poser.pugx.org/myclabs/php-enum/downloads.png)](https://packagist.org/packages/myclabs/php-enum)
[![psalm](https://shepherd.dev/github/myclabs/php-enum/coverage.svg)](https://shepherd.dev/github/myclabs/php-enum)

Maintenance for this project is [supported via Tidelift](https://tidelift.com/subscription/pkg/packagist-myclabs-php-enum?utm_source=packagist-myclabs-php-enum&utm_medium=referral&utm_campaign=readme).

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
},
"require": {
"php": ">=7.1",
"ext-json": "*"
"ext-json": "*",
"vimeo/psalm": "^3.8"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35|^5.7|^6.0",
"phpunit/phpunit": "^7",
"squizlabs/php_codesniffer": "1.*"
}
}
20 changes: 20 additions & 0 deletions psalm.xml
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>
26 changes: 22 additions & 4 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,25 @@
* @author Matthieu Napoli <matthieu@mnapoli.fr>
* @author Daniel Costa <danielcosta@gmail.com>
* @author Mirosław Filip <mirfilip@gmail.com>
*
* @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 = [];

Expand All @@ -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)
Expand All @@ -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()
{
Expand All @@ -62,6 +70,7 @@ public function getValue()
/**
* Returns the enum key (i.e. the constant name).
*
* @psalm-pure
* @return mixed
*/
public function getKey()
Expand All @@ -70,6 +79,7 @@ public function getKey()
}

/**
* @psalm-suppress InvalidCast
* @return string
*/
public function __toString()
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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();
Expand All @@ -138,6 +152,7 @@ public static function toArray()
* Check if is valid enum value
*
* @param $value
* @psalm-param mixed $value
*
* @return bool
*/
Expand All @@ -150,6 +165,7 @@ public static function isValid($value)
* Check if is valid enum key
*
* @param $key
* @psalm-param string $key
*
* @return bool
*/
Expand All @@ -165,6 +181,8 @@ public static function isValidKey($key)
*
* @param $value
*
* @psalm-param mixed $value
* @psalm-pure
* @return mixed
*/
public static function search($value)
Expand All @@ -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);
}

/**
Expand Down