Skip to content

Commit 0a96fca

Browse files
authored
Keep non nullability if right expr is NeverType (#1063)
1 parent cf27b2b commit 0a96fca

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2283,7 +2283,11 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
22832283
$hasYield = $result->hasYield();
22842284
$throwPoints = $result->getThrowPoints();
22852285
$scope = $result->getScope();
2286-
$scope = $this->revertNonNullability($scope, $nonNullabilityResult->getSpecifiedExpressions());
2286+
2287+
$rightExprType = $scope->getType($expr->right);
2288+
if (!$rightExprType instanceof NeverType || !$rightExprType->isExplicit()) {
2289+
$scope = $this->revertNonNullability($scope, $nonNullabilityResult->getSpecifiedExpressions());
2290+
}
22872291

22882292
if ($expr->left instanceof PropertyFetch || $expr->left instanceof Expr\NullsafePropertyFetch) {
22892293
$scope = $this->lookForExitVariableAssign($scope, $expr->left->var);

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public function dataFileAsserts(): iterable
155155
yield from $this->gatherAssertTypes(__DIR__ . '/data/pow.php');
156156
if (PHP_VERSION_ID >= 80000 || self::$useStaticReflectionProvider) {
157157
yield from $this->gatherAssertTypes(__DIR__ . '/data/throw-expr.php');
158+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5351.php');
158159
}
159160

160161
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-array.php');
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php // lint >= 8.0
2+
3+
namespace Bug5351;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
public function doFoo(?string $html): void
11+
{
12+
$html ?? throw new \Exception();
13+
assertType('string', $html);
14+
}
15+
16+
/**
17+
* @return never
18+
*/
19+
public function neverReturn() {
20+
throw new \Exception();
21+
}
22+
23+
public function doBar(?string $html): void
24+
{
25+
$html ?? $this->neverReturn();
26+
assertType('string', $html);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)