Skip to content

Commit

Permalink
isset() - truthy vs. true
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 22, 2020
1 parent 3b21093 commit fcb78d9
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,9 @@ public function specifyTypesInCondition(
(
$expr instanceof Expr\Isset_
&& count($expr->vars) > 0
&& $context->truthy()
&& $context->true()
)
|| ($expr instanceof Expr\Empty_ && $context->falsey())
|| ($expr instanceof Expr\Empty_ && $context->false())
) {
$vars = [];
if ($expr instanceof Expr\Isset_) {
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10209,6 +10209,16 @@ public function dataCount(): array
return $this->gatherAssertTypes(__DIR__ . '/data/count-type.php');
}

public function dataBug2816(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-2816.php');
}

public function dataBug2816Two(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-2816-2.php');
}

public function dataBug3985(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3985.php');
Expand Down Expand Up @@ -10298,6 +10308,8 @@ public function dataBug3985(): array
* @dataProvider dataBug1924
* @dataProvider dataExtraIntTypes
* @dataProvider dataCount
* @dataProvider dataBug2816
* @dataProvider dataBug2816Two
* @dataProvider dataBug3985
* @param string $assertType
* @param string $file
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/data/bug-2816-2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Bug2816;

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

if (isset($_GET['x'])) {
$a = 1;
}

assertVariableCertainty(TrinaryLogic::createMaybe(), $a);
assertType('mixed', $a);

if (isset($a)) {
echo "hello";
assertVariableCertainty(TrinaryLogic::createYes(), $a);
assertType('mixed~null', $a);
} else {
assertVariableCertainty(TrinaryLogic::createMaybe(), $a);
}

assertVariableCertainty(TrinaryLogic::createMaybe(), $a);
assertType('mixed', $a);

if (isset($a)) {
echo "hello2";
assertVariableCertainty(TrinaryLogic::createYes(), $a);
assertType('mixed~null', $a);
} else {
assertVariableCertainty(TrinaryLogic::createMaybe(), $a);
}

assertVariableCertainty(TrinaryLogic::createMaybe(), $a);
assertType('mixed', $a);
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/data/bug-2816.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Bug2816;

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

if (isset($_GET['x'])) {
$a = 1;
}

assertVariableCertainty(TrinaryLogic::createMaybe(), $a);
assertType('mixed', $a);

if (isset($a) === true) {
echo "hello";
assertVariableCertainty(TrinaryLogic::createYes(), $a);
assertType('mixed~null', $a);
} else {
assertVariableCertainty(TrinaryLogic::createMaybe(), $a);
}

assertVariableCertainty(TrinaryLogic::createMaybe(), $a);
assertType('mixed', $a);

if (isset($a) === true) {
echo "hello2";
assertVariableCertainty(TrinaryLogic::createYes(), $a);
assertType('mixed~null', $a);
} else {
assertVariableCertainty(TrinaryLogic::createMaybe(), $a);
}

assertVariableCertainty(TrinaryLogic::createMaybe(), $a);
assertType('mixed', $a);

0 comments on commit fcb78d9

Please sign in to comment.