Fix phpstan/phpstan#13920: Anonymous class causes "variable might not be defined" issue#5205
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#13920: Anonymous class causes "variable might not be defined" issue#5205phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Throw points from anonymous class constructor body carried inner method scopes instead of the outer scope, causing variables defined before try blocks to be reported as "might not be defined" in finally blocks - Added replaceScope() method to InternalThrowPoint to allow scope replacement - New regression test in tests/PHPStan/Rules/Variables/data/bug-13920.php Closes phpstan/phpstan#13920
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 an anonymous class with an explicit constructor calling
parent::__construct()was used inside atryblock, PHPStan incorrectly reported that variables defined before thetryblock "might not be defined" in thefinallyblock.Changes
src/Analyser/ExprHandler/NewHandler.phpto replace the scope on throw points extracted from the anonymous class constructor's statement result with the outer scope (the scope at thenewexpression site)replaceScope()method tosrc/Analyser/InternalThrowPoint.phptests/PHPStan/Rules/Variables/DefinedVariableRuleTest.phpandtests/PHPStan/Rules/Variables/data/bug-13920.phpRoot cause
When processing
new class() extends Foo { public function __construct() { parent::__construct(); } }, theNewHandlerextracted throw points from the constructor's body analysis. These throw points carried scopes from within the constructor method, which naturally didn't contain the outer method's variables. When the try/finally handling merged these throw point scopes into thefinallyScopeviamergeWith(), any outer variable present in the finally scope but absent from the inner constructor scope was downgraded from "definitely defined" (Yescertainty) to "might not be defined" (Maybecertainty).The fix replaces the inner scope on these throw points with the outer
$scopeat the call site, matching the behavior already used for inherited constructors (theelsebranch at line 196, which usesgetConstructorThrowPoint()with the outer scope).Test
Added a regression test that verifies no errors are reported when a variable is defined before a
tryblock containing an anonymous class withparent::__construct(), and that variable is used in thefinallyblock.Fixes phpstan/phpstan#13920