Fix phpstan/phpstan#10345: Array inside object used in anonymous function assumed to be empty ("Empty array passed to foreach")#5196
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
d1de9eb to
25046b7
Compare
|
Testing something. Are you going to see this in a review? |
ondrejmirtes
left a comment
There was a problem hiding this comment.
Hey, this is a review. Let me know if you see this.
|
Looking at the review, it appears ondrejmirtes is testing the review notification system rather than requesting actual code changes. The review body says:
And the earlier comment says:
There are no specific code changes requested in this review. The reviewer seems to be verifying that the PR automation (likely this bot) can detect and respond to reviews. No code changes are needed since no actionable feedback was provided. If there are actual code review comments or change requests to address, please share them and I'll be happy to implement the changes. |
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