From 0f7ae5f21f8bc776815891f9b0d7530f7b735513 Mon Sep 17 00:00:00 2001 From: Jarred Stelfox Date: Thu, 6 Feb 2020 16:41:46 -0800 Subject: [PATCH 1/7] PHPUnit: Update to a modern version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 871156d..86a3de3 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "ext-json": "*" }, "require-dev": { - "phpunit/phpunit": "^4.8.35|^5.7|^6.0", + "phpunit/phpunit": "^7", "squizlabs/php_codesniffer": "1.*" } } From 4c20d3f4d0bd9037be67650fd81f3f5e3551c6c1 Mon Sep 17 00:00:00 2001 From: Jarred Stelfox Date: Thu, 6 Feb 2020 16:42:31 -0800 Subject: [PATCH 2/7] Psalm: Add to allow type checking --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 86a3de3..1c7ba1c 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ }, "require": { "php": ">=7.1", - "ext-json": "*" + "ext-json": "*", + "vimeo/psalm": "^3.8" }, "require-dev": { "phpunit/phpunit": "^7", From d81fc4fde33aa9536435febeb9f81d6fcd132865 Mon Sep 17 00:00:00 2001 From: Jarred Stelfox Date: Thu, 6 Feb 2020 16:44:12 -0800 Subject: [PATCH 3/7] Psalm: Set up to be the most strict settings --- psalm.xml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 psalm.xml diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..b07e929 --- /dev/null +++ b/psalm.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + From 3116aff3398745dfb569d43d1fa65689a1b9a1d9 Mon Sep 17 00:00:00 2001 From: Jarred Stelfox Date: Thu, 6 Feb 2020 16:46:33 -0800 Subject: [PATCH 4/7] Psalm: Add psalm types to remove most psalm errors --- src/Enum.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Enum.php b/src/Enum.php index 3bce0dc..d750289 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -14,6 +14,9 @@ * @author Matthieu Napoli * @author Daniel Costa * @author Mirosław Filip + * + * @template T + * @psalm-immutable */ abstract class Enum implements \JsonSerializable { @@ -21,6 +24,7 @@ abstract class Enum implements \JsonSerializable * Enum value * * @var mixed + * @psalm-var T */ protected $value; @@ -28,6 +32,7 @@ abstract class Enum implements \JsonSerializable * Store existing constants in a static cache per object. * * @var array + * @psalm-var array> */ 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) @@ -53,6 +60,7 @@ public function __construct($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,6 +93,7 @@ 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 @@ -121,6 +132,8 @@ public static function values() /** * Returns all possible values as an array * + * @psalm-pure + * @psalm-return array * @return array Constant name in key, constant value in value */ public static function toArray() @@ -138,6 +151,7 @@ public static function toArray() * Check if is valid enum value * * @param $value + * @psalm-param mixed $value * * @return bool */ @@ -150,6 +164,7 @@ public static function isValid($value) * Check if is valid enum key * * @param $key + * @psalm-param string $key * * @return bool */ @@ -165,6 +180,8 @@ public static function isValidKey($key) * * @param $value * + * @psalm-param mixed $value + * @psalm-pure * @return mixed */ public static function search($value) From efff03a72c4454d9019616f486c04b84354f2049 Mon Sep 17 00:00:00 2001 From: Jarred Stelfox Date: Thu, 6 Feb 2020 16:49:46 -0800 Subject: [PATCH 5/7] Psalm: Make happy inside an immutable class For some reason psalm doesnt like get_called_class even when I tried to stub it to explain to psalm this method was pure (immutable) Instead lets use another valid method for getting the late static bond class name --- src/Enum.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Enum.php b/src/Enum.php index d750289..5c15251 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -52,7 +52,7 @@ 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; @@ -100,7 +100,7 @@ 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); } /** @@ -138,7 +138,8 @@ public static function values() */ 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(); @@ -205,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); } /** From 0fd28e9ed4f2e92793b151526c41f68a4aaee622 Mon Sep 17 00:00:00 2001 From: Jarred Stelfox Date: Thu, 6 Feb 2020 16:52:13 -0800 Subject: [PATCH 6/7] Psalm: Add to travis Lets static type check all pulls --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index a9f9cd8..c6ac198 100755 --- a/.travis.yml +++ b/.travis.yml @@ -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 From cb27bc182b08826828e9bbd620f22526d78c3286 Mon Sep 17 00:00:00 2001 From: Jarred Stelfox Date: Thu, 6 Feb 2020 16:56:20 -0800 Subject: [PATCH 7/7] Readme: Add Type checking badge --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7bdd4f8..6065f49 100644 --- a/README.md +++ b/README.md @@ -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).