Skip to content

Commit 6a22dbc

Browse files
committed
only in immediately invoked
1 parent e970533 commit 6a22dbc

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/Analyser/MutatingScope.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
use PHPStan\Node\Printer\ExprPrinter;
5151
use PHPStan\Node\PropertyAssignNode;
5252
use PHPStan\Parser\ArrayMapArgVisitor;
53+
use PHPStan\Parser\ImmediatelyInvokedClosureVisitor;
5354
use PHPStan\Parser\NewAssignedToPropertyVisitor;
5455
use PHPStan\Parser\Parser;
5556
use PHPStan\Php\PhpVersion;
@@ -3714,6 +3715,7 @@ private function enterAnonymousFunctionWithoutReflection(
37143715
&& $expr->var instanceof Variable
37153716
&& is_string($expr->var->name)
37163717
&& $expr->var->name === 'this'
3718+
&& $closure->getAttribute(ImmediatelyInvokedClosureVisitor::ATTRIBUTE_NAME) === true
37173719
&& !$closure->static
37183720
&& $this->hasVariableType('this')->yes()
37193721
&& $this->phpVersion->supportsReadOnlyProperties()

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ public function bar(): void
2929
return;
3030
}
3131

32-
$test = function () {
32+
(function () {
3333
assertType(Foo::class, $this->foo);
3434
assertType(Foo::class.'|null', $this->writableFoo);
3535

36+
echo $this->foo->value;
37+
})();
38+
39+
$test = function () {
40+
assertType(Foo::class.'|null', $this->foo);
41+
assertType(Foo::class.'|null', $this->writableFoo);
42+
3643
echo $this->foo->value;
3744
};
3845

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function bar(): void
3232
assertType('non-empty-string', $this->foo->value);
3333

3434
$test = function () {
35-
assertType(Foo::class, $this->foo);
35+
assertType(Foo::class.'|null', $this->foo);
3636
assertType('string', $this->foo->value);
3737
};
3838

0 commit comments

Comments
 (0)