Skip to content

Commit ff059b0

Browse files
authored
Merge pull request #111 from jarstelfox/master
Add static type checking!
2 parents d67fb8a + cb27bc1 commit ff059b0

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ before_script:
1818

1919
script:
2020
- vendor/bin/phpunit
21+
- vendor/bin/psalm --shepherd
2122

2223
# Use Travis' new container-based infrastructure.
2324
# See http://docs.travis-ci.com/user/migrating-from-legacy/#How-can-I-use-container-based-infrastructure%3F

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![Build Status](https://travis-ci.org/myclabs/php-enum.png?branch=master)](https://travis-ci.org/myclabs/php-enum)
44
[![Latest Stable Version](https://poser.pugx.org/myclabs/php-enum/version.png)](https://packagist.org/packages/myclabs/php-enum)
55
[![Total Downloads](https://poser.pugx.org/myclabs/php-enum/downloads.png)](https://packagist.org/packages/myclabs/php-enum)
6+
[![psalm](https://shepherd.dev/github/myclabs/php-enum/coverage.svg)](https://shepherd.dev/github/myclabs/php-enum)
67

78
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).
89

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
},
2424
"require": {
2525
"php": ">=7.1",
26-
"ext-json": "*"
26+
"ext-json": "*",
27+
"vimeo/psalm": "^3.8"
2728
},
2829
"require-dev": {
29-
"phpunit/phpunit": "^4.8.35|^5.7|^6.0",
30+
"phpunit/phpunit": "^7",
3031
"squizlabs/php_codesniffer": "1.*"
3132
}
3233
}

psalm.xml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
totallyTyped="true"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
>
9+
<projectFiles>
10+
<directory name="src" />
11+
<ignoreFiles>
12+
<directory name="vendor" />
13+
<directory name="src/PHPUnit" />
14+
</ignoreFiles>
15+
</projectFiles>
16+
17+
<issueHandlers>
18+
<MixedAssignment errorLevel="info" />
19+
</issueHandlers>
20+
</psalm>

src/Enum.php

+22-4
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,25 @@
1414
* @author Matthieu Napoli <[email protected]>
1515
* @author Daniel Costa <[email protected]>
1616
* @author Mirosław Filip <[email protected]>
17+
*
18+
* @template T
19+
* @psalm-immutable
1720
*/
1821
abstract class Enum implements \JsonSerializable
1922
{
2023
/**
2124
* Enum value
2225
*
2326
* @var mixed
27+
* @psalm-var T
2428
*/
2529
protected $value;
2630

2731
/**
2832
* Store existing constants in a static cache per object.
2933
*
3034
* @var array
35+
* @psalm-var array<class-string, array<string, mixed>>
3136
*/
3237
protected static $cache = [];
3338

@@ -36,6 +41,8 @@ abstract class Enum implements \JsonSerializable
3641
*
3742
* @param mixed $value
3843
*
44+
* @psalm-param T $value
45+
* @psalm-suppress InvalidCast
3946
* @throws \UnexpectedValueException if incompatible type is given.
4047
*/
4148
public function __construct($value)
@@ -45,14 +52,15 @@ public function __construct($value)
4552
}
4653

4754
if (!$this->isValid($value)) {
48-
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . \get_called_class());
55+
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . static::class);
4956
}
5057

5158
$this->value = $value;
5259
}
5360

5461
/**
5562
* @return mixed
63+
* @psalm-return T
5664
*/
5765
public function getValue()
5866
{
@@ -62,6 +70,7 @@ public function getValue()
6270
/**
6371
* Returns the enum key (i.e. the constant name).
6472
*
73+
* @psalm-pure
6574
* @return mixed
6675
*/
6776
public function getKey()
@@ -70,6 +79,7 @@ public function getKey()
7079
}
7180

7281
/**
82+
* @psalm-suppress InvalidCast
7383
* @return string
7484
*/
7585
public function __toString()
@@ -83,13 +93,14 @@ public function __toString()
8393
*
8494
* This method is final, for more information read https://github.com/myclabs/php-enum/issues/4
8595
*
96+
* @psalm-param mixed $variable
8697
* @return bool
8798
*/
8899
final public function equals($variable = null): bool
89100
{
90101
return $variable instanceof self
91102
&& $this->getValue() === $variable->getValue()
92-
&& \get_called_class() === \get_class($variable);
103+
&& static::class === \get_class($variable);
93104
}
94105

95106
/**
@@ -121,11 +132,14 @@ public static function values()
121132
/**
122133
* Returns all possible values as an array
123134
*
135+
* @psalm-pure
136+
* @psalm-return array<string, mixed>
124137
* @return array Constant name in key, constant value in value
125138
*/
126139
public static function toArray()
127140
{
128-
$class = \get_called_class();
141+
$class = static::class;
142+
129143
if (!isset(static::$cache[$class])) {
130144
$reflection = new \ReflectionClass($class);
131145
static::$cache[$class] = $reflection->getConstants();
@@ -138,6 +152,7 @@ public static function toArray()
138152
* Check if is valid enum value
139153
*
140154
* @param $value
155+
* @psalm-param mixed $value
141156
*
142157
* @return bool
143158
*/
@@ -150,6 +165,7 @@ public static function isValid($value)
150165
* Check if is valid enum key
151166
*
152167
* @param $key
168+
* @psalm-param string $key
153169
*
154170
* @return bool
155171
*/
@@ -165,6 +181,8 @@ public static function isValidKey($key)
165181
*
166182
* @param $value
167183
*
184+
* @psalm-param mixed $value
185+
* @psalm-pure
168186
* @return mixed
169187
*/
170188
public static function search($value)
@@ -188,7 +206,7 @@ public static function __callStatic($name, $arguments)
188206
return new static($array[$name]);
189207
}
190208

191-
throw new \BadMethodCallException("No static method or enum constant '$name' in class " . \get_called_class());
209+
throw new \BadMethodCallException("No static method or enum constant '$name' in class " . static::class);
192210
}
193211

194212
/**

0 commit comments

Comments
 (0)