-
Notifications
You must be signed in to change notification settings - Fork 584
Merge all callable acceptors' parameters into NativeParameterReflection when a closure is passed to a union of callables
#6128
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
Merged
staabm
merged 2 commits into
phpstan:2.2.x
from
phpstan-bot:create-pull-request/patch-3xe1x59
Jul 28, 2026
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug15003; | ||
|
|
||
| use Closure; | ||
|
|
||
| /** | ||
| * @phpstan-type Foo InvokableClass|callable(string, mixed): int | ||
| */ | ||
| class TypeImportShortcut {} | ||
|
|
||
| interface InvokableClass | ||
| { | ||
| /** | ||
| * @param Closure(string, ?string=): string $fail | ||
| */ | ||
| public function __invoke(string $foo, Closure $fail): int; | ||
| } | ||
|
|
||
| /** @phpstan-import-type Foo from TypeImportShortcut */ | ||
| class A | ||
| { | ||
|
|
||
| /** @param callable(string):Foo|Foo $param */ | ||
| public function foo($param): void {} | ||
|
|
||
| } | ||
|
|
||
| (new A)->foo(function(string $foo) { | ||
| return 5; | ||
| }); | ||
|
|
||
| interface InvokableRule | ||
| { | ||
|
|
||
| /** | ||
| * @param Closure(string): string $fail | ||
| */ | ||
| public function __invoke(string $attribute, mixed $value, Closure $fail): void; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * @phpstan-type FieldValidationRule InvokableRule|(callable(string, mixed, Closure): void) | ||
| * @phpstan-type ValidationRules array<int, FieldValidationRule>|FieldValidationRule | ||
| */ | ||
| final class Field | ||
| { | ||
|
|
||
| /** | ||
| * @param (callable(string): ValidationRules)|ValidationRules $rules | ||
| */ | ||
| public function rules($rules): self | ||
| { | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @param (callable(string): ValidationRules)|ValidationRules ...$rules | ||
| */ | ||
| public function creationRules($rules): self | ||
| { | ||
| return $this; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| function rules(): Field | ||
| { | ||
| return (new Field())->rules(function ($attribute, $value, $fail) { | ||
| }); | ||
| } | ||
|
|
||
| function creationRules(): Field | ||
| { | ||
| return (new Field())->creationRules([ | ||
| function ($attribute, $value, $fail) { | ||
| }, | ||
| ]); | ||
| } | ||
89 changes: 89 additions & 0 deletions
89
tests/PHPStan/Analyser/nsrt/closure-passed-to-union-of-callables.php
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,89 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace ClosurePassedToUnionOfCallables; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** @template T */ | ||
| final class Invokable | ||
| { | ||
|
|
||
| /** @param T $a */ | ||
| public function __invoke($a, string $b): void | ||
| { | ||
| } | ||
|
|
||
| } | ||
|
|
||
| final class Foo | ||
| { | ||
|
|
||
| /** | ||
| * @param callable(int, string): void|callable(int): void $cb | ||
| */ | ||
| public function longestFirst($cb): void | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @param callable(int): void|callable(int, string): void $cb | ||
| */ | ||
| public function shortestFirst($cb): void | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @param callable(int): void|Invokable<string>|callable(bool, float): void $cb | ||
| */ | ||
| public function withInvokable($cb): void | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @param callable(int...): void|callable(string, string): void $cb | ||
| */ | ||
| public function variadicFirst($cb): void | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @param callable(string, string): void|callable(int...): void $cb | ||
| */ | ||
| public function variadicLast($cb): void | ||
| { | ||
| } | ||
|
|
||
| public function run(): void | ||
| { | ||
| $this->longestFirst(function ($a, $b): void { | ||
| assertType('int', $a); | ||
| assertType('string', $b); | ||
| }); | ||
|
|
||
| $this->shortestFirst(function ($a, $b): void { | ||
| assertType('int', $a); | ||
| assertType('string', $b); | ||
| }); | ||
|
|
||
| $this->longestFirst(fn ($a, $b) => assertType('int', $a)); | ||
| $this->longestFirst(fn ($a, $b) => assertType('string', $b)); | ||
| $this->shortestFirst(fn ($a, $b) => assertType('int', $a)); | ||
| $this->shortestFirst(fn ($a, $b) => assertType('string', $b)); | ||
|
|
||
| $this->withInvokable(function ($a, $b): void { | ||
| assertType('bool|int|string', $a); | ||
| assertType('float|string', $b); | ||
| }); | ||
|
|
||
| $this->variadicFirst(function ($a, $b): void { | ||
| assertType('int|string', $a); | ||
| assertType('string', $b); | ||
| }); | ||
|
|
||
| $this->variadicLast(function ($a, $b): void { | ||
| assertType('int|string', $a); | ||
| assertType('string', $b); | ||
| }); | ||
| } | ||
|
|
||
| } |
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.
crash tests should be moved into a
AnalyserIntegrationTestThere 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.
this test should 1:1 reflect the snippet the user reported in https://phpstan.org/r/fa4c94b1-ab67-442d-8c89-36ea65b86346
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.
Addressed the review — the crash reproducer now lives in
AnalyserIntegrationTest.What changed (commit
0915dc14c):tests/PHPStan/Analyser/nsrt/bug-15003.php→tests/PHPStan/Analyser/data/bug-15003.php, with theassertType()calls dropped (a crash test doesn't need them) and two adjustments so the file analyses cleanly at the integration test's level:InvokableRule::__invoke()got a: voidreturn type (otherwise "has no return type specified");Fieldcalls are wrapped in functions that return the result, instead of being standalone statements ("call on a separate line has no effect").AnalyserIntegrationTest::testBug15003()added,#[RequiresPhp('>= 8.0.0')], asserting no errors.Verified the moved test still catches the bug: with
src/Analyser/NodeScopeResolver.phpandsrc/Reflection/Native/NativeParameterReflection.phpchecked out from the pre-fix commit,testBug15003errors withCall to undefined method PHPStan\Reflection\Php\ExtendedDummyParameter::union(); with the fix it passes.nsrt/closure-passed-to-union-of-callables.phpstays where it is — it asserts inferred closure parameter types (order independence, variadics, theInvokable<string>acceptor), so it's a type inference test rather than a crash test, even though its three-acceptor case exercises the same code path.make tests(17708 tests, 97 skipped) andmake phpstanare both green.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.
Waiting for
make teststo finish; I'll report when it lands.