From 7ad11d1a4e6256ef89d3b73ecd0c52c2f1be8e79 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 26 Sep 2024 01:36:31 +0200 Subject: [PATCH] Refacto LooseComparisonHelper --- src/Type/LooseComparisonHelper.php | 35 ++++++++++++--------- src/Type/NullType.php | 5 ++- src/Type/Traits/ConstantScalarTypeTrait.php | 5 ++- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/Type/LooseComparisonHelper.php b/src/Type/LooseComparisonHelper.php index b4df432c6f..8612faefec 100644 --- a/src/Type/LooseComparisonHelper.php +++ b/src/Type/LooseComparisonHelper.php @@ -8,7 +8,21 @@ final class LooseComparisonHelper { + /** + * @deprecated Use getConstantScalarValuesForComparison instead + */ public static function compareConstantScalars(ConstantScalarType $leftType, ConstantScalarType $rightType, PhpVersion $phpVersion): BooleanType + { + [$leftValue, $rightValue] = self::getConstantScalarValuesForComparison($leftType, $rightType, $phpVersion); + + // @phpstan-ignore equal.notAllowed + return new ConstantBooleanType($leftValue == $rightValue); // phpcs:ignore + } + + /** + * @return array{scalar|null, scalar|null} + */ + public static function getConstantScalarValuesForComparison(ConstantScalarType $leftType, ConstantScalarType $rightType, PhpVersion $phpVersion): array { if ($phpVersion->castsNumbersToStringsOnLooseComparison()) { $isNumber = new UnionType([ @@ -17,34 +31,27 @@ public static function compareConstantScalars(ConstantScalarType $leftType, Cons ]); if ($leftType->isString()->yes() && $leftType->isNumericString()->no() && $isNumber->isSuperTypeOf($rightType)->yes()) { - $stringValue = (string) $rightType->getValue(); - return new ConstantBooleanType($stringValue === $leftType->getValue()); + return [$leftType->getValue(), (string) $rightType->getValue()]; } if ($rightType->isString()->yes() && $rightType->isNumericString()->no() && $isNumber->isSuperTypeOf($leftType)->yes()) { - $stringValue = (string) $leftType->getValue(); - return new ConstantBooleanType($stringValue === $rightType->getValue()); + return [(string) $leftType->getValue(), $rightType->getValue()]; } } else { if ($leftType->isString()->yes() && $leftType->isNumericString()->no() && $rightType->isFloat()->yes()) { - $numericPart = (float) $leftType->getValue(); - return new ConstantBooleanType($numericPart === $rightType->getValue()); + return [(float) $leftType->getValue(), $rightType->getValue()]; } if ($rightType->isString()->yes() && $rightType->isNumericString()->no() && $leftType->isFloat()->yes()) { - $numericPart = (float) $rightType->getValue(); - return new ConstantBooleanType($numericPart === $leftType->getValue()); + return [$leftType->getValue(), (float) $rightType->getValue()]; } if ($leftType->isString()->yes() && $leftType->isNumericString()->no() && $rightType->isInteger()->yes()) { - $numericPart = (int) $leftType->getValue(); - return new ConstantBooleanType($numericPart === $rightType->getValue()); + return [(int) $leftType->getValue(), $rightType->getValue()]; } if ($rightType->isString()->yes() && $rightType->isNumericString()->no() && $leftType->isInteger()->yes()) { - $numericPart = (int) $rightType->getValue(); - return new ConstantBooleanType($numericPart === $leftType->getValue()); + return [$leftType->getValue(), (int) $rightType->getValue()]; } } - // @phpstan-ignore equal.notAllowed - return new ConstantBooleanType($leftType->getValue() == $rightType->getValue()); // phpcs:ignore + return [$leftType->getValue(), $rightType->getValue()]; } } diff --git a/src/Type/NullType.php b/src/Type/NullType.php index ab50c2040e..5cc8a490a7 100644 --- a/src/Type/NullType.php +++ b/src/Type/NullType.php @@ -328,7 +328,10 @@ public function isScalar(): TrinaryLogic public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType { if ($type instanceof ConstantScalarType) { - return LooseComparisonHelper::compareConstantScalars($this, $type, $phpVersion); + [$leftValue, $rightValue] = LooseComparisonHelper::getConstantScalarValuesForComparison($this, $type, $phpVersion); + + // @phpstan-ignore equal.notAllowed + return new ConstantBooleanType($leftValue == $rightValue); // phpcs:ignore } if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) { diff --git a/src/Type/Traits/ConstantScalarTypeTrait.php b/src/Type/Traits/ConstantScalarTypeTrait.php index 527d10b68a..45680f04e2 100644 --- a/src/Type/Traits/ConstantScalarTypeTrait.php +++ b/src/Type/Traits/ConstantScalarTypeTrait.php @@ -58,7 +58,10 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType } if ($type instanceof ConstantScalarType) { - return LooseComparisonHelper::compareConstantScalars($this, $type, $phpVersion); + [$leftValue, $rightValue] = LooseComparisonHelper::getConstantScalarValuesForComparison($this, $type, $phpVersion); + + // @phpstan-ignore equal.alwaysTrue, equal.notAllowed + return new ConstantBooleanType($leftValue == $rightValue); // phpcs:ignore } if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {