Skip to content

Commit

Permalink
Add TypeCombinator::removeFalsey()
Browse files Browse the repository at this point in the history
  • Loading branch information
axlon authored and ondrejmirtes committed Dec 16, 2022
1 parent bc4b2fe commit c9a546f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/Analyser/MutatingScope.php
Expand Up @@ -99,7 +99,6 @@
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\ParserNodeTypeToPHPStanType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\StringType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -1681,7 +1680,7 @@ private function resolveType(string $exprString, Expr $node): Type
return $this->filterByFalseyValue($node->cond)->getType($node->else);
}
return TypeCombinator::union(
TypeCombinator::remove($this->filterByTruthyValue($node->cond)->getType($node->cond), StaticTypeFactory::falsey()),
TypeCombinator::removeFalsey($this->filterByTruthyValue($node->cond)->getType($node->cond)),
$this->filterByFalseyValue($node->cond)->getType($node->else),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Expand Up @@ -3449,7 +3449,7 @@ private function processAssignVar(

$conditionalExpressions = [];

$truthyType = TypeCombinator::remove($type, StaticTypeFactory::falsey());
$truthyType = TypeCombinator::removeFalsey($type);
$falseyType = TypeCombinator::intersect($type, StaticTypeFactory::falsey());

$conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyType);
Expand Down
5 changes: 2 additions & 3 deletions src/Reflection/InitializerExprTypeResolver.php
Expand Up @@ -52,7 +52,6 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\StringType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -165,15 +164,15 @@ public function getType(Expr $expr, InitializerExprContext $context): Type
$elseType = $this->getType($expr->else, $context);
if ($expr->if === null) {
return TypeCombinator::union(
TypeCombinator::remove($condType, StaticTypeFactory::falsey()),
TypeCombinator::removeFalsey($condType),
$elseType,
);
}

$ifType = $this->getType($expr->if, $context);

return TypeCombinator::union(
TypeCombinator::remove($ifType, StaticTypeFactory::falsey()),
TypeCombinator::removeFalsey($ifType),
$elseType,
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Type/TypeCombinator.php
Expand Up @@ -1072,4 +1072,9 @@ public static function intersect(Type ...$types): Type
return new IntersectionType($types);
}

public static function removeFalsey(Type $type): Type
{
return self::remove($type, StaticTypeFactory::falsey());
}

}

0 comments on commit c9a546f

Please sign in to comment.