Report indirect modification of readonly properties through array dim fetch chains#5489
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Report indirect modification of readonly properties through array dim fetch chains#5489phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
… fetch chains - Add new ReadOnlyPropertyIndirectModificationRule that processes PropertyAssignNode - Walk up the var chain from the assigned PropertyFetch to detect intermediate ArrayDimFetch on readonly (native or @readonly) properties - Skip properties implementing ArrayAccess (offsetSet is a method call, not array modification) - Handle both native readonly and @readonly by PHPDoc properties - Respect isAllowedPrivateMutation() for @readonly properties - Cover analogous cases: deeper chains, compound assignments, nested array dim fetches
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
PHP prevents indirect modification of readonly properties even when the modification target is a property on an object accessed through the readonly array. For example,
$this->readonlyArr[$key]->brand[$key2] = valuethrows "Cannot indirectly modify readonly property" at runtime, but PHPStan did not report this error. The existingReadOnlyPropertyAssignRuleonly checked the directly assigned property (brand), not intermediate readonly properties in the assignment chain.Changes
ReadOnlyPropertyIndirectModificationRuleinsrc/Rules/Properties/ReadOnlyPropertyIndirectModificationRule.phpPropertyAssignNodeand walks up thevarchain from the assignedPropertyFetchArrayDimFetch→PropertyFetchpatterns where the property is readonlyreadonlyand@readonlyby PHPDoc propertiesArrayAccessimplementations (their[]access is viaoffsetGet/offsetSetmethods)isAllowedPrivateMutation()for@readonlypropertiesRoot cause
The
AssignHandlerunwindsArrayDimFetchnodes to find the base variable for creatingPropertyAssignNode. For$this->readonlyArr[$key]->brand[$key2] = value, it unwinds the outer[$key2]and findsPropertyFetch(brand)as the base — emitting aPropertyAssignNodeforbrand, not forreadonlyArr. Sincebrandis not readonly, no error was reported. The intermediateArrayDimFetch(PropertyFetch(readonlyArr), $key)was never checked for readonly status.Analogous cases probed
All of these are covered by the new rule and tests:
$this->readonlyArr[$key]->prop[$key2] = value$this->readonlyArr[$key]->prop = value$this->readonlyArr[$key1][$key2]->prop[$key3] = value$this->readonlyArr[$key]->prop .= 'x'$this->readonlyArr[$key]->obj->prop = value@readonlyby PHPDoc property — same pattern with@readonlyannotation@readonlyclass — readonly class with array propertyCases verified as already correct (no false positives):
$this->normalArr[$key]->prop = value— no error$this->readonlyArrayObject[$key]->prop = value— no error$this->readonlyObj->prop = value— no error (objects use reference semantics)Test
tests/PHPStan/Rules/Properties/data/bug-14481.phpwith comprehensive test casestests/PHPStan/Rules/Properties/ReadOnlyPropertyIndirectModificationRuleTest.phpFixes phpstan/phpstan#14481