-
Notifications
You must be signed in to change notification settings - Fork 568
Fix phpstan/phpstan#12686: Unable to mark an anonymous function as impure #5348
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
dbdc192
2464761
93e1e21
b788e4e
a811e24
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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug12686; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** @phpstan-impure */ | ||
| $f = function (): bool { | ||
| return (bool) rand(0,1); | ||
| }; | ||
|
|
||
| if ($f()) { | ||
| assertType('bool', $f()); | ||
| } | ||
|
|
||
| // Pure closure should still have narrowing | ||
| $h = function (): bool { | ||
| return true; | ||
| }; | ||
|
|
||
| if ($h()) { | ||
| assertType('true', $h()); | ||
| } | ||
|
|
||
| // Multiple callable parameter acceptors (union of closures) | ||
| // When one variant is impure, the combined result should be impure | ||
| /** @phpstan-impure */ | ||
| $impure = function (): bool { | ||
| return (bool) rand(0, 1); | ||
| }; | ||
|
|
||
| $pure = function (): bool { | ||
| return true; | ||
| }; | ||
|
|
||
| if (rand(0, 1)) { | ||
| $g = $impure; | ||
| } else { | ||
| $g = $pure; | ||
| } | ||
|
|
||
| if ($g()) { | ||
| assertType('bool', $g()); | ||
| } | ||
|
|
||
| // Multiple callable parameter acceptors where all are pure | ||
| $pure1 = function (): bool { | ||
| return true; | ||
| }; | ||
|
|
||
| $pure2 = function (): bool { | ||
| return true; | ||
| }; | ||
|
|
||
| if (rand(0, 1)) { | ||
| $p = $pure1; | ||
| } else { | ||
| $p = $pure2; | ||
| } | ||
|
|
||
| if ($p()) { | ||
| assertType('true', $p()); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug3770; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| // PHPDoc on closures should be respected for purity | ||
|
|
||
| /** @phpstan-impure */ | ||
| $f = static function (string $input): bool { | ||
| return strlen($input) > rand(0, 10); | ||
| }; | ||
|
|
||
| if ($f('hello')) { | ||
| // Should not narrow to true because closure is impure | ||
| assertType('bool', $f('hello')); | ||
| } | ||
|
|
||
| // Closure with @phpstan-pure should allow narrowing | ||
| /** @phpstan-pure */ | ||
| $g = static function (string $input): bool { | ||
| return strlen($input) > 5; | ||
| }; | ||
|
|
||
| if ($g('hello world')) { | ||
| assertType('true', $g('hello world')); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug6822; | ||
|
Contributor
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. bug 6822 was about a false-positive rule error. we need a rule test for this file to prove it no longer errors.
Collaborator
Author
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. Done. Added a rule test for bug 6822 in
Contributor
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. re-use the tests/PHPStan/Analyser/nsrt/bug-6822.php for the new rule-test instead of adding another new one
Collaborator
Author
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. Full test suite passed too. All done — the review feedback has been addressed, tests and static analysis are green, and the commit is pushed. |
||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| // Closures marked as @phpstan-impure should not have their return type narrowed | ||
|
|
||
| /** @phpstan-impure */ | ||
| $closure = function (): bool { | ||
| return (bool) rand(0, 1); | ||
| }; | ||
|
|
||
| assertType('bool', $closure()); | ||
|
|
||
| if ($closure()) { | ||
| assertType('bool', $closure()); | ||
|
|
||
| if ($closure()) { // should not be reported as "always true" | ||
| echo 'yes'; | ||
| } | ||
| } | ||
|
|
||
| // Same with an explicit impure closure assigned to a variable | ||
| /** @phpstan-impure */ | ||
| $impureFn = function (): int { | ||
| return rand(0, 100); | ||
| }; | ||
|
|
||
| if ($impureFn() > 50) { | ||
| assertType('int<0, 100>', $impureFn()); | ||
|
|
||
| if ($impureFn() > 50) { // should not be reported as "always true" | ||
| echo 'yes'; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.