-
Notifications
You must be signed in to change notification settings - Fork 569
Fix phpstan/phpstan#8926: Incorrect inference of instance variable modified from callback #5347
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 3 commits
9b410c5
59f45b5
9fab5c0
5955b7a
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,29 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug10903; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public bool $hasFooKey = false; | ||
|
|
||
| public function sayHello(): void | ||
| { | ||
| /** @var array<string, string> $arr */ | ||
| $arr = ['foo' => 'bar']; | ||
| $this->hasFooKey = false; | ||
|
|
||
| $filteredArr = \array_filter($arr, function ($value, $key) { | ||
| if (\stripos($key, 'foo') !== false) { | ||
| $this->hasFooKey = true; | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| }, ARRAY_FILTER_USE_BOTH); | ||
|
|
||
| if ($this->hasFooKey) { | ||
| // do something | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?php | ||
|
|
||
| namespace Bug11417; | ||
|
|
||
| class Wrap { | ||
| /** | ||
| * @param-immediately-invoked-callable $cb | ||
| */ | ||
| public static function run(callable $cb): void | ||
| { | ||
| $cb(); | ||
| } | ||
| } | ||
|
|
||
| class HelloWorld | ||
| { | ||
| private ?string $conn = null; | ||
|
|
||
| public function getConn(): string | ||
| { | ||
| if (!is_null($this->conn)) { | ||
| return $this->conn; | ||
| } | ||
|
|
||
| Wrap::run(function() { | ||
| $this->conn = "conn"; | ||
| }); | ||
|
|
||
| if (is_null($this->conn)) { | ||
| throw new \Exception("conn failed"); | ||
| } | ||
|
|
||
| return $this->conn; | ||
| } | ||
|
|
||
| public function disc(): void | ||
| { | ||
| $this->conn = null; | ||
| } | ||
| } | ||
|
|
||
| class WrapLater { | ||
| /** | ||
| * @param-later-invoked-callable $cb | ||
| */ | ||
| public static function run(callable $cb): void | ||
| { | ||
| $cb(); | ||
| } | ||
| } | ||
|
|
||
| class HelloWorldLater | ||
| { | ||
| private ?string $conn = null; | ||
|
|
||
| public function getConn(): string | ||
| { | ||
| if (!is_null($this->conn)) { | ||
| return $this->conn; | ||
| } | ||
|
|
||
| WrapLater::run(function() { | ||
| $this->conn = "conn"; | ||
| }); | ||
|
|
||
| if (is_null($this->conn)) { // should be always true - later-invoked callable doesn't invalidate | ||
|
||
| throw new \Exception("conn failed"); | ||
| } | ||
|
|
||
| return $this->conn; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the same test with later-invoked-callable ;
$this->connshould not be invalidate forlater-invoked-callable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make testscompleted successfully as well — all tests pass. The commit has already been pushed.