Skip to content

Commit

Permalink
Fix type-specifying for nested assign in === and match condition
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 2, 2023
1 parent a4e2bfe commit 01deed6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1392,9 +1392,14 @@ public function create(
}

$specifiedExprs = [];
if ($expr instanceof AlwaysRememberedExpr) {
$specifiedExprs[] = $expr;
$expr = $expr->expr;
}

if ($expr instanceof Expr\Assign) {
$specifiedExprs[] = $expr->var;
$specifiedExprs[] = $expr->expr;

while ($expr->expr instanceof Expr\Assign) {
$specifiedExprs[] = $expr->expr->var;
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,7 @@ public function dataFileAsserts(): iterable
}
if (PHP_VERSION_ID >= 80000) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-comparisons-php8.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10084.php');
}
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-comparisons.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7563.php');
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/TypeSpecifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,11 @@ public function dataCondition(): iterable
),
[
'$notNullBar' => 'null',
'$barOrNull' => 'null',
],
[
'$notNullBar' => '~null',
'$barOrNull' => '~null',
],
],
[
Expand Down Expand Up @@ -874,9 +876,11 @@ public function dataCondition(): iterable
),
[
'$notNullBar' => '~null',
'$barOrNull' => '~null',
],
[
'$notNullBar' => 'null',
'$barOrNull' => 'null',
],
],
[
Expand All @@ -901,9 +905,11 @@ public function dataCondition(): iterable
),
[
'$notFalseBar' => 'false & ' . self::SURE_NOT_TRUTHY,
'$barOrFalse' => 'false',
],
[
'$notFalseBar' => '~false',
'$barOrFalse' => '~false',
],
],
[
Expand Down Expand Up @@ -949,9 +955,11 @@ public function dataCondition(): iterable
),
[
'$notFalseBar' => '~false',
'$barOrFalse' => '~false',
],
[
'$notFalseBar' => 'false & ' . self::SURE_NOT_TRUTHY,
'$barOrFalse' => 'false',
],
],
[
Expand All @@ -964,9 +972,11 @@ public function dataCondition(): iterable
),
[
'$notFalseBar' => 'Bar',
'$barOrFalse' => 'Bar',
],
[
'$notFalseBar' => '~Bar',
'$barOrFalse' => '~Bar',
],
],
[
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/data/bug-10084.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Bug10084;

use function PHPStan\Testing\assertType;

function (?int $val) {
match ($val) {
null => assertType('null', $val),
default => assertType('int', $val),
};
};

function (?int $val) {
match ($foo = $val) {
null => assertType('null', $val),
default => assertType('int', $val),
};
};

function (?int $val) {
match ($foo = $val) {
null => assertType('null', $foo),
default => assertType('int', $foo),
};
};

0 comments on commit 01deed6

Please sign in to comment.