diff --git a/src/Type/TypeCombinator.php b/src/Type/TypeCombinator.php index 69351e75b3..be48ebe6ff 100644 --- a/src/Type/TypeCombinator.php +++ b/src/Type/TypeCombinator.php @@ -33,6 +33,7 @@ use function array_values; use function count; use function get_class; +use function in_array; use function is_int; use function sprintf; use function usort; @@ -494,54 +495,50 @@ private static function compareTypesInUnion(Type $a, Type $b): ?array if ( $a instanceof ConstantStringType ) { - $description = $b->describe(VerbosityLevel::value()); - if ( - $a->getValue() === '' - && ($description === 'non-empty-string' - || $description === 'non-falsy-string') - ) { - return [null, self::intersect( - new StringType(), - ...self::getAccessoryCaseStringTypes($b), - )]; - } - - if ( - $a->getValue() === '0' - && $description === 'non-falsy-string' - ) { - return [null, new IntersectionType([ - new StringType(), - new AccessoryNonEmptyStringType(), - ...self::getAccessoryCaseStringTypes($b), - ])]; + if ($a->getValue() === '') { + $description = $b->describe(VerbosityLevel::value()); + if (in_array($description, ['non-empty-string', 'non-falsy-string'], true)) { + return [null, self::intersect( + new StringType(), + ...self::getAccessoryCaseStringTypes($b), + )]; + } + } + + if ($a->getValue() === '0') { + $description = $b->describe(VerbosityLevel::value()); + if ($description === 'non-falsy-string') { + return [null, new IntersectionType([ + new StringType(), + new AccessoryNonEmptyStringType(), + ...self::getAccessoryCaseStringTypes($b), + ])]; + } } } if ( $b instanceof ConstantStringType ) { - $description = $a->describe(VerbosityLevel::value()); - if ( - $b->getValue() === '' - && ($description === 'non-empty-string' - || $description === 'non-falsy-string') - ) { - return [self::intersect( - new StringType(), - ...self::getAccessoryCaseStringTypes($a), - ), null]; - } - - if ( - $b->getValue() === '0' - && $description === 'non-falsy-string' - ) { - return [new IntersectionType([ - new StringType(), - new AccessoryNonEmptyStringType(), - ...self::getAccessoryCaseStringTypes($a), - ]), null]; + if ($b->getValue() === '') { + $description = $a->describe(VerbosityLevel::value()); + if (in_array($description, ['non-empty-string', 'non-falsy-string'], true)) { + return [self::intersect( + new StringType(), + ...self::getAccessoryCaseStringTypes($a), + ), null]; + } + } + + if ($b->getValue() === '0') { + $description = $a->describe(VerbosityLevel::value()); + if ($description === 'non-falsy-string') { + return [new IntersectionType([ + new StringType(), + new AccessoryNonEmptyStringType(), + ...self::getAccessoryCaseStringTypes($a), + ]), null]; + } } }