Skip to content

Treat a reference to a property element as a reference to the property - #6648

Open
Cayan wants to merge 1 commit into
phpstan:2.3.xfrom
Cayan:readonly-property-dim-fetch-by-ref
Open

Cayan wants to merge 1 commit into
phpstan:2.3.xfrom
Cayan:readonly-property-dim-fetch-by-ref

Conversation

@Cayan

@Cayan Cayan commented Oct 1, 2026

Copy link
Copy Markdown

Fixes phpstan/phpstan#14243

$a = &$this->prop['key'] modifies the property itself, but only $a = &$this->prop was reported. The rules now look through array dims (nested and [] too) to the property fetch, for native readonly, @readonly and asymmetric visibility. Without the last one, &$obj->ro['x'] from outside the class would be reported on PHP 8.2 but not on 8.4+, where PropertyAssignRefRule reports readonly writes from outside.

Elements of a property holding an ArrayAccess object are skipped, since they go through offsetGet() and PHP does not throw; a type that is only maybe ArrayAccess (e.g. array|ArrayAccess) is still reported, the same way PropertyAssignNode::isArrayAccessOffsetWrite() treats it. I checked the runtime behaviour of each case on PHP 8.2 and 8.5. Messages and identifiers are unchanged.

- `$a = &$this->prop['key']` modifies the property itself, so readonly, @readonly and
  asymmetric-visibility properties now report it the same way as `$a = &$this->prop`, including
  nested and appended dims.
- Elements of a property that holds an ArrayAccess object are not reported: they are reached
  through offsetGet() and do not touch the property. A type that may be ArrayAccess is reported.
- The three rules share one lookup in PropertyReflectionFinder.
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.

Missing error when attempting to fetch by reference an array element of a readonly property

1 participant