diff --git a/src/Type/TypeCombinator.php b/src/Type/TypeCombinator.php index cd45719d5c..d79af61977 100644 --- a/src/Type/TypeCombinator.php +++ b/src/Type/TypeCombinator.php @@ -544,6 +544,11 @@ private static function unionWithSubtractedType( $subtractedType = $type->getSubtractedType() === null ? $subtractedType : self::union($type->getSubtractedType(), $subtractedType); + + $subtractedType = self::intersect( + $type->getTypeWithoutSubtractedType(), + $subtractedType, + ); if ($subtractedType instanceof NeverType) { $subtractedType = null; } diff --git a/tests/PHPStan/Type/TypeCombinatorTest.php b/tests/PHPStan/Type/TypeCombinatorTest.php index 6a8a87d0c4..a989ec244a 100644 --- a/tests/PHPStan/Type/TypeCombinatorTest.php +++ b/tests/PHPStan/Type/TypeCombinatorTest.php @@ -4127,6 +4127,22 @@ public static function dataIntersect(): iterable IntersectionType::class, 'lowercase-string&uppercase-string', ], + [ + [ + new ObjectType(DateTime::class), + new MixedType(subtractedType: new NullType()), + ], + ObjectType::class, + 'DateTime', + ], + [ + [ + new ObjectWithoutClassType(), + new MixedType(subtractedType: new NullType()), + ], + ObjectWithoutClassType::class, + 'object', + ], ]; if (PHP_VERSION_ID < 80100) {