diff --git a/src/Analyser/TypeSpecifier.php b/src/Analyser/TypeSpecifier.php index 27fee51d78..1280e3fedc 100644 --- a/src/Analyser/TypeSpecifier.php +++ b/src/Analyser/TypeSpecifier.php @@ -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; diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 009217b5bd..ac945e2ed3 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -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'); diff --git a/tests/PHPStan/Analyser/TypeSpecifierTest.php b/tests/PHPStan/Analyser/TypeSpecifierTest.php index ee0d1e5f4f..1d9bdebb1c 100644 --- a/tests/PHPStan/Analyser/TypeSpecifierTest.php +++ b/tests/PHPStan/Analyser/TypeSpecifierTest.php @@ -752,9 +752,11 @@ public function dataCondition(): iterable ), [ '$notNullBar' => 'null', + '$barOrNull' => 'null', ], [ '$notNullBar' => '~null', + '$barOrNull' => '~null', ], ], [ @@ -874,9 +876,11 @@ public function dataCondition(): iterable ), [ '$notNullBar' => '~null', + '$barOrNull' => '~null', ], [ '$notNullBar' => 'null', + '$barOrNull' => 'null', ], ], [ @@ -901,9 +905,11 @@ public function dataCondition(): iterable ), [ '$notFalseBar' => 'false & ' . self::SURE_NOT_TRUTHY, + '$barOrFalse' => 'false', ], [ '$notFalseBar' => '~false', + '$barOrFalse' => '~false', ], ], [ @@ -949,9 +955,11 @@ public function dataCondition(): iterable ), [ '$notFalseBar' => '~false', + '$barOrFalse' => '~false', ], [ '$notFalseBar' => 'false & ' . self::SURE_NOT_TRUTHY, + '$barOrFalse' => 'false', ], ], [ @@ -964,9 +972,11 @@ public function dataCondition(): iterable ), [ '$notFalseBar' => 'Bar', + '$barOrFalse' => 'Bar', ], [ '$notFalseBar' => '~Bar', + '$barOrFalse' => '~Bar', ], ], [ diff --git a/tests/PHPStan/Analyser/data/bug-10084.php b/tests/PHPStan/Analyser/data/bug-10084.php new file mode 100644 index 0000000000..cce0d15707 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-10084.php @@ -0,0 +1,26 @@ + 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), + }; +};