diff --git a/src/Type/UnionType.php b/src/Type/UnionType.php index b3a95c4231..77740ed053 100644 --- a/src/Type/UnionType.php +++ b/src/Type/UnionType.php @@ -649,12 +649,7 @@ public function traverse(callable $cb): Type public function tryRemove(Type $typeToRemove): ?Type { - $innerTypes = []; - foreach ($this->getTypes() as $innerType) { - $innerTypes[] = TypeCombinator::remove($innerType, $typeToRemove); - } - - return TypeCombinator::union(...$innerTypes); + return $this->unionTypes(static fn (Type $type): Type => TypeCombinator::remove($type, $typeToRemove)); } /** diff --git a/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php b/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php index a39294e995..ee5d2dcc42 100644 --- a/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php @@ -159,11 +159,11 @@ public function testStrictComparison(): void 426, ], [ - 'Strict comparison using === between int|int<2, max>|string and 1.0 will always evaluate to false.', + 'Strict comparison using === between (int|int<2, max>|string) and 1.0 will always evaluate to false.', 464, ], [ - 'Strict comparison using === between int|int<2, max>|string and stdClass will always evaluate to false.', + 'Strict comparison using === between (int|int<2, max>|string) and stdClass will always evaluate to false.', 466, ], [ @@ -333,11 +333,11 @@ public function testStrictComparisonWithoutAlwaysTrue(): void 408, ], [ - 'Strict comparison using === between int|int<2, max>|string and 1.0 will always evaluate to false.', + 'Strict comparison using === between (int|int<2, max>|string) and 1.0 will always evaluate to false.', 464, ], [ - 'Strict comparison using === between int|int<2, max>|string and stdClass will always evaluate to false.', + 'Strict comparison using === between (int|int<2, max>|string) and stdClass will always evaluate to false.', 466, ], [ diff --git a/tests/PHPStan/Type/TypeCombinatorTest.php b/tests/PHPStan/Type/TypeCombinatorTest.php index c2f05851c9..297c5b32fa 100644 --- a/tests/PHPStan/Type/TypeCombinatorTest.php +++ b/tests/PHPStan/Type/TypeCombinatorTest.php @@ -3669,13 +3669,13 @@ public function dataRemove(): array new BenevolentUnionType([new IntegerType(), new StringType()]), new ConstantStringType('foo'), UnionType::class, - 'int|string', + '(int|string)', ], [ new BenevolentUnionType([new IntegerType(), new StringType()]), new ConstantIntegerType(1), UnionType::class, - 'int|int<2, max>|string', + '(int|int<2, max>|string)', ], [ new BenevolentUnionType([new IntegerType(), new StringType()]),