-
Notifications
You must be signed in to change notification settings - Fork 574
Narrow array&callable intersection to list{class-string|object, string} #5563
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
phpstan-bot
wants to merge
5
commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-rrmiaik
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
64fbc7b
Narrow array&callable intersection to list{class-string|object, string}
phpstan-bot 0f240cc
Narrow ConstantArrayType with callable and remove IntersectionType sp…
phpstan-bot fa7b3c6
Require both offsets 0 and 1 before narrowing ConstantArrayType with …
phpstan-bot 65daf7c
Return NeverType early for constant arrays too small to be callable
phpstan-bot fae693f
Check key type compatibility when narrowing ArrayType with callable
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
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
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,70 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14549; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class Foo | ||
| { | ||
| public function foo(array $task): void | ||
| { | ||
| if (\is_callable($task)) { | ||
| assertType('list{class-string|object, string}&callable(): mixed', $task); | ||
| assertType('class-string|object', $task[0]); | ||
| assertType('string', $task[1]); | ||
|
|
||
| foreach ($task as $key => $value) { | ||
| assertType('object|string', $value); | ||
| assertType('0|1', $key); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function testCallableArrayIterableTypes(callable $value): void | ||
| { | ||
| if (is_array($value)) { | ||
| assertType('list{class-string|object, string}&callable(): mixed', $value); | ||
|
|
||
| foreach ($value as $key => $val) { | ||
| assertType('0|1', $key); | ||
| assertType('object|string', $val); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** @param array{string, string} $task */ | ||
| public function testConstantArrayNarrowing(array $task): void | ||
| { | ||
| if (\is_callable($task)) { | ||
| assertType('list{class-string, string}&callable(): mixed', $task); | ||
| assertType('class-string', $task[0]); | ||
| assertType('string', $task[1]); | ||
| } | ||
| } | ||
|
|
||
| /** @param array<string> $task */ | ||
| public function testTypedArrayNarrowing(array $task): void | ||
| { | ||
| if (\is_callable($task)) { | ||
| // When value type is string, intersect with class-string|object gives class-string | ||
| // and intersect with string gives string | ||
| assertType('list{class-string, string}&callable(): mixed', $task); | ||
| } | ||
| } | ||
|
|
||
| /** @param array<string, mixed> $task */ | ||
| public function testStringKeyedArrayNarrowing(array $task): void | ||
| { | ||
| if (\is_callable($task)) { | ||
| assertType('*NEVER*', $task); | ||
| } | ||
| } | ||
|
|
||
| /** @param callable-array $value */ | ||
| public function testCallableArrayPhpDoc(array $value): void | ||
| { | ||
| assertType('list{class-string|object, string}&callable(): mixed', $value); | ||
| assertType('class-string|object', $value[0]); | ||
| assertType('string', $value[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
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,20 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14549Rule; | ||
|
|
||
| class Foo | ||
| { | ||
| public function foo(array $task): void | ||
| { | ||
| if (\is_callable($task)) { | ||
| $this->call($task); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int> $task | ||
| */ | ||
| public function call(array $task): void | ||
| { | ||
| } | ||
| } |
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.
we might want to immediately return NeverType when we have less than 2 key-types, so we don't create new ConstantArrayType for "too small/empty" constant arrays
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.
Done. The change adds an early
NeverTypereturn innarrowConstantArrayWithCallablewhen the constant array has fewer than 2 keys or doesn't contain both offsets 0 and 1 — avoiding unnecessaryConstantArrayTypeconstruction for arrays that can't be callable. All tests pass and phpstan reports no errors.