From 9bec4224f4e7f90244313e50d6e3a30c8dfe89c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20V=C3=A1radi?= Date: Thu, 12 Dec 2024 16:05:19 +0100 Subject: [PATCH] Add coalesce assignments to NewAssignedToPropertyVisitor --- src/Parser/NewAssignedToPropertyVisitor.php | 2 +- .../TypesAssignedToPropertiesRuleTest.php | 5 +++ .../Rules/Properties/data/bug-12250.php | 31 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Rules/Properties/data/bug-12250.php diff --git a/src/Parser/NewAssignedToPropertyVisitor.php b/src/Parser/NewAssignedToPropertyVisitor.php index 209e72730d..2e1cfc7437 100644 --- a/src/Parser/NewAssignedToPropertyVisitor.php +++ b/src/Parser/NewAssignedToPropertyVisitor.php @@ -12,7 +12,7 @@ final class NewAssignedToPropertyVisitor extends NodeVisitorAbstract 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 dede3e9211..35492a29bc 100644 --- a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php @@ -651,6 +651,11 @@ public function testBug11275(): void ]); } + public function testBug12250(): void + { + $this->analyse([__DIR__ . '/data/bug-12250.php'], []); + } + public function testBug11617(): void { $this->checkExplicitMixed = true; 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..10c128150e --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/bug-12250.php @@ -0,0 +1,31 @@ += 7.4 + +namespace Bug12250; + +class Model {} + +/** + * @template T of object|array + */ +class WeakAnalysingMap +{ + /** @var list */ + public array $values = []; +} + +class Reference +{ + /** @var WeakAnalysingMap */ + private static WeakAnalysingMap $analysingTheirModelMap; + + public function createAnalysingTheirModel(): Model + { + self::$analysingTheirModelMap ??= new WeakAnalysingMap(); + + $theirModel = new Model(); + + self::$analysingTheirModelMap->values[] = $theirModel; + + return end(self::$analysingTheirModelMap->values); + } +}