Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeCombinator::removeFalsey() #2003

Merged
merged 1 commit into from Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(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 @@ -3417,7 +3417,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());
}

}