Fix phpstan/phpstan#10345: Array inside object used in anonymous function assumed to be empty ("Empty array passed to foreach")#5196
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…ject use - Don't carry forward property fetch expression types for dynamic (undeclared) properties on objects captured by value in closures - Objects are references in PHP, so their properties can change between closure definition and invocation - Only affects dynamic properties (e.g. stdClass); declared/native properties still carry forward type narrowings - New regression test in tests/PHPStan/Rules/Arrays/data/bug-10345.php Closes phpstan/phpstan#10345
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 object with a dynamic property (e.g.
stdClass) was captured byusein an anonymous function, PHPStan incorrectly froze the property's type at closure definition time. This caused false positives like "Empty array passed to foreach" when the property was modified between closure definition and invocation.Changes
shouldNotCarryForwardPropertyFetchInClosure()method insrc/Analyser/MutatingScope.phpto check whether a PropertyFetch expression type should be excluded from forwarding into closure scopestdClass)MethodCall::$name) still carry forward type narrowings frominstanceofcheckstests/PHPStan/Rules/Arrays/data/bug-10345.phpand test method inDeadForeachRuleTest.phpRoot cause
In
MutatingScope::enterAnonymousFunctionWithoutReflection(), expression type holders for non-ref captured variables were carried forward into the closure scope, including PropertyFetch expressions. For objects captured by value, this froze the property type at closure definition time. Since PHP objects are always references (even when captured by value viause), the object's properties can be modified externally before the closure is invoked. Dynamic properties (not declared in the class) are particularly prone to this because their types come entirely from assignment context rather than class declarations.The fix checks whether the property is a native/declared property on the class. For dynamic properties (like on
stdClass), the expression type holder is not carried forward, allowing the property to resolve to its natural type (mixed) inside the closure. For declared properties, type narrowings (e.g., frominstanceofchecks) are still carried forward as before.Test
Added
testBug10345inDeadForeachRuleTestwith a test data file that reproduces the exact scenario from the issue: astdClasswith an empty array property captured by a closure, where the property is later populated before invocation. The test expects no errors.Fixes phpstan/phpstan#10345