Fix phpstan/phpstan#14096: Reference is no longer honored since PHPStan 2.1.39#5401
Closed
phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
Closed
Fix phpstan/phpstan#14096: Reference is no longer honored since PHPStan 2.1.39#5401phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
Conversation
…pe after deferred application - When multiple closures capture the same by-ref variable, deferred application caused later closures to overwrite earlier modifications with their stale types - Pass current scope as prevScope when applying deferred by-ref results so types are unioned instead of replaced - Guard processClosureScope against undefined variables in prevScope - New regression test in tests/PHPStan/Analyser/nsrt/bug-14096.php
| } | ||
|
|
||
| public function applyByRefUseScope(MutatingScope $scope): MutatingScope | ||
| public function applyByRefUseScope(MutatingScope $scope, ?MutatingScope $prevScope = null): MutatingScope |
Contributor
There was a problem hiding this comment.
Do not use null as default value, prefer being explicit when calling this method
Collaborator
Author
There was a problem hiding this comment.
Done. Removed the = null default from applyByRefUseScope()'s $prevScope parameter and updated the caller in ClosureHandler.php to explicitly pass null. Both make phpstan and make tests pass.
Make the $prevScope parameter explicit at all call sites instead of using a default null value, as suggested in code review. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
Since 2.1.39, PHPStan incorrectly typed by-reference variables as
nullafter a method call where multiple closure arguments captured the same variable via&$var. The variable's type should reflect modifications made inside any of the closures.Changes
src/Analyser/ProcessClosureResult.php: Added optional$prevScopeparameter toapplyByRefUseScope()so callers can provide the current scope for type mergingsrc/Analyser/NodeScopeResolver.php: Pass$scopeas$prevScopewhen applying deferred by-ref closure results inprocessArgs(), so types from multiple closures are unioned instead of the last one winningsrc/Analyser/MutatingScope.php: GuardprocessClosureScope()against undefined variables in$prevScopeby checkinghasVariableType()beforegetVariableType()Root cause
Commit 996ad98 deferred by-ref closure scope modifications in
processArgs()to prevent them from polluting subsequent argument evaluation. However, when applying the deferred results sequentially at the end, each closure's result overwrote the previous one. If the second closure didn't modify the by-ref variable, its stale type (from before the first closure's modifications) replaced the first closure's correct type. The fix passes the current accumulated scope as$prevScopeso thatprocessClosureScope()unions the types instead of replacing them.Test
Added
tests/PHPStan/Analyser/nsrt/bug-14096.phpreproducing the issue from the playground sample. The test verifies that after a method call with two closures capturing&$view, the variable's type isT|nullrather than justnull.Fixes phpstan/phpstan#14096