-
Notifications
You must be signed in to change notification settings - Fork 574
Support call_user_func_array #5286
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| namespace CallUserFuncArrayPhp8; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @template T | ||
| * @param T $t | ||
| * @return T | ||
| */ | ||
| function generic($t) { | ||
| return $t; | ||
| } | ||
|
|
||
| /** | ||
| * @template T | ||
| * @param T $t | ||
| * @return T | ||
| */ | ||
| function generic3($t = '', int $b = 100, string $c = '') { | ||
| return $t; | ||
| } | ||
|
|
||
|
|
||
| function fun3($a = '', $b = '', $c = ''): int { | ||
| return 1; | ||
| } | ||
|
|
||
| class Foo { | ||
| function doNamed() { | ||
| assertType('1', call_user_func_array('CallUserFuncPhp8\generic', ['t' => 1])); | ||
| assertType('array{1, 2, 3}', call_user_func_array('CallUserFuncPhp8\generic', ['t' => [1, 2, 3]])); | ||
|
|
||
| assertType('array{1, 2, 3}', call_user_func_array('CallUserFuncPhp8\generic3', ['t' => [1, 2, 3]])); | ||
| assertType('\'\'', call_user_func_array('CallUserFuncPhp8\generic3', ['b' => 150])); | ||
| assertType('\'\'', call_user_func_array('CallUserFuncPhp8\generic3', ['c' => 'lala'])); | ||
| assertType('\'\'', call_user_func_array(args: ['c' => 'lala'], callback: 'CallUserFuncPhp8\generic3')); | ||
|
|
||
| assertType('int', call_user_func_array('CallUserFuncPhp8\fun3', ['a' => [1, 2, 3]])); | ||
| assertType('int', call_user_func_array('CallUserFuncPhp8\fun3', ['b' => [1, 2, 3]])); | ||
| assertType('int', call_user_func_array('CallUserFuncPhp8\fun3', ['c' => [1, 2, 3]])); | ||
| assertType('int', call_user_func_array('CallUserFuncPhp8\fun3', ['a' => [1, 2, 3], 'c' => 'c'])); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
|
|
||
| namespace CallUserFuncArray; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @template T | ||
| * @param T $t | ||
| * @return T | ||
| */ | ||
| function generic($t) { | ||
| return $t; | ||
| } | ||
|
|
||
| function fun(): int | ||
| { | ||
| return 3; | ||
| } | ||
|
|
||
| function fun3($i, $x, $y): int | ||
| { | ||
| return 3; | ||
| } | ||
|
|
||
| class c { | ||
| static function m(): string | ||
| { | ||
| return 'hello'; | ||
| } | ||
| } | ||
|
|
||
| class Foo { | ||
| function proxy() { | ||
| $params = [ | ||
| 'CallUserFunc\generic', | ||
| [123, 456], | ||
| ]; | ||
|
|
||
| assertType('mixed', call_user_func_array(...$params)); | ||
| } | ||
|
|
||
| /** | ||
| * @param string[] $strings | ||
| */ | ||
| function doFunc($strings) { | ||
| assertType('true', call_user_func_array('CallUserFunc\generic', [true])); | ||
| assertType('\'hello\'', call_user_func_array('CallUserFunc\generic', ['hello'])); | ||
| assertType('array<string>', call_user_func_array('CallUserFunc\generic', [$strings])); | ||
|
|
||
| assertType('int', call_user_func_array('CallUserFunc\fun', [])); | ||
| assertType('int', call_user_func_array('CallUserFunc\fun3', [1 ,2 ,3])); | ||
| assertType('string', call_user_func_array(['CallUserFunc\c', 'm'], [])); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1128,7 +1128,7 @@ public function testCallUserFuncArray(): void | |
| ], | ||
| ]; | ||
| } | ||
| $this->analyse([__DIR__ . '/data/call-user-func-array.php'], $errors); | ||
| $this->analyse([__DIR__ . '/data/call-user-func-array-named-args.php'], $errors); | ||
| } | ||
|
Comment on lines
1130
to
1132
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just renamed the initial
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interessting.. the PR does not include a file which got renamed?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| public function testFirstClassCallables(): void | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <?php | ||
|
|
||
| call_user_func_array('array_merge', ['foo' => ['bar' => 2]]); |

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.
since the following loop might return early, we could move this 4 lines nearer to the usage in line 174 to prevent unnecessary work