Repository navigation
Count a property bound by reference as read when the other side can read it - #7266
Open
ondrejmirtes wants to merge 1 commit into
Open
ondrejmirtes wants to merge 1 commit into
ondrejmirtes wants to merge 1 commit into
Conversation
…ead it A private property bound by reference - a by-ref promoted parameter, $this->prop = &$var, $this->prop['k'] = &$var, self::$prop = &$var - was only written, so it was reported as never read although whatever is written to it can be read through the variable it is bound to. PropertyWrite::isByReference() marks these writes, and UnusedPrivatePropertyRule counts them as reads. No PropertyRead is recorded, so the uninitialized-property checks see no read before the assignment. A binding to a local variable of a method waits for the method's VariableWritesNode and counts only when the method also reads the variable or lets it escape apart from the binding, or the variable is a by-ref or promoted parameter: a binding to a throwaway local is still reported. VariableWritesNode::getObservingAccessCount() gives the number of read and escape sites per variable for that. Closes phpstan/phpstan#15886 Closes phpstan/phpstan#10068 Amp-Thread-ID: https://ampcode.com/threads/T-01a22bbf-fbea-7343-93fe-fd2c23d22096
ondrejmirtes
force-pushed
the
by-ref-property-binding-read
branch
from
November 30, 2026 10:45
3126e8f to
5d0872e
Compare
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.
A private property bound by reference was reported as "never read, only written", although whatever is written to it can be read through the variable it is bound to:
The same holds for
$this->prop = &$var,$this->prop['k'] = &$var(nested offsets too) andself::$prop = &$var.How
PropertyWrite::isByReference()marks the write of a by-ref promoted property and of a reference binding, andUnusedPrivatePropertyRulecounts such a write as a read. NoPropertyReadis recorded, socheckUninitializedPropertiesand the@readonlychecks do not see a read before the property is assigned (Access to an uninitialized propertyon$this->forwarded = &$forwarded).$tmp = []; $this->cache = &$tmp;, a by-value parameter that is not read afterwards).ClassStatementsGathererholds the write until the method'sVariableWritesNodearrives, and counts it when the variable is a by-ref or promoted parameter, or the method reads it or lets it escape apart from the binding (reads it later, passes it on,global,static,use (&$tmp),compact()).VariableWritesNode::getObservingAccessCount()gives the number of read and escape sites, whichVariableLivenessResolverderives from the access lists it already builds, so no method is walked again however many bindings it has.ClassStatementsGathererandVariableLivenessResolverhave the same change in their turbo-ext mirrors.Tests
The fixtures of #7253 and the cases checked in its review: a by-ref closure parameter, a by-ref
foreachvalue, a by-value parameter read after the binding, two properties bound to the same throwaway local (both reported), and a promoted by-value property that is never read (still reported).UninitializedPropertyRuleTestandMissingReadOnlyByPhpDocPropertyAssignRuleTestcover the uninitialized-access regression.Closes phpstan/phpstan#15886
Closes phpstan/phpstan#10068
Closes #7253