Skip to content

Commit 2422f94

Browse files
committed
fix
1 parent 54db532 commit 2422f94

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/Analyser/MutatingScope.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3771,22 +3771,34 @@ private function enterAnonymousFunctionWithoutReflection(
37713771

37723772
if (
37733773
!$expr instanceof PropertyFetch
3774-
|| !$expr->name instanceof Node\Identifier
3775-
|| !$expr->var instanceof Variable
3776-
|| !is_string($expr->var->name)
3777-
|| $expr->var->name !== 'this'
37783774
) {
37793775
continue;
37803776
}
37813777

3782-
$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($expr, $this);
3783-
if ($propertyReflection === null) {
3784-
continue;
3785-
}
3778+
while ($expr instanceof PropertyFetch) {
3779+
if ($expr->var instanceof Variable) {
3780+
if (
3781+
! $expr->name instanceof Node\Identifier
3782+
|| !is_string($expr->var->name)
3783+
|| $expr->var->name !== 'this'
3784+
) {
3785+
continue 2;
3786+
}
3787+
} elseif (!$expr->var instanceof PropertyFetch) {
3788+
continue 2;
3789+
}
37863790

3787-
$nativePropertyReflection = $propertyReflection->getNativeReflection();
3788-
if ($nativePropertyReflection === null || !$nativePropertyReflection->isReadOnly()) {
3789-
continue;
3791+
$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($expr, $this);
3792+
if ($propertyReflection === null) {
3793+
continue 2;
3794+
}
3795+
3796+
$nativePropertyReflection = $propertyReflection->getNativeReflection();
3797+
if ($nativePropertyReflection === null || !$nativePropertyReflection->isReadOnly()) {
3798+
continue 2;
3799+
}
3800+
3801+
$expr = $expr->var;
37903802
}
37913803

37923804
$expressionTypes[$exprString] = $typeHolder;

tests/PHPStan/Analyser/nsrt/bug-13321b.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
class Foo
88
{
9-
public function __construct(public string $value)
9+
public function __construct(
10+
public string $value,
11+
readonly public string $readonlyValue,
12+
)
1013
{
1114
}
1215
}
@@ -27,13 +30,18 @@ public function bar(): void
2730
if ($this->foo->value === '') {
2831
return;
2932
}
33+
if ($this->foo->readonlyValue === '') {
34+
return;
35+
}
3036

3137
assertType(Foo::class, $this->foo);
3238
assertType('non-empty-string', $this->foo->value);
39+
assertType('non-empty-string', $this->foo->readonlyValue);
3340

3441
$test = function () {
3542
assertType(Foo::class, $this->foo);
3643
assertType('string', $this->foo->value);
44+
assertType('non-empty-string', $this->foo->readonlyValue);
3745
};
3846

3947
$test();

0 commit comments

Comments
 (0)