-
-
Notifications
You must be signed in to change notification settings - Fork 132
Fix #134 by marking Enum::__callStatic
as @psalm-pure
#138
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
.travis.yml export-ignore | ||
tests/ export-ignore | ||
phpunit.xml export-ignore | ||
static-analysis/ export-ignore |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
language: php | ||
|
||
php: | ||
- '7.1' | ||
- '7.2' | ||
- '7.3' | ||
- '7.4' | ||
- '8.0' | ||
|
||
matrix: | ||
fast_finish: true | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
* | ||
* @psalm-template T | ||
* @psalm-immutable | ||
* @psalm-consistent-constructor | ||
*/ | ||
abstract class Enum implements \JsonSerializable | ||
{ | ||
|
@@ -51,7 +52,7 @@ abstract class Enum implements \JsonSerializable | |
* @psalm-pure | ||
* @param mixed $value | ||
* | ||
* @psalm-param static<T>|T $value | ||
* @psalm-param T $value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was changed because:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But your change only prevents that from happening explicitly, through static analysis. So after all this change makes sense I think. |
||
* @throws \UnexpectedValueException if incompatible type is given. | ||
*/ | ||
public function __construct($value) | ||
|
@@ -162,7 +163,9 @@ public static function toArray() | |
$class = static::class; | ||
|
||
if (!isset(static::$cache[$class])) { | ||
/** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */ | ||
$reflection = new \ReflectionClass($class); | ||
/** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */ | ||
static::$cache[$class] = $reflection->getConstants(); | ||
} | ||
|
||
|
@@ -219,6 +222,8 @@ public static function search($value) | |
* | ||
* @return static | ||
* @throws \BadMethodCallException | ||
* | ||
* @psalm-pure | ||
*/ | ||
public static function __callStatic($name, $arguments) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyCLabs\Tests\Enum\StaticAnalysis; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
/** | ||
* @method static PureEnum A() | ||
* @method static PureEnum C() | ||
* | ||
* @psalm-immutable | ||
* @psalm-template T of 'A'|'B' | ||
* @template-extends Enum<T> | ||
*/ | ||
final class PureEnum extends Enum | ||
{ | ||
const A = 'A'; | ||
const C = 'C'; | ||
} | ||
|
||
/** @psalm-pure */ | ||
function enumFetchViaMagicMethodIsPure(): PureEnum | ||
{ | ||
return PureEnum::A(); | ||
} | ||
|
||
/** @psalm-pure */ | ||
function enumFetchViaExplicitMagicCallIsPure(): PureEnum | ||
{ | ||
return PureEnum::__callStatic('A', []); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.