@@ -9,6 +9,17 @@ final class LooseComparisonHelper
9
9
{
10
10
11
11
public static function compareConstantScalars (ConstantScalarType $ leftType , ConstantScalarType $ rightType , PhpVersion $ phpVersion ): BooleanType
12
+ {
13
+ [$ leftValue , $ rightValue ] = self ::getConstantScalarValuesForComparison ($ leftType , $ rightType , $ phpVersion );
14
+
15
+ // @phpstan-ignore equal.notAllowed
16
+ return new ConstantBooleanType ($ leftValue == $ rightValue ); // phpcs:ignore
17
+ }
18
+
19
+ /**
20
+ * @return array{scalar, scalar}
21
+ */
22
+ public static function getConstantScalarValuesForComparison (ConstantScalarType $ leftType , ConstantScalarType $ rightType , PhpVersion $ phpVersion ): array
12
23
{
13
24
if ($ phpVersion ->castsNumbersToStringsOnLooseComparison ()) {
14
25
$ isNumber = new UnionType ([
@@ -17,34 +28,27 @@ public static function compareConstantScalars(ConstantScalarType $leftType, Cons
17
28
]);
18
29
19
30
if ($ leftType ->isString ()->yes () && $ leftType ->isNumericString ()->no () && $ isNumber ->isSuperTypeOf ($ rightType )->yes ()) {
20
- $ stringValue = (string ) $ rightType ->getValue ();
21
- return new ConstantBooleanType ($ stringValue === $ leftType ->getValue ());
31
+ return [$ leftType ->getValue (), (string ) $ rightType ->getValue ()];
22
32
}
23
33
if ($ rightType ->isString ()->yes () && $ rightType ->isNumericString ()->no () && $ isNumber ->isSuperTypeOf ($ leftType )->yes ()) {
24
- $ stringValue = (string ) $ leftType ->getValue ();
25
- return new ConstantBooleanType ($ stringValue === $ rightType ->getValue ());
34
+ return [(string ) $ leftType ->getValue (), $ rightType ->getValue ()];
26
35
}
27
36
} else {
28
37
if ($ leftType ->isString ()->yes () && $ leftType ->isNumericString ()->no () && $ rightType ->isFloat ()->yes ()) {
29
- $ numericPart = (float ) $ leftType ->getValue ();
30
- return new ConstantBooleanType ($ numericPart === $ rightType ->getValue ());
38
+ return [(float ) $ leftType ->getValue (), $ rightType ->getValue ()];
31
39
}
32
40
if ($ rightType ->isString ()->yes () && $ rightType ->isNumericString ()->no () && $ leftType ->isFloat ()->yes ()) {
33
- $ numericPart = (float ) $ rightType ->getValue ();
34
- return new ConstantBooleanType ($ numericPart === $ leftType ->getValue ());
41
+ return [$ leftType ->getValue (), (float ) $ rightType ->getValue ()];
35
42
}
36
43
if ($ leftType ->isString ()->yes () && $ leftType ->isNumericString ()->no () && $ rightType ->isInteger ()->yes ()) {
37
- $ numericPart = (int ) $ leftType ->getValue ();
38
- return new ConstantBooleanType ($ numericPart === $ rightType ->getValue ());
44
+ return [(int ) $ leftType ->getValue (), $ rightType ->getValue ()];
39
45
}
40
46
if ($ rightType ->isString ()->yes () && $ rightType ->isNumericString ()->no () && $ leftType ->isInteger ()->yes ()) {
41
- $ numericPart = (int ) $ rightType ->getValue ();
42
- return new ConstantBooleanType ($ numericPart === $ leftType ->getValue ());
47
+ return [$ leftType ->getValue (), (int ) $ rightType ->getValue ()];
43
48
}
44
49
}
45
50
46
- // @phpstan-ignore equal.notAllowed
47
- return new ConstantBooleanType ($ leftType ->getValue () == $ rightType ->getValue ()); // phpcs:ignore
51
+ return [$ leftType ->getValue (), $ rightType ->getValue ()];
48
52
}
49
53
50
54
}
0 commit comments