Skip to content

Commit 405d819

Browse files
committed
fix
1 parent 8fa9c86 commit 405d819

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
@@ -3741,22 +3741,34 @@ private function enterAnonymousFunctionWithoutReflection(
37413741

37423742
if (
37433743
!$expr instanceof PropertyFetch
3744-
|| !$expr->name instanceof Node\Identifier
3745-
|| !$expr->var instanceof Variable
3746-
|| !is_string($expr->var->name)
3747-
|| $expr->var->name !== 'this'
37483744
) {
37493745
continue;
37503746
}
37513747

3752-
$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($expr, $this);
3753-
if ($propertyReflection === null) {
3754-
continue;
3755-
}
3748+
while ($expr instanceof PropertyFetch) {
3749+
if ($expr->var instanceof Variable) {
3750+
if (
3751+
! $expr->name instanceof Node\Identifier
3752+
|| !is_string($expr->var->name)
3753+
|| $expr->var->name !== 'this'
3754+
) {
3755+
continue 2;
3756+
}
3757+
} elseif (!$expr->var instanceof PropertyFetch) {
3758+
continue 2;
3759+
}
37563760

3757-
$nativePropertyReflection = $propertyReflection->getNativeReflection();
3758-
if ($nativePropertyReflection === null || !$nativePropertyReflection->isReadOnly()) {
3759-
continue;
3761+
$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($expr, $this);
3762+
if ($propertyReflection === null) {
3763+
continue 2;
3764+
}
3765+
3766+
$nativePropertyReflection = $propertyReflection->getNativeReflection();
3767+
if ($nativePropertyReflection === null || !$nativePropertyReflection->isReadOnly()) {
3768+
continue 2;
3769+
}
3770+
3771+
$expr = $expr->var;
37603772
}
37613773

37623774
$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)