Skip to content

Commit 01deed6

Browse files
committed
Fix type-specifying for nested assign in === and match condition
1 parent a4e2bfe commit 01deed6

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,9 +1392,14 @@ public function create(
13921392
}
13931393

13941394
$specifiedExprs = [];
1395+
if ($expr instanceof AlwaysRememberedExpr) {
1396+
$specifiedExprs[] = $expr;
1397+
$expr = $expr->expr;
1398+
}
13951399

13961400
if ($expr instanceof Expr\Assign) {
13971401
$specifiedExprs[] = $expr->var;
1402+
$specifiedExprs[] = $expr->expr;
13981403

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

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,7 @@ public function dataFileAsserts(): iterable
10981098
}
10991099
if (PHP_VERSION_ID >= 80000) {
11001100
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-comparisons-php8.php');
1101+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10084.php');
11011102
}
11021103
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-comparisons.php');
11031104
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7563.php');

tests/PHPStan/Analyser/TypeSpecifierTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,11 @@ public function dataCondition(): iterable
752752
),
753753
[
754754
'$notNullBar' => 'null',
755+
'$barOrNull' => 'null',
755756
],
756757
[
757758
'$notNullBar' => '~null',
759+
'$barOrNull' => '~null',
758760
],
759761
],
760762
[
@@ -874,9 +876,11 @@ public function dataCondition(): iterable
874876
),
875877
[
876878
'$notNullBar' => '~null',
879+
'$barOrNull' => '~null',
877880
],
878881
[
879882
'$notNullBar' => 'null',
883+
'$barOrNull' => 'null',
880884
],
881885
],
882886
[
@@ -901,9 +905,11 @@ public function dataCondition(): iterable
901905
),
902906
[
903907
'$notFalseBar' => 'false & ' . self::SURE_NOT_TRUTHY,
908+
'$barOrFalse' => 'false',
904909
],
905910
[
906911
'$notFalseBar' => '~false',
912+
'$barOrFalse' => '~false',
907913
],
908914
],
909915
[
@@ -949,9 +955,11 @@ public function dataCondition(): iterable
949955
),
950956
[
951957
'$notFalseBar' => '~false',
958+
'$barOrFalse' => '~false',
952959
],
953960
[
954961
'$notFalseBar' => 'false & ' . self::SURE_NOT_TRUTHY,
962+
'$barOrFalse' => 'false',
955963
],
956964
],
957965
[
@@ -964,9 +972,11 @@ public function dataCondition(): iterable
964972
),
965973
[
966974
'$notFalseBar' => 'Bar',
975+
'$barOrFalse' => 'Bar',
967976
],
968977
[
969978
'$notFalseBar' => '~Bar',
979+
'$barOrFalse' => '~Bar',
970980
],
971981
],
972982
[
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Bug10084;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function (?int $val) {
8+
match ($val) {
9+
null => assertType('null', $val),
10+
default => assertType('int', $val),
11+
};
12+
};
13+
14+
function (?int $val) {
15+
match ($foo = $val) {
16+
null => assertType('null', $val),
17+
default => assertType('int', $val),
18+
};
19+
};
20+
21+
function (?int $val) {
22+
match ($foo = $val) {
23+
null => assertType('null', $foo),
24+
default => assertType('int', $foo),
25+
};
26+
};

0 commit comments

Comments
 (0)