Repository navigation
Conversation
…n scope When a condition is stored in a variable, the conditional holders for the narrowed expressions intersect the narrowing with the expression's current type. That type was read from the stored result of the subexpression, but the specified types of `$a['k'] === null || is_int($a['k'])` carry the `$a['k']` node from the right operand, which was walked on the left's falsey branch and so has the type mixed~null. Intersecting it with int|null dropped null, and `if ($ok)` then narrowed `$a['k']` to int. The stored result is now only used when it answers on the assign scope; otherwise the expression is re-priced on that scope. Closes phpstan/phpstan#15479
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.
Closes phpstan/phpstan#15479. Thanks @PupFuzz for the report and the version table.
When a condition is assigned to a variable,
AssignHandleradds conditional holders for each expression the condition narrows. The holder type is the narrowing intersected with the expression's current type, andcurrentTypeForConditionalHolder()read that current type from the subexpression's stored result.For
$ok = $a['k'] === null || is_int($a['k']), the truthy specified types holdint|nullfor$a['k'], but the node they carry is the$a['k']from the right operand. That node was walked on the left operand's falsey branch, so its stored type ismixed~null. Intersecting the two gaveint, andif ($ok)narrowed$a['k']toint. Variables were not affected because they are read from the scope state, which is whyi()in the issue still worked. 2.2.17 used$scope->getType($expr)here.The stored result is now used only when
answersOnScope()holds for the assign scope. Otherwise the expression is processed on demand on that scope, the same waySwitchHandler,MatchHandlerandCoalesceCompositionHelperhandle a stored result that does not answer. The change is mirrored inturbo-ext/src/AssignHandler.cpp, followed by the expected turbo version bump.tests/PHPStan/Analyser/nsrt/bug-15479.phpuses the code from the issue. It fails without the fix (f()andg()giveint) and passes with it, with and without the Turbo extension loaded.