-
Notifications
You must be signed in to change notification settings - Fork 574
Collect all remaining callable parameter types for variadic closure parameters instead of using only the matching index #5634
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
VincentLanglet
merged 4 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-e1mgzvb
May 11, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fa1b686
Collect all remaining callable parameter types for variadic closure p…
VincentLanglet 5b5ae48
Fix
VincentLanglet 8b0fefc
Add rule test for bug-9240 verifying no false positive offset errors
phpstan-bot b984472
Add PHP 7.x test expectations and rule test for bug-9240
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| <?php // lint < 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug9240Php7; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @phpstan-type PhpFileArray array{error: int, name: string} | ||
| */ | ||
| class Upload | ||
| { | ||
| /** | ||
| * @param \Closure(PhpFileArray, PhpFileArray, PhpFileArray): bool $fx | ||
| */ | ||
| public function onUpload(\Closure $fx): bool | ||
| { | ||
| $v = ['error' => 1, 'name' => 'x']; | ||
| $postFiles = [$v, $v, $v]; | ||
|
|
||
| return $fx(...$postFiles); | ||
| } | ||
| } | ||
|
|
||
| function test(): void | ||
| { | ||
| $u = new Upload(); | ||
| $u->onUpload(function (...$postFiles): bool { | ||
| assertType('list<array{error: int, name: string}>', $postFiles); | ||
| foreach ($postFiles as $postFile) { | ||
| assertType('array{error: int, name: string}', $postFile); | ||
| if ($postFile['error'] !== 0) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @param \Closure(int, string, float): void $fx | ||
| */ | ||
| function mixedTypes(\Closure $fx): void | ||
| { | ||
| $fx(1, 'hello', 3.14); | ||
| } | ||
|
|
||
| function testMixedTypes(): void | ||
| { | ||
| mixedTypes(function (...$args): void { | ||
| assertType('list<float|int|string>', $args); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @param \Closure(int, string): void $fx | ||
| */ | ||
| function twoParams(\Closure $fx): void | ||
| { | ||
| $fx(1, 'hello'); | ||
| } | ||
|
|
||
| function testVariadicNotFirst(): void | ||
| { | ||
| twoParams(function (int $first, string ...$rest): void { | ||
| assertType('int', $first); | ||
| assertType('list<string>', $rest); | ||
| }); | ||
| } | ||
|
|
||
| // Arrow function version | ||
| function testArrowFunction(): void | ||
| { | ||
| $u = new Upload(); | ||
| $u->onUpload(fn (...$postFiles) => assertType('list<array{error: int, name: string}>', $postFiles) || true); | ||
| } | ||
|
|
||
| // Immediately-invoked closure with variadic | ||
| function testImmediatelyInvoked(): void | ||
| { | ||
| $result = (function (...$args): string { | ||
| assertType('list<1|3.14|\'hello\'>', $args); | ||
| return implode(', ', $args); | ||
| })(1, 'hello', 3.14); | ||
| } | ||
|
|
||
| // Immediately-invoked arrow function with variadic | ||
| function testImmediatelyInvokedArrow(): void | ||
| { | ||
| $result = (fn (...$args) => assertType('list<1|3.14|\'hello\'>', $args))(1, 'hello', 3.14); | ||
| } | ||
|
|
||
| // Variadic param with last callable parameter also variadic | ||
| /** | ||
| * @param \Closure(string, int...): void $fx | ||
| */ | ||
| function variadicExpected(\Closure $fx): void | ||
| { | ||
| } | ||
|
|
||
| function testVariadicExpected(): void | ||
| { | ||
| variadicExpected(function (...$args): void { | ||
| assertType('list<int|string>', $args); | ||
| }); | ||
| } |
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,108 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug9240; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @phpstan-type PhpFileArray array{error: int, name: string} | ||
| */ | ||
| class Upload | ||
| { | ||
| /** | ||
| * @param \Closure(PhpFileArray, PhpFileArray, PhpFileArray): bool $fx | ||
| */ | ||
| public function onUpload(\Closure $fx): bool | ||
| { | ||
| $v = ['error' => 1, 'name' => 'x']; | ||
| $postFiles = [$v, $v, $v]; | ||
|
|
||
| return $fx(...$postFiles); | ||
| } | ||
| } | ||
|
|
||
| function test(): void | ||
| { | ||
| $u = new Upload(); | ||
| $u->onUpload(function (...$postFiles): bool { | ||
| assertType('array<int|string, array{error: int, name: string}>', $postFiles); | ||
| foreach ($postFiles as $postFile) { | ||
| assertType('array{error: int, name: string}', $postFile); | ||
| if ($postFile['error'] !== 0) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @param \Closure(int, string, float): void $fx | ||
| */ | ||
| function mixedTypes(\Closure $fx): void | ||
| { | ||
| $fx(1, 'hello', 3.14); | ||
| } | ||
|
|
||
| function testMixedTypes(): void | ||
| { | ||
| mixedTypes(function (...$args): void { | ||
| assertType('array<int|string, float|int|string>', $args); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @param \Closure(int, string): void $fx | ||
| */ | ||
| function twoParams(\Closure $fx): void | ||
| { | ||
| $fx(1, 'hello'); | ||
| } | ||
|
|
||
| function testVariadicNotFirst(): void | ||
| { | ||
| twoParams(function (int $first, string ...$rest): void { | ||
| assertType('int', $first); | ||
| assertType('array<int|string, string>', $rest); | ||
| }); | ||
| } | ||
|
|
||
| // Arrow function version | ||
| function testArrowFunction(): void | ||
| { | ||
| $u = new Upload(); | ||
| $u->onUpload(fn (...$postFiles) => assertType('array<int|string, array{error: int, name: string}>', $postFiles) || true); | ||
| } | ||
|
|
||
| // Immediately-invoked closure with variadic | ||
| function testImmediatelyInvoked(): void | ||
| { | ||
| $result = (function (...$args): string { | ||
| assertType('array<int|string, 1|3.14|\'hello\'>', $args); | ||
| return implode(', ', $args); | ||
| })(1, 'hello', 3.14); | ||
| } | ||
|
|
||
| // Immediately-invoked arrow function with variadic | ||
| function testImmediatelyInvokedArrow(): void | ||
| { | ||
| $result = (fn (...$args) => assertType('array<int|string, 1|3.14|\'hello\'>', $args))(1, 'hello', 3.14); | ||
| } | ||
|
|
||
| // Variadic param with last callable parameter also variadic | ||
| /** | ||
| * @param \Closure(string, int...): void $fx | ||
| */ | ||
| function variadicExpected(\Closure $fx): void | ||
| { | ||
| } | ||
|
|
||
| function testVariadicExpected(): void | ||
| { | ||
| variadicExpected(function (...$args): void { | ||
| assertType('array<int|string, int|string>', $args); | ||
| }); | ||
| } | ||
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,36 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug9240Rule; | ||
|
|
||
| /** | ||
| * @phpstan-type PhpFileArray array{error: int, name: string} | ||
| */ | ||
| class Upload | ||
| { | ||
| /** | ||
| * @param \Closure(PhpFileArray, PhpFileArray, PhpFileArray): bool $fx | ||
| */ | ||
| public function onUpload(\Closure $fx): bool | ||
| { | ||
| $v = ['error' => 1, 'name' => 'x']; | ||
| $postFiles = [$v, $v, $v]; | ||
|
|
||
| return $fx(...$postFiles); | ||
| } | ||
| } | ||
|
|
||
| function test(): void | ||
| { | ||
| $u = new Upload(); | ||
| $u->onUpload(function (...$postFiles): bool { | ||
| foreach ($postFiles as $postFile) { | ||
| if ($postFile['error'] !== 0) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| }); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.