-
Notifications
You must be signed in to change notification settings - Fork 569
Merge scope after ?-> method call to account for argument short-circuiting
#5456
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
Open
phpstan-bot
wants to merge
3
commits into
phpstan:2.1.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-z1mfsd1
base: 2.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
e9211ff
Merge scope after `?->` method call to account for argument short-cir…
ondrejmirtes 1d91140
Use $varType->isNull()->maybe() instead of TypeCombinator::containsNu…
phpstan-bot 12bdb77
Add throw-points test for mixed~null (non-nullable) nullsafe method call
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
Some comments aren't visible on the classic Files Changed page.
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,44 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug10729Types; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class Foo | ||
| { | ||
| public function bar(string $a, string $b): string | ||
| { | ||
| return $a . $b; | ||
| } | ||
| } | ||
|
|
||
| function nullable(?Foo $foo): void | ||
| { | ||
| $foo?->bar($a = 'hello', $b = 'world'); | ||
| assertType("'hello'|null", $a ?? null); | ||
| assertType("'world'|null", $b ?? null); | ||
| } | ||
|
|
||
| function nonNullable(Foo $foo): void | ||
| { | ||
| $foo->bar($a = 'hello', $b = 'world'); | ||
| assertType("'hello'", $a); | ||
| assertType("'world'", $b); | ||
| } | ||
|
|
||
| function alwaysNull(): void | ||
| { | ||
| $foo = null; | ||
| $foo?->bar($a = 'hello', $b = 'world'); | ||
| assertType('null', $a ?? null); // $a is never assigned when $foo is always null | ||
| } | ||
|
|
||
| function chainedNullsafe(?Foo $foo): void | ||
| { | ||
| $result = $foo?->bar($x = 'a', $y = 'b'); | ||
| assertType('string|null', $result); | ||
| assertType("'a'|null", $x ?? null); | ||
| assertType("'b'|null", $y ?? null); | ||
| } |
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
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,47 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug10729; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public function sayHello(?\DateTimeImmutable $date): void | ||
| { | ||
| var_dump($date?->format($format = "Y-m-d")); | ||
| var_dump($format); // might not be defined if $date is null | ||
| } | ||
|
|
||
| public function nonNullable(\DateTimeImmutable $date): void | ||
| { | ||
| var_dump($date->format($format = "Y-m-d")); | ||
| var_dump($format); // always defined, $date can't be null | ||
| } | ||
|
|
||
| public function nullOnly(): void | ||
| { | ||
| $date = null; | ||
| var_dump($date?->format($format = "Y-m-d")); | ||
| var_dump($format); // undefined, $date is always null | ||
| } | ||
|
|
||
| public function multipleArgs(?\DateTimeImmutable $date): void | ||
| { | ||
| $date?->createFromFormat($format = 'Y-m-d', $value = '2024-01-01'); | ||
| var_dump($format); // might not be defined | ||
| var_dump($value); // might not be defined | ||
| } | ||
|
|
||
| public function nestedAssignment(?\DateTimeImmutable $date): void | ||
| { | ||
| $result = $date?->format($format = "Y-m-d"); | ||
| var_dump($format); // might not be defined | ||
| } | ||
|
|
||
| public function existingVarStillDefined(?\DateTimeImmutable $date): void | ||
| { | ||
| $existing = 'before'; | ||
| $date?->format($format = "Y-m-d"); | ||
| var_dump($existing); // always defined, not affected by nullsafe | ||
| } | ||
| } |
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.
The call need to be chaine inside the try catch
If mixed~null is complex to indicate in phpdoc, then maybe use
objectas return type of doesntThrow2