Skip to content

Commit 9bec422

Browse files
committed
Add coalesce assignments to NewAssignedToPropertyVisitor
1 parent 627ad2a commit 9bec422

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/Parser/NewAssignedToPropertyVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class NewAssignedToPropertyVisitor extends NodeVisitorAbstract
1212

1313
public function enterNode(Node $node): ?Node
1414
{
15-
if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignRef) {
15+
if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignRef || $node instanceof Node\Expr\AssignOp\Coalesce) {
1616
if (
1717
($node->var instanceof Node\Expr\PropertyFetch || $node->var instanceof Node\Expr\StaticPropertyFetch)
1818
&& $node->expr instanceof Node\Expr\New_

tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,11 @@ public function testBug11275(): void
651651
]);
652652
}
653653

654+
public function testBug12250(): void
655+
{
656+
$this->analyse([__DIR__ . '/data/bug-12250.php'], []);
657+
}
658+
654659
public function testBug11617(): void
655660
{
656661
$this->checkExplicitMixed = true;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php // lint >= 7.4
2+
3+
namespace Bug12250;
4+
5+
class Model {}
6+
7+
/**
8+
* @template T of object|array<mixed>
9+
*/
10+
class WeakAnalysingMap
11+
{
12+
/** @var list<T> */
13+
public array $values = [];
14+
}
15+
16+
class Reference
17+
{
18+
/** @var WeakAnalysingMap<Model> */
19+
private static WeakAnalysingMap $analysingTheirModelMap;
20+
21+
public function createAnalysingTheirModel(): Model
22+
{
23+
self::$analysingTheirModelMap ??= new WeakAnalysingMap();
24+
25+
$theirModel = new Model();
26+
27+
self::$analysingTheirModelMap->values[] = $theirModel;
28+
29+
return end(self::$analysingTheirModelMap->values);
30+
}
31+
}

0 commit comments

Comments
 (0)