-
Notifications
You must be signed in to change notification settings - Fork 554
Fix phpstan/phpstan#7280: array_reduce could infer the precise type of the $initial argument within the callable and in the return type
#5168
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
1
commit into
phpstan:2.1.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-28evfgd
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.
+114
−0
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\Php; | ||
|
|
||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Reflection\FunctionReflection; | ||
| use PHPStan\Reflection\Native\NativeParameterReflection; | ||
| use PHPStan\Reflection\ParameterReflection; | ||
| use PHPStan\Type\ClosureType; | ||
| use PHPStan\Type\FunctionParameterClosureTypeExtension; | ||
| use PHPStan\Type\GeneralizePrecision; | ||
| use PHPStan\Type\MixedType; | ||
| use PHPStan\Type\NullType; | ||
| use PHPStan\Type\Type; | ||
|
|
||
| #[AutowiredService] | ||
| final class ArrayReduceCallbackClosureTypeExtension implements FunctionParameterClosureTypeExtension | ||
| { | ||
|
|
||
| public function isFunctionSupported(FunctionReflection $functionReflection, ParameterReflection $parameter): bool | ||
| { | ||
| return $functionReflection->getName() === 'array_reduce' && $parameter->getName() === 'callback'; | ||
| } | ||
|
|
||
| public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, ParameterReflection $parameter, Scope $scope): ?Type | ||
| { | ||
| $args = $functionCall->getArgs(); | ||
|
|
||
| if (!isset($args[0])) { | ||
| return null; | ||
| } | ||
|
|
||
| $arrayType = $scope->getType($args[0]->value); | ||
| $valueType = $arrayType->getIterableValueType(); | ||
|
|
||
| if (isset($args[2])) { | ||
| $initialType = $scope->getType($args[2]->value); | ||
| } else { | ||
| $initialType = new NullType(); | ||
| } | ||
|
|
||
| $carryType = $initialType->generalize(GeneralizePrecision::templateArgument()); | ||
|
|
||
| return new ClosureType( | ||
| [ | ||
| new NativeParameterReflection('carry', false, $carryType, $parameter->passedByReference(), false, null), | ||
| new NativeParameterReflection('value', false, $valueType, $parameter->passedByReference(), false, null), | ||
| ], | ||
| new MixedType(), | ||
| ); | ||
| } | ||
|
|
||
| } |
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,59 @@ | ||||||
| <?php // lint >= 8.0 | ||||||
|
|
||||||
| declare(strict_types = 1); | ||||||
|
|
||||||
| namespace Bug7280; | ||||||
|
|
||||||
| use function PHPStan\Testing\assertType; | ||||||
|
|
||||||
| // Test 1: Precise carry type from initial argument with constant array shape | ||||||
| $result1 = array_reduce( | ||||||
| ['test1', 'test2'], | ||||||
| static function (array $carry, string $value): array { | ||||||
| assertType("array{starts: array{}, ends: array{}}", $carry); | ||||||
| $carry['starts'][] = $value; | ||||||
| $carry['ends'][] = $value; | ||||||
|
|
||||||
| return $carry; | ||||||
| }, | ||||||
| initial: ['starts' => [], 'ends' => []], | ||||||
| ); | ||||||
|
|
||||||
| // Test 2: Arrow function with precise carry type | ||||||
| $result2 = array_reduce( | ||||||
| [1, 2, 3], | ||||||
| static fn (int $carry, int $value): int => $carry + $value, | ||||||
| 0, | ||||||
| ); | ||||||
| assertType('int', $result2); | ||||||
|
|
||||||
| // Test 3: Carry type with no initial (defaults to null) | ||||||
| $result3 = array_reduce( | ||||||
| [1, 2, 3], | ||||||
| static function (?int $carry, int $value): int { | ||||||
| assertType('null', $carry); | ||||||
|
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.
Suggested change
|
||||||
| return ($carry ?? 0) + $value; | ||||||
| }, | ||||||
| ); | ||||||
|
|
||||||
| // Test 4: Initial value type narrows carry - literal string initial | ||||||
| $result4 = array_reduce( | ||||||
| ['a', 'b', 'c'], | ||||||
| static function (string $carry, string $value): string { | ||||||
| assertType('string', $carry); | ||||||
| return $carry . $value; | ||||||
| }, | ||||||
| '', | ||||||
| ); | ||||||
| assertType('string', $result4); | ||||||
|
|
||||||
| // Test 5: Carry type with literal int initial | ||||||
| $result5 = array_reduce( | ||||||
| [1, 2, 3], | ||||||
| static function (int $carry, int $value): int { | ||||||
| assertType('int', $carry); | ||||||
| return $carry + $value; | ||||||
| }, | ||||||
| 0, | ||||||
| ); | ||||||
| assertType('int', $result5); | ||||||
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.
I think this type is wrong. it only is correct for the first invocation when initial is used.
in this example, I think we need to aim for `assertType("array{starts: array, ends: array}", $carry);