Repository navigation
Keep the narrowed scope of a ternary branch when a changed node is refreshed - #9007
Merged
TomasVotruba merged 1 commit intoNov 20, 2026
Conversation
TomasVotruba
approved these changes
Nov 20, 2026
Member
|
LGTM, thank you 👍 |
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.
PHPStan stores the narrowed scope on each branch of a ternary.
processTernary()then sets the scope of both branch nodes to the scope of the whole ternary, which has noinstanceofnarrowing. Only the branch node itself loses the narrowing; the nodes inside it keep their own scope, so rules still see the right type. When a rule changes the call in a branch, Rector refreshes the call from the scope stored on it, and the narrowing is lost for the nodes inside as well.For example,
$checker instanceof WiderChecker ? $checker->check($value, $attempts, $timedOut) : $checker->check($value), whereWiderChecker::check()adds two parameters toParentChecker::check($value).RemoveExtraParametersRectoralone leaves the call as it is. When another rule changes the call first,RemoveExtraParametersRectorreadsParentChecker::check()and removes$attemptsand$timedOut. I hit this with a rule that adds argument names. The same call in anifblock or behind&&is not affected.processTernary()now sets the scope of a branch only when the branch has none, the same asprocessBinaryOp()does for&&and||. A branch without a scope still gets the scope of the ternary, as before.The new test fails on main and passes with this change. The rest of the test suite gives the same result as on main.