From f222ea6e7315ad5b1acbd8ca0777394a8650b9b8 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 3 Aug 2025 14:40:30 +0200 Subject: [PATCH] Improve TypeCombinator::unionWithSubtractedType --- src/Type/TypeCombinator.php | 12 ++++++++++ tests/PHPStan/Analyser/nsrt/subtracted.php | 27 ++++++++++++++++++++++ tests/PHPStan/Type/TypeCombinatorTest.php | 8 +++++++ 3 files changed, 47 insertions(+) create mode 100644 tests/PHPStan/Analyser/nsrt/subtracted.php diff --git a/src/Type/TypeCombinator.php b/src/Type/TypeCombinator.php index d79af61977..30c481c725 100644 --- a/src/Type/TypeCombinator.php +++ b/src/Type/TypeCombinator.php @@ -540,6 +540,18 @@ private static function unionWithSubtractedType( return $type; } + if ($subtractedType instanceof SubtractableType) { + $withoutSubtracted = $subtractedType->getTypeWithoutSubtractedType(); + if ($withoutSubtracted->isSuperTypeOf($type)->yes()) { + $subtractedSubtractedType = $subtractedType->getSubtractedType(); + if ($subtractedSubtractedType === null) { + return new NeverType(); + } + + return self::intersect($type, $subtractedSubtractedType); + } + } + if ($type instanceof SubtractableType) { $subtractedType = $type->getSubtractedType() === null ? $subtractedType diff --git a/tests/PHPStan/Analyser/nsrt/subtracted.php b/tests/PHPStan/Analyser/nsrt/subtracted.php new file mode 100644 index 0000000000..903cf0b485 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/subtracted.php @@ -0,0 +1,27 @@ +