Skip to content

Commit 7ad11d1

Browse files
Refacto LooseComparisonHelper
1 parent 4168fe5 commit 7ad11d1

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

Diff for: src/Type/LooseComparisonHelper.php

+21-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,21 @@
88
final class LooseComparisonHelper
99
{
1010

11+
/**
12+
* @deprecated Use getConstantScalarValuesForComparison instead
13+
*/
1114
public static function compareConstantScalars(ConstantScalarType $leftType, ConstantScalarType $rightType, PhpVersion $phpVersion): BooleanType
15+
{
16+
[$leftValue, $rightValue] = self::getConstantScalarValuesForComparison($leftType, $rightType, $phpVersion);
17+
18+
// @phpstan-ignore equal.notAllowed
19+
return new ConstantBooleanType($leftValue == $rightValue); // phpcs:ignore
20+
}
21+
22+
/**
23+
* @return array{scalar|null, scalar|null}
24+
*/
25+
public static function getConstantScalarValuesForComparison(ConstantScalarType $leftType, ConstantScalarType $rightType, PhpVersion $phpVersion): array
1226
{
1327
if ($phpVersion->castsNumbersToStringsOnLooseComparison()) {
1428
$isNumber = new UnionType([
@@ -17,34 +31,27 @@ public static function compareConstantScalars(ConstantScalarType $leftType, Cons
1731
]);
1832

1933
if ($leftType->isString()->yes() && $leftType->isNumericString()->no() && $isNumber->isSuperTypeOf($rightType)->yes()) {
20-
$stringValue = (string) $rightType->getValue();
21-
return new ConstantBooleanType($stringValue === $leftType->getValue());
34+
return [$leftType->getValue(), (string) $rightType->getValue()];
2235
}
2336
if ($rightType->isString()->yes() && $rightType->isNumericString()->no() && $isNumber->isSuperTypeOf($leftType)->yes()) {
24-
$stringValue = (string) $leftType->getValue();
25-
return new ConstantBooleanType($stringValue === $rightType->getValue());
37+
return [(string) $leftType->getValue(), $rightType->getValue()];
2638
}
2739
} else {
2840
if ($leftType->isString()->yes() && $leftType->isNumericString()->no() && $rightType->isFloat()->yes()) {
29-
$numericPart = (float) $leftType->getValue();
30-
return new ConstantBooleanType($numericPart === $rightType->getValue());
41+
return [(float) $leftType->getValue(), $rightType->getValue()];
3142
}
3243
if ($rightType->isString()->yes() && $rightType->isNumericString()->no() && $leftType->isFloat()->yes()) {
33-
$numericPart = (float) $rightType->getValue();
34-
return new ConstantBooleanType($numericPart === $leftType->getValue());
44+
return [$leftType->getValue(), (float) $rightType->getValue()];
3545
}
3646
if ($leftType->isString()->yes() && $leftType->isNumericString()->no() && $rightType->isInteger()->yes()) {
37-
$numericPart = (int) $leftType->getValue();
38-
return new ConstantBooleanType($numericPart === $rightType->getValue());
47+
return [(int) $leftType->getValue(), $rightType->getValue()];
3948
}
4049
if ($rightType->isString()->yes() && $rightType->isNumericString()->no() && $leftType->isInteger()->yes()) {
41-
$numericPart = (int) $rightType->getValue();
42-
return new ConstantBooleanType($numericPart === $leftType->getValue());
50+
return [$leftType->getValue(), (int) $rightType->getValue()];
4351
}
4452
}
4553

46-
// @phpstan-ignore equal.notAllowed
47-
return new ConstantBooleanType($leftType->getValue() == $rightType->getValue()); // phpcs:ignore
54+
return [$leftType->getValue(), $rightType->getValue()];
4855
}
4956

5057
}

Diff for: src/Type/NullType.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,10 @@ public function isScalar(): TrinaryLogic
328328
public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
329329
{
330330
if ($type instanceof ConstantScalarType) {
331-
return LooseComparisonHelper::compareConstantScalars($this, $type, $phpVersion);
331+
[$leftValue, $rightValue] = LooseComparisonHelper::getConstantScalarValuesForComparison($this, $type, $phpVersion);
332+
333+
// @phpstan-ignore equal.notAllowed
334+
return new ConstantBooleanType($leftValue == $rightValue); // phpcs:ignore
332335
}
333336

334337
if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {

Diff for: src/Type/Traits/ConstantScalarTypeTrait.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
5858
}
5959

6060
if ($type instanceof ConstantScalarType) {
61-
return LooseComparisonHelper::compareConstantScalars($this, $type, $phpVersion);
61+
[$leftValue, $rightValue] = LooseComparisonHelper::getConstantScalarValuesForComparison($this, $type, $phpVersion);
62+
63+
// @phpstan-ignore equal.alwaysTrue, equal.notAllowed
64+
return new ConstantBooleanType($leftValue == $rightValue); // phpcs:ignore
6265
}
6366

6467
if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {

0 commit comments

Comments
 (0)