File tree Expand file tree Collapse file tree 8 files changed +72
-29
lines changed Expand file tree Collapse file tree 8 files changed +72
-29
lines changed Original file line number Diff line number Diff line change 77.travis.yml export-ignore
88tests / export-ignore
99phpunit.xml export-ignore
10+ static-analysis / export-ignore
Original file line number Diff line number Diff line change 11language : php
22
33php :
4- - ' 7.1'
5- - ' 7.2'
64 - ' 7.3'
75 - ' 7.4'
6+ - ' 8.0'
87
98matrix :
109 fast_finish : true
Original file line number Diff line number Diff line change 2222 }
2323 },
2424 "require" : {
25- "php" : " >=7.1 " ,
25+ "php" : " ^7.3 || ^8.0 " ,
2626 "ext-json" : " *"
2727 },
2828 "require-dev" : {
29- "phpunit/phpunit" : " ^7 " ,
29+ "phpunit/phpunit" : " ^9.5 " ,
3030 "squizlabs/php_codesniffer" : " 1.*" ,
31- "vimeo/psalm" : " ^3.8 "
31+ "vimeo/psalm" : " ^4.5.1 "
3232 }
3333}
Original file line number Diff line number Diff line change 11<?xml version =" 1.0" encoding =" utf-8" ?>
2- <!--
3- phpunit -c phpunit.xml
4- -->
5- <phpunit backupGlobals =" false"
6- backupStaticAttributes =" false"
7- colors =" true"
8- convertErrorsToExceptions =" true"
9- convertNoticesToExceptions =" true"
10- convertWarningsToExceptions =" true"
11- processIsolation =" false"
12- stopOnFailure =" false"
13- syntaxCheck =" false"
14- bootstrap =" ./tests/bootstrap.php" >
2+ <phpunit
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : noNamespaceSchemaLocation =" vendor/phpunit/phpunit/phpunit.xsd"
5+ colors =" true"
6+ bootstrap =" ./tests/bootstrap.php"
7+ >
158 <testsuites >
169 <testsuite name =" PHP Enum Test Suite" >
1710 <directory suffix =" .php" >./tests</directory >
Original file line number Diff line number Diff line change 88>
99 <projectFiles >
1010 <directory name =" src" />
11+ <directory name =" static-analysis" />
1112 <ignoreFiles >
1213 <directory name =" vendor" />
1314 <directory name =" src/PHPUnit" />
1617
1718 <issueHandlers >
1819 <MixedAssignment errorLevel =" info" />
20+
21+ <ImpureStaticProperty >
22+ <!-- self::$... usages in Enum are used to populate an internal cache, and cause no side-effects -->
23+ <errorLevel type =" suppress" >
24+ <file name =" src/Enum.php" />
25+ </errorLevel >
26+ </ImpureStaticProperty >
27+
28+ <ImpureVariable >
29+ <!-- $this usages in Enum point themselves to an immutable instance -->
30+ <errorLevel type =" suppress" >
31+ <file name =" src/Enum.php" />
32+ </errorLevel >
33+ </ImpureVariable >
1934 </issueHandlers >
2035</psalm >
Original file line number Diff line number Diff line change 1717 *
1818 * @psalm-template T
1919 * @psalm-immutable
20+ * @psalm-consistent-constructor
2021 */
2122abstract class Enum implements \JsonSerializable
2223{
@@ -58,7 +59,7 @@ abstract class Enum implements \JsonSerializable
5859 * @psalm-pure
5960 * @param mixed $value
6061 *
61- * @psalm-param static<T>| T $value
62+ * @psalm-param T $value
6263 * @throws \UnexpectedValueException if incompatible type is given.
6364 */
6465 public function __construct ($ value )
@@ -186,7 +187,9 @@ public static function toArray()
186187 $ class = static ::class;
187188
188189 if (!isset (static ::$ cache [$ class ])) {
190+ /** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */
189191 $ reflection = new \ReflectionClass ($ class );
192+ /** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */
190193 static ::$ cache [$ class ] = $ reflection ->getConstants ();
191194 }
192195
@@ -270,6 +273,8 @@ public static function search($value)
270273 *
271274 * @return static
272275 * @throws \BadMethodCallException
276+ *
277+ * @psalm-pure
273278 */
274279 public static function __callStatic ($ name , $ arguments )
275280 {
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace MyCLabs \Tests \Enum \StaticAnalysis ;
6+
7+ use MyCLabs \Enum \Enum ;
8+
9+ /**
10+ * @method static PureEnum A()
11+ * @method static PureEnum C()
12+ *
13+ * @psalm-immutable
14+ * @psalm-template T of 'A'|'B'
15+ * @template-extends Enum<T>
16+ */
17+ final class PureEnum extends Enum
18+ {
19+ const A = 'A ' ;
20+ const C = 'C ' ;
21+ }
22+
23+ /** @psalm-pure */
24+ function enumFetchViaMagicMethodIsPure (): PureEnum
25+ {
26+ return PureEnum::A ();
27+ }
28+
29+ /** @psalm-pure */
30+ function enumFetchViaExplicitMagicCallIsPure (): PureEnum
31+ {
32+ return PureEnum::__callStatic ('A ' , []);
33+ }
Original file line number Diff line number Diff line change @@ -38,13 +38,12 @@ public function testGetKey()
3838 $ this ->assertNotEquals ('BA ' , $ value ->getKey ());
3939 }
4040
41- /**
42- * @dataProvider invalidValueProvider
43- * @expectedException \UnexpectedValueException
44- * @expectedExceptionMessage is not part of the enum MyCLabs\Tests\Enum\EnumFixture
45- */
41+ /** @dataProvider invalidValueProvider */
4642 public function testCreatingEnumWithInvalidValue ($ value )
4743 {
44+ $ this ->expectException (\UnexpectedValueException::class);
45+ $ this ->expectExceptionMessage ('is not part of the enum ' . EnumFixture::class);
46+
4847 new EnumFixture ($ value );
4948 }
5049
@@ -166,13 +165,11 @@ public function testStaticAccess()
166165 $ this ->assertNotSame (EnumFixture::NUMBER (), EnumFixture::NUMBER ());
167166 }
168167
169- /**
170- * @expectedException \BadMethodCallException
171- * @expectedExceptionMessage No static method or enum constant 'UNKNOWN' in class
172- * UnitTest\MyCLabs\Enum\Enum\EnumFixture
173- */
174168 public function testBadStaticAccess ()
175169 {
170+ $ this ->expectException (\BadMethodCallException::class);
171+ $ this ->expectExceptionMessage ('No static method or enum constant \'UNKNOWN \' in class ' . EnumFixture::class);
172+
176173 EnumFixture::UNKNOWN ();
177174 }
178175
You can’t perform that action at this time.
0 commit comments