Skip to content

Refacto LooseComparisonHelper #3485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/Type/LooseComparisonHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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()];
}

}
5 changes: 4 additions & 1 deletion src/Type/NullType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
5 changes: 4 additions & 1 deletion src/Type/Traits/ConstantScalarTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Loading