Skip to content

Count a property bound by reference as read when the other side can read it - #7266

Open
ondrejmirtes wants to merge 1 commit into
2.3.xfrom
by-ref-property-binding-read
Open

ondrejmirtes wants to merge 1 commit into
2.3.xfrom
by-ref-property-binding-read

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

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:

class Foo
{
	/** @param array<string, bool> $spy */
	public function __construct(private array &$spy) {}

	public function doSomething(): void
	{
		$this->spy['doSomething'] = true; // the caller reads it through $spy
	}
}

The same holds for $this->prop = &$var, $this->prop['k'] = &$var (nested offsets too) and self::$prop = &$var.

How

  • PropertyWrite::isByReference() marks the write of a by-ref promoted property and of a reference binding, and UnusedPrivatePropertyRule counts such a write as a read. No PropertyRead is recorded, so checkUninitializedProperties and the @readonly checks do not see a read before the property is assigned (Access to an uninitialized property on $this->forwarded = &$forwarded).
  • A binding to a local variable of a method is still reported when nothing else in the method uses the variable ($tmp = []; $this->cache = &$tmp;, a by-value parameter that is not read afterwards). ClassStatementsGatherer holds the write until the method's VariableWritesNode arrives, 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, which VariableLivenessResolver derives from the access lists it already builds, so no method is walked again however many bindings it has.
  • Both ClassStatementsGatherer and VariableLivenessResolver have 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 foreach value, 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). UninitializedPropertyRuleTest and MissingReadOnlyByPhpDocPropertyAssignRuleTest cover the uninitialized-access regression.

Closes phpstan/phpstan#15886
Closes phpstan/phpstan#10068
Closes #7253

…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
ondrejmirtes force-pushed the by-ref-property-binding-read branch from 3126e8f to 5d0872e Compare November 30, 2026 10:45
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.

"Property is never read, only written" false positive, when initialized by ref Constructor argument passed by reference is considered only written

1 participant