Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 45 additions & 24 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,24 +315,9 @@ private function rememberConstructorExpressions(array $currentExpressionTypes):
continue;
}
} elseif ($expr instanceof PropertyFetch) {
if (
!$expr->name instanceof Node\Identifier
|| !$expr->var instanceof Variable
|| $expr->var->name !== 'this'
|| !$this->phpVersion->supportsReadOnlyProperties()
) {
continue;
}

$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($expr, $this);
if ($propertyReflection === null) {
if (!$this->isReadonlyPropertyFetch($expr, true)) {
continue;
}

$nativePropertyReflection = $propertyReflection->getNativeReflection();
if ($nativePropertyReflection === null || !$nativePropertyReflection->isReadOnly()) {
continue;
}
} elseif (!$expr instanceof ConstFetch && !$expr instanceof PropertyInitializationExpr) {
continue;
}
Expand Down Expand Up @@ -369,6 +354,44 @@ public function rememberConstructorScope(): self
);
}

private function isReadonlyPropertyFetch(PropertyFetch $expr, bool $allowOnlyOnThis): bool
{
if (!$this->phpVersion->supportsReadOnlyProperties()) {
return false;
}

while ($expr instanceof PropertyFetch) {
if ($expr->var instanceof Variable) {
if (
$allowOnlyOnThis
&& (
! $expr->name instanceof Node\Identifier
|| !is_string($expr->var->name)
|| $expr->var->name !== 'this'
)
) {
return false;
}
} elseif (!$expr->var instanceof PropertyFetch) {
return false;
}

$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($expr, $this);
if ($propertyReflection === null) {
return false;
}

$nativePropertyReflection = $propertyReflection->getNativeReflection();
if ($nativePropertyReflection === null || !$nativePropertyReflection->isReadOnly()) {
return false;
}

$expr = $expr->var;
}

return true;
}

/** @api */
public function isInClass(): bool
{
Expand Down Expand Up @@ -4440,14 +4463,12 @@ private function shouldInvalidateExpression(string $exprStringToInvalidate, Expr
return false;
}

if ($this->phpVersion->supportsReadOnlyProperties() && $expr instanceof PropertyFetch && $expr->name instanceof Node\Identifier && $requireMoreCharacters) {
$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($expr, $this);
if ($propertyReflection !== null) {
$nativePropertyReflection = $propertyReflection->getNativeReflection();
if ($nativePropertyReflection !== null && $nativePropertyReflection->isReadOnly()) {
return false;
}
}
if (
$expr instanceof PropertyFetch
&& $requireMoreCharacters
&& $this->isReadonlyPropertyFetch($expr, false)
) {
return false;
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RememberReadOnlyConstructor;

use LogicException;
use function PHPStan\Testing\assertType;

class HelloWorldReadonlyProperty {
Expand Down Expand Up @@ -107,3 +108,38 @@ public function doFoo() {
assertType('4|10', $this->i);
}
}

class Foo {
public readonly int $readonly;
public int $writable;

public function __construct()
{
$this->readonly = 5;
$this->writable = rand(0,1) ? 5 : 10;
}
}

class DeepPropertyFetching {
public readonly ?Foo $prop;

public function __construct() {
$this->prop = new Foo();
if($this->prop->readonly != 5) {
throw new LogicException();
}
if ($this->prop->writable != 5) {
throw new LogicException();
}

assertType(Foo::class, $this->prop);
assertType('5', $this->prop->readonly);
assertType('5', $this->prop->writable);
}

public function doFoo() {
assertType(Foo::class, $this->prop);
assertType('5', $this->prop->readonly);
assertType('int', $this->prop->writable);
}
}
Loading