Skip to content

Commit

Permalink
Fix $var ?? null
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 29, 2023
1 parent 093bbb2 commit c2ba341
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ public function specifyTypesInCondition(
);
}

if ((new ConstantBooleanType(false))->isSuperTypeOf($scope->getType($expr->right))->yes()) {
if ((new ConstantBooleanType(false))->isSuperTypeOf($scope->getType($expr->right)->toBoolean())->yes()) {
return $this->create(
$expr->left,
new NullType(),
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 @@ -415,6 +415,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-987.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3677.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4215.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10224.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4695.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2977.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3190.php');
Expand Down
32 changes: 32 additions & 0 deletions tests/PHPStan/Analyser/data/bug-10224.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Bug10224;

use PHPStan\TrinaryLogic;
use function PHPStan\Testing\assertType;
use function PHPStan\Testing\assertVariableCertainty;

function foo(bool $condition): ?string
{
if ($condition) {
$foo = 'bar';
}
if ($foo ?? null) {
assertVariableCertainty(TrinaryLogic::createYes(), $foo);
return $foo;
}
return null;
}

function bar(bool $condition, $a): ?string
{
if ($condition) {
$foo = $a;
}
if ($foo ?? null) {
assertVariableCertainty(TrinaryLogic::createYes(), $foo);
assertType("mixed~null", $foo);
return $foo;
}
return null;
}

0 comments on commit c2ba341

Please sign in to comment.