Fix #13563: Closure / Arrow functions type difference when an array is reset#4904
Closed
phpstan-bot wants to merge 2 commits into2.1.xfrom
Closed
Fix #13563: Closure / Arrow functions type difference when an array is reset#4904phpstan-bot wants to merge 2 commits into2.1.xfrom
phpstan-bot wants to merge 2 commits into2.1.xfrom
Conversation
…nt scope
- Arrow functions incorrectly preserved narrowed property types on $this from the
parent scope (e.g. after $this->dates = [], the arrow function saw array{} instead
of the PHPDoc type array<int, DateTime>)
- Added invalidateNonReadonlyThisPropertyFetches() to MutatingScope that removes
non-readonly $this property fetch expression types, matching closure behavior
- Updated TemplateTypeTrait::describe() to use a local variable instead of $this->default
inside an arrow function, avoiding the now-correctly-invalidated property narrowing
- New regression test in tests/PHPStan/Analyser/nsrt/bug-13563.php
Fixes phpstan/phpstan#13563
Closes phpstan/phpstan#12912 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
Arrow functions incorrectly inherited narrowed
$thisproperty types from the parent scope. After$this->dates = [](wheredateshas PHPDoc typearray<int, DateTime>), an arrow function would see$this->datesasarray{}instead ofarray<int, DateTime>. Closures already correctly dropped this narrowing. This fix brings arrow function behavior in line with closures for$thisproperty type tracking.Changes
invalidateNonReadonlyThisPropertyFetches()method tosrc/Analyser/MutatingScope.phpthat filters out non-readonly$this->propertyexpression type holdersenterArrowFunctionWithoutReflection()insrc/Analyser/MutatingScope.phpto call this method for both expression types and native expression types when$thisis available and the arrow function is not staticsrc/Type/Generic/TemplateTypeTrait.phpto assign$this->defaultto a local variable before using it in an arrow function, since the property narrowing from the!== nullcheck is no longer preserved inside arrow functions (matching closure behavior)Root cause
enterArrowFunctionWithoutReflection()started with$arrowFunctionScope = $this, inheriting ALL expression types from the parent scope including narrowed property types on$this. In contrast,enterAnonymousFunctionWithoutReflection()(for closures) builds expression types from scratch and only preserves readonly property fetches on$this(lines 2762-2776). Since both closures and arrow functions may be invoked later when properties could have been modified, they should both drop non-readonly$thisproperty narrowing.Test
Added
tests/PHPStan/Analyser/nsrt/bug-13563.phpwhich verifies that both arrow functions and closures resolve$this->datestoarray<int, DateTime>(the PHPDoc type) after$this->dates = [], rather thanarray{}(the narrowed empty array type).Fixes phpstan/phpstan#13563
Closes phpstan/phpstan#12912