Use before-scope for evaluating array_splice argument types#5682
Conversation
VincentLanglet
left a comment
There was a problem hiding this comment.
Would it work if we keep trace of
$scopeBeforeArgs = $scope;
before the
$argsResult = $nodeScopeResolver->processArgs($stmt, $functionReflection, null, $parametersAcceptor, $normalizedExpr, $scope, $storage, $nodeCallbackForArgs, $context);
call line 268 ?
|
Done. I implemented the reviewer's suggestion: instead of per-argument |
VincentLanglet
left a comment
There was a problem hiding this comment.
Why not just
new NativeTypeExpr(
$this->getArrayFunctionAppendingType($functionReflection, $scopeBeforeArgs, $normalizedExpr),
$this->getArrayFunctionAppendingType($functionReflection, $scopeBeforeArgs->doNotTreatPhpDocTypesAsCertain(), $normalizedExpr),
|
Done. I implemented the reviewer's suggestion: instead of pre-computing argument types from |
031f8c4 to
63ef184
Compare
array_push/array_unshift/array_splice argument typesarray_splice argument types
…plice` argument types - When `array_unshift($this->arr, array_pop($this->arr))` was processed, `getArrayFunctionAppendingType` re-evaluated argument types using `$scope->getType()` on the post-args scope. By that point, the inner `array_pop` had already modified `$this->arr` to be possibly empty, causing `array_pop` to be re-evaluated as returning `T|null` instead of `T`. - Pre-compute argument types from `ExpressionResultStorage::findBeforeScope()` which gives the scope that existed when each argument was originally evaluated. Pass pre-computed PHPDoc and native types separately to `getArrayFunctionAppendingType`. - Apply the same fix to `array_splice` handler, which also re-evaluated offset, length, and replacement argument types in the post-args scope. - Probed sort/shuffle/arsort handlers: these only read the array argument itself (not other args from the scope), so they are not affected.
63ef184 to
84aeaa3
Compare
The NSRT test already covers the type inference regression. The separate rule test is unnecessary. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
staabm
left a comment
There was a problem hiding this comment.
do we need the same fix for more functions in this handler?
(separate PR)
When I ran the bot, it only opened the PR "Use before-scope for evaluating array_push/array_unshift/array_splice argument types" ; so I'm not sure there is more |
…n#5682) Co-authored-by: VincentLanglet <9052536+VincentLanglet@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: VincentLanglet <9052536+VincentLanglet@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Same than #5579 for array_splice.
Fixes phpstan/phpstan#13510