Skip to content

Commit c2ba341

Browse files
committed
Fix $var ?? null
1 parent 093bbb2 commit c2ba341

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ public function specifyTypesInCondition(
763763
);
764764
}
765765

766-
if ((new ConstantBooleanType(false))->isSuperTypeOf($scope->getType($expr->right))->yes()) {
766+
if ((new ConstantBooleanType(false))->isSuperTypeOf($scope->getType($expr->right)->toBoolean())->yes()) {
767767
return $this->create(
768768
$expr->left,
769769
new NullType(),

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ public function dataFileAsserts(): iterable
415415
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-987.php');
416416
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3677.php');
417417
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4215.php');
418+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10224.php');
418419
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4695.php');
419420
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2977.php');
420421
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3190.php');
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Bug10224;
4+
5+
use PHPStan\TrinaryLogic;
6+
use function PHPStan\Testing\assertType;
7+
use function PHPStan\Testing\assertVariableCertainty;
8+
9+
function foo(bool $condition): ?string
10+
{
11+
if ($condition) {
12+
$foo = 'bar';
13+
}
14+
if ($foo ?? null) {
15+
assertVariableCertainty(TrinaryLogic::createYes(), $foo);
16+
return $foo;
17+
}
18+
return null;
19+
}
20+
21+
function bar(bool $condition, $a): ?string
22+
{
23+
if ($condition) {
24+
$foo = $a;
25+
}
26+
if ($foo ?? null) {
27+
assertVariableCertainty(TrinaryLogic::createYes(), $foo);
28+
assertType("mixed~null", $foo);
29+
return $foo;
30+
}
31+
return null;
32+
}

0 commit comments

Comments
 (0)