diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 0dbfabed20..db999bf292 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -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; @@ -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), ); } diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index ae1d26eace..680b306679 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -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); diff --git a/src/Reflection/InitializerExprTypeResolver.php b/src/Reflection/InitializerExprTypeResolver.php index b3d49df16e..3b1c00178a 100644 --- a/src/Reflection/InitializerExprTypeResolver.php +++ b/src/Reflection/InitializerExprTypeResolver.php @@ -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; @@ -165,7 +164,7 @@ 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, ); } @@ -173,7 +172,7 @@ public function getType(Expr $expr, InitializerExprContext $context): Type $ifType = $this->getType($expr->if, $context); return TypeCombinator::union( - TypeCombinator::remove($ifType, StaticTypeFactory::falsey()), + TypeCombinator::removeFalsey($ifType), $elseType, ); } diff --git a/src/Type/TypeCombinator.php b/src/Type/TypeCombinator.php index d76f8a1317..a837d6b97a 100644 --- a/src/Type/TypeCombinator.php +++ b/src/Type/TypeCombinator.php @@ -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()); + } + }