Skip to content

Commit

Permalink
more tests, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Nov 3, 2023
1 parent 1cf1a08 commit b3f2af1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
43 changes: 29 additions & 14 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,23 +714,37 @@ public function specifyTypesInCondition(
return new SpecifiedTypes();
}

$exprType = $this->create(
$issetExpr,
new NullType(),
$context->negate(),
false,
$scope,
$rootExpr,
);
if ($isset === true) {
return $exprType;
}

if ($issetExpr instanceof Expr\Variable && is_string($issetExpr->name)) {
$type = $scope->getType($issetExpr);

$nullType = new NullType();
if (!$nullType->isSuperTypeOf($type)->no()) {
$isNullable = !$nullType->isSuperTypeOf($type)->no();

$exprType = $this->create(
$issetExpr,
new NullType(),
$context->negate(),
false,
$scope,
$rootExpr,
);
if ($isset === true) {
if ($isNullable) {
return $exprType;
}

// variable cannot exist in !isset()
return $exprType->unionWith($this->create(
new IssetExpr($issetExpr),
new NullType(),
$context,
false,
$scope,
$rootExpr,
));
}

if ($isNullable) {
// reduces variable certainty to maybe
return $exprType->unionWith($this->create(
new IssetExpr($issetExpr),
new NullType(),
Expand All @@ -741,6 +755,7 @@ public function specifyTypesInCondition(
));
}

// variable cannot exist in !isset()
return $this->create(
new IssetExpr($issetExpr),
new NullType(),
Expand Down
32 changes: 31 additions & 1 deletion tests/PHPStan/Analyser/data/falsey-isset-certainty.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace FalseyIssetCertainty;

use function PHPStan\dumpType;
use function PHPStan\Testing\assertType;
use function PHPStan\Testing\assertVariableCertainty;
use PHPStan\TrinaryLogic;
Expand Down Expand Up @@ -121,6 +120,37 @@ function falseyIssetVariable(): void
assertVariableCertainty(TrinaryLogic::createMaybe(), $a);
}

function nullableVariable(): void
{
$a = 'bar';
if (rand() % 2) {
$a = null;
}

assertVariableCertainty(TrinaryLogic::createYes(), $a);
if (isset($a)) {
assertVariableCertainty(TrinaryLogic::createYes(), $a);
} else {
assertVariableCertainty(TrinaryLogic::createYes(), $a);
}

assertVariableCertainty(TrinaryLogic::createYes(), $a);
}

function nonNullableVariable(): void
{
$a = 'bar';

assertVariableCertainty(TrinaryLogic::createYes(), $a);
if (isset($a)) {
assertVariableCertainty(TrinaryLogic::createYes(), $a);
} else {
assertVariableCertainty(TrinaryLogic::createNo(), $a);
}

assertVariableCertainty(TrinaryLogic::createYes(), $a);
}

function falseyIssetNullableVariable(): void
{
if (rand() % 2) {
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/falsy-isset.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function subtractedMixedIsset(mixed $m): void
if (isset($m)) {
assertType("mixed~null", $m);
} else {
assertType("*NEVER*", $m);
assertType("*ERROR*", $m);
}
}

Expand Down

0 comments on commit b3f2af1

Please sign in to comment.