-
Notifications
You must be signed in to change notification settings - Fork 552
Fix #12574: Erroneous Offset might not exist #4965
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
86a07a2
Fix isset() type narrowing lost when result stored in variable for pr…
phpstan-bot 71e33f8
Add regression test for #11870
phpstan-bot 02719b1
Add regression test for #11102
phpstan-bot 156b1ea
Add regression test for #10040
phpstan-bot 3836343
Add regression test for #7716
phpstan-bot 0ca1890
Add regression test for #6991
phpstan-bot 2998d07
Add regression test for #6120
phpstan-bot a538aa4
Fix bug-10040.php: put declare(strict_types) after lint comment
phpstan-bot a56a74a
Revert "Fix bug-10040.php: put declare(strict_types) after lint comment"
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php declare(strict_types = 1); // lint >= 8.0 | ||
|
|
||
| namespace Bug10040; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class A | ||
| { | ||
| public string|null $foo; | ||
| } | ||
|
|
||
| /** | ||
| * @phpstan-assert-if-true !null $a->foo | ||
| */ | ||
| function assertStringNotNull(A $a): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| $a1 = new A(); | ||
| if(assertStringNotNull($a1)){ | ||
| assertType('string', $a1->foo); | ||
| } | ||
|
|
||
| $a2 = new A(); | ||
| $stringIsNotNull = assertStringNotNull($a2); | ||
| if($stringIsNotNull){ | ||
| assertType('string', $a2->foo); | ||
| } | ||
|
|
||
| $a3 = new A(); | ||
| if($stringIsNotNull = assertStringNotNull($a3)){ | ||
| assertType('string', $a3->foo); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug11102; | ||
|
|
||
| use DateTime; | ||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** @var array{'start': DateTime|null} $details */ | ||
| $details = ['start' => null]; | ||
|
|
||
| $startIsADateTime = $details['start'] instanceof DateTime; | ||
|
|
||
| if ($startIsADateTime) { | ||
| assertType('DateTime', $details['start']); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug11870; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function () { | ||
| assert(isset($v) && is_array($v) && isset($v['LANGUAGE'])); | ||
|
|
||
| $language = $v['LANGUAGE'] !== 1 ? 'en' : ''; | ||
| if ($language !== '') { | ||
| assertType('true', $v['LANGUAGE'] !== 1); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php declare(strict_types = 1); // lint >= 8.0 | ||
|
|
||
| namespace Bug6120; | ||
|
|
||
| use Generator; | ||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| /** | ||
| * @var Generator<int, string> $gen | ||
| */ | ||
| private ?Generator $gen = null; | ||
|
|
||
| public function setGenerator(Generator $gen): void { | ||
| $this->gen = $gen; | ||
| } | ||
|
|
||
| public function sayHello(): void | ||
| { | ||
| while ($v = $this->gen?->current()) { | ||
| assertType('Generator<int, string, mixed, mixed>', $this->gen); | ||
| echo $v; | ||
| $this->gen->next(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php declare(strict_types = 1); // lint >= 8.0 | ||
|
|
||
| namespace Bug6991; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public int $more = 1; | ||
|
|
||
| public ?string $key = null; | ||
| } | ||
|
|
||
| class Other { | ||
| public ?HelloWorld $optional = null; | ||
| } | ||
|
|
||
| function test(Other $object): int | ||
| { | ||
| $key = $object->optional?->key; | ||
|
|
||
| if (!$key) { | ||
| return 0; | ||
| } | ||
|
|
||
| assertType('Bug6991\HelloWorld', $object->optional); | ||
|
|
||
| return $object->optional->more * 100; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug7716; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| /** | ||
| * @param array{foo?: int, bar?: int} $array | ||
| */ | ||
| public function sayHello(array $array): int | ||
| { | ||
| $hasFoo = isset($array['foo']) && $array['foo'] > 1; | ||
| $hasBar = isset($array['bar']) && $array['bar'] > 1; | ||
|
|
||
| if ($hasFoo) { | ||
| assertType('int<2, max>', $array['foo']); | ||
| return $array['foo']; | ||
| } | ||
|
|
||
| if ($hasBar) { | ||
| assertType('int<2, max>', $array['bar']); | ||
| return $array['bar']; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * @param array{foo?: int, bar?: int} $array | ||
| */ | ||
| public function sayHello2(array $array): int | ||
| { | ||
| $hasBar = isset($array['bar']) && $array['bar'] > 1; | ||
| $hasFoo = isset($array['foo']) && $array['foo'] > 1; | ||
|
|
||
| if ($hasFoo) { | ||
| assertType('int<2, max>', $array['foo']); | ||
| return $array['foo']; | ||
| } | ||
|
|
||
| if ($hasBar) { | ||
| assertType('int<2, max>', $array['bar']); | ||
| return $array['bar']; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug12574c; | ||
|
|
||
| class Galaxy | ||
| { | ||
| /** @var list<World> $worlds */ | ||
| public array $worlds = []; | ||
| } | ||
|
|
||
| class World | ||
| { | ||
| public int $x = 1; | ||
| } | ||
|
|
||
|
|
||
| function hello(Galaxy $a): void | ||
| { | ||
| $notEmpty = isset($a->worlds[0]); | ||
| if ($notEmpty && $a->worlds[0]->x === 1) { | ||
| echo 'hello'; | ||
| } | ||
| } | ||
|
|
||
| function hello2(Galaxy $a): void | ||
| { | ||
| $worlds = $a->worlds; | ||
| $notEmpty = isset($worlds[0]); | ||
| if ($notEmpty && $worlds[0]->x === 1) { | ||
| echo 'hello'; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I feel like we're missing a lot:
Since PropertyFetch works, I think NullsafePropertyFetch should be added too.
CallLike, see:
https://phpstan.org/r/838517f6-ed32-4fab-993a-d54d85ea6986
In the end, I'm not sure which are the skipped
$expr