Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support inferring new templated type from initial null coalesce assignment #3728

Draft
wants to merge 1 commit into
base: 2.0.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Parser/NewAssignedToPropertyVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-12250.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php // lint >= 7.4

namespace Bug12250;

class Model {}

/**
* @template T of object|array<mixed>
*/
class WeakAnalysingMap
{
/** @var list<T> */
public array $values = [];
}

class Reference
{
/** @var WeakAnalysingMap<Model> */
private static WeakAnalysingMap $analysingTheirModelMap;

public function createAnalysingTheirModel(): Model
{
self::$analysingTheirModelMap ??= new WeakAnalysingMap();

$theirModel = new Model();

self::$analysingTheirModelMap->values[] = $theirModel;

return end(self::$analysingTheirModelMap->values);
}
}
Loading