Fix phpstan/phpstan#6799: wrongly reported empty array#5206
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#6799: wrongly reported empty array#5206phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Widen variable type to mixed when passed by reference inside an array literal ([&$var, ...])
- This fixes false positives like "Comparison operation > between 0 and 0 is always false" when variables are passed by reference through call_user_func_array
- Added regression test in tests/PHPStan/Analyser/nsrt/bug-6799.php
- The root cause was that ArrayHandler did not account for byRef array items, so PHPStan retained the initial precise type (e.g. array{}) even after a reference was created
Closes phpstan/phpstan#6799
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When a variable is passed by reference inside an array literal (e.g.,
[&$whereFilter, ...]) and that array is passed to a function likecall_user_func_array, PHPStan incorrectly retained the variable's initial precise type (e.g.,array{}for an empty array). This caused false positives such as "Comparison operation > between 0 and 0 is always false" because PHPStan didn't recognize the variable could be modified through the reference.Changes
src/Analyser/ExprHandler/ArrayHandler.phpto assignMixedTypeto variables used in by-reference array items ($arrayItem->byRef), widening their type since a reference is created that could be used to modify the variabletests/PHPStan/Analyser/nsrt/bug-6799.phpthat reproduces the original issue withcall_user_func_arrayand a by-reference array parameterRoot cause
The
ArrayHandler::processExprmethod did not handle thebyRefflag onArrayItemnodes. When processing[&$var, ...], the referenced variable's type remained unchanged in the scope. This meant that after$whereFilter = array()followed bycall_user_func_array([$this, 'method'], [&$whereFilter, ...]), PHPStan still considered$whereFilterto bearray{}(empty array), leading to false positives aboutcount($whereFilter) > 0being always false.The fix follows the same pattern used for
foreachby-reference (foreach ($arr as &$val)), which assignsMixedTypeto the referenced variable since it can be modified through the reference.Test
Added
tests/PHPStan/Analyser/nsrt/bug-6799.phpwhich reproduces the original issue: a class method that usescall_user_func_arraywith a by-reference variable inside the argument array. The test asserts that the variable's type is widened tomixedafter the reference is created, rather than remaining asarray{}.Fixes phpstan/phpstan#6799