Fix #6777: Bogus write-only error when appending to an array-like object in private class property#5102
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Conversation
- In ClassStatementsGatherer, also record a PropertyRead when a PropertyAssignNode is for an ArrayAccess property via array dim fetch (e.g. $this->array[] = $s), because this is semantically $this->array->offsetSet() which reads the property - New regression test in tests/PHPStan/Rules/DeadCode/data/bug-6777.php - Updated existing WriteToCollection test expectations Closes phpstan/phpstan#6777
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 appending to an ArrayAccess object stored in a private class property (e.g.
$this->array[] = $s), PHPStan incorrectly reported "Property is never read, only written." This is a false positive because appending to an ArrayAccess object is semantically$this->array->offsetSet(null, $s)— a method call that reads the property.Changes
src/Node/ClassStatementsGatherer.php: When handling aPropertyAssignNode, check if the assigned expression is aSetOffsetValueTypeExprorSetExistingOffsetValueTypeExpr(indicating array dim fetch assignment) and the property type is an ArrayAccess object. If so, also record aPropertyRead.WriteToCollectiontest expectations intests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.phpto reflect the fix.tests/PHPStan/Rules/DeadCode/data/bug-6777.php.Root cause
In
ClassStatementsGatherer, whenNodeScopeResolverprocesses$this->array[] = $son an ArrayAccess property, it emits aPropertyAssignNodewhich was unconditionally recorded as aPropertyWrite. Meanwhile, thePropertyFetchfor$this->arraywas processed in expression-assign mode, causingClassStatementsGathererto skip recording aPropertyRead. This resulted in the property being tracked as "only written" even though it was being read to calloffsetSet()on the object.The fix detects when a property assignment comes from an array dim fetch operation on an ArrayAccess object and also records a
PropertyRead, since the object must be read from the property to invoke the method.Test
Added
tests/PHPStan/Rules/DeadCode/data/bug-6777.phpwhich reproduces the original issue: a class with a privateArrayObjectproperty that is only appended to via$this->array[] = $s. The test expects no errors.Fixes phpstan/phpstan#6777