Skip to content

Fix #13563: Closure / Arrow functions type difference when an array is reset#4904

Closed
phpstan-bot wants to merge 2 commits into2.1.xfrom
create-pull-request/patch-uctu3ku
Closed

Fix #13563: Closure / Arrow functions type difference when an array is reset#4904
phpstan-bot wants to merge 2 commits into2.1.xfrom
create-pull-request/patch-uctu3ku

Conversation

@phpstan-bot
Copy link
Collaborator

@phpstan-bot phpstan-bot commented Feb 12, 2026

Summary

Arrow functions incorrectly inherited narrowed $this property types from the parent scope. After $this->dates = [] (where dates has PHPDoc type array<int, DateTime>), an arrow function would see $this->dates as array{} instead of array<int, DateTime>. Closures already correctly dropped this narrowing. This fix brings arrow function behavior in line with closures for $this property type tracking.

Changes

  • Added invalidateNonReadonlyThisPropertyFetches() method to src/Analyser/MutatingScope.php that filters out non-readonly $this->property expression type holders
  • Updated enterArrowFunctionWithoutReflection() in src/Analyser/MutatingScope.php to call this method for both expression types and native expression types when $this is available and the arrow function is not static
  • Updated src/Type/Generic/TemplateTypeTrait.php to assign $this->default to a local variable before using it in an arrow function, since the property narrowing from the !== null check 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 $this property narrowing.

Test

Added tests/PHPStan/Analyser/nsrt/bug-13563.php which verifies that both arrow functions and closures resolve $this->dates to array<int, DateTime> (the PHPDoc type) after $this->dates = [], rather than array{} (the narrowed empty array type).

Fixes phpstan/phpstan#13563

Closes phpstan/phpstan#12912

ondrejmirtes and others added 2 commits February 12, 2026 15:18
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Closure / Arrow functions type difference when an array is reset Difference between Arrow function and Closure

2 participants