diff --git a/src/Parser/NewAssignedToPropertyVisitor.php b/src/Parser/NewAssignedToPropertyVisitor.php index 60fd8fd72e..4eb7b93bff 100644 --- a/src/Parser/NewAssignedToPropertyVisitor.php +++ b/src/Parser/NewAssignedToPropertyVisitor.php @@ -16,7 +16,7 @@ final class NewAssignedToPropertyVisitor extends NodeVisitorAbstract #[Override] public function enterNode(Node $node): ?Node { - if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignRef) { + if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignRef || $node instanceof Node\Expr\AssignOp\Coalesce) { if ( ($node->var instanceof Node\Expr\PropertyFetch || $node->var instanceof Node\Expr\StaticPropertyFetch) && $node->expr instanceof Node\Expr\New_ diff --git a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php index 29c2c32aca..e69080835e 100644 --- a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php @@ -1003,4 +1003,15 @@ public function testCloneWith(): void ]); } + #[RequiresPhp('>= 8.0')] + public function testBug12250(): void + { + $this->analyse([__DIR__ . '/data/bug-12250.php'], []); + } + + public function testBug4525(): void + { + $this->analyse([__DIR__ . '/data/bug-4525.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Properties/data/bug-12250.php b/tests/PHPStan/Rules/Properties/data/bug-12250.php new file mode 100644 index 0000000000..4c0ebea53e --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/bug-12250.php @@ -0,0 +1,17 @@ += 8.0 + +namespace Bug12250; + +class HelloWorld +{ + /** + * @var \WeakMap<\stdClass, \stdClass> + */ + protected \WeakMap $bug, $ok; + + public function bug(): void + { + $this->bug ??= new \WeakMap(); + $this->ok = new \WeakMap(); + } +} diff --git a/tests/PHPStan/Rules/Properties/data/bug-4525.php b/tests/PHPStan/Rules/Properties/data/bug-4525.php new file mode 100644 index 0000000000..3441b25c88 --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/bug-4525.php @@ -0,0 +1,24 @@ += 7.4 + +namespace Bug4525; + +use SplObjectStorage; + +class HelloWorld +{ + /** + * @var SplObjectStorage<\DateTime, \DateTimeImmutable> + */ + private SplObjectStorage $map; + + public function sayHello(): void + { + $this->map = new SplObjectStorage(); + } + + /** @phpstan-return SplObjectStorage<\DateTime, \DateTimeImmutable> */ + public function getMap(): SplObjectStorage + { + return $this->map ??= new SplObjectStorage(); + } +}