diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 4b4096c92c..7a4d38545e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1710,7 +1710,7 @@ parameters: - rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.' identifier: phpstanApi.instanceofType - count: 5 + count: 3 path: src/Type/TypeCombinator.php - diff --git a/src/Type/TypeCombinator.php b/src/Type/TypeCombinator.php index 0d6ef1cb4c..c28e26dc5a 100644 --- a/src/Type/TypeCombinator.php +++ b/src/Type/TypeCombinator.php @@ -494,50 +494,56 @@ private static function compareTypesInUnion(Type $a, Type $b): ?array if ( $a instanceof ConstantStringType - && $a->getValue() === '' - && ($b->describe(VerbosityLevel::value()) === 'non-empty-string' - || $b->describe(VerbosityLevel::value()) === 'non-falsy-string') ) { - return [null, self::intersect( - new StringType(), - ...self::getAccessoryCaseStringTypes($b), - )]; - } - - if ( - $b instanceof ConstantStringType - && $b->getValue() === '' - && ($a->describe(VerbosityLevel::value()) === 'non-empty-string' - || $a->describe(VerbosityLevel::value()) === 'non-falsy-string') - ) { - return [self::intersect( - new StringType(), - ...self::getAccessoryCaseStringTypes($a), - ), null]; - } - - if ( - $a instanceof ConstantStringType - && $a->getValue() === '0' - && $b->describe(VerbosityLevel::value()) === 'non-falsy-string' - ) { - return [null, self::intersect( - new StringType(), - new AccessoryNonEmptyStringType(), - ...self::getAccessoryCaseStringTypes($b), - )]; + $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, self::intersect( + new StringType(), + new AccessoryNonEmptyStringType(), + ...self::getAccessoryCaseStringTypes($b), + )]; + } } if ( $b instanceof ConstantStringType - && $b->getValue() === '0' - && $a->describe(VerbosityLevel::value()) === 'non-falsy-string' ) { - return [self::intersect( - new StringType(), - new AccessoryNonEmptyStringType(), - ...self::getAccessoryCaseStringTypes($a), - ), null]; + $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 [self::intersect( + new StringType(), + new AccessoryNonEmptyStringType(), + ...self::getAccessoryCaseStringTypes($a), + ), null]; + } } return null;