8
8
final class LooseComparisonHelper
9
9
{
10
10
11
+ /**
12
+ * @deprecated Use getConstantScalarValuesForComparison instead
13
+ */
11
14
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
12
26
{
13
27
if ($ phpVersion ->castsNumbersToStringsOnLooseComparison ()) {
14
28
$ isNumber = new UnionType ([
@@ -17,34 +31,27 @@ public static function compareConstantScalars(ConstantScalarType $leftType, Cons
17
31
]);
18
32
19
33
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 ()];
22
35
}
23
36
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 ()];
26
38
}
27
39
} else {
28
40
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 ()];
31
42
}
32
43
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 ()];
35
45
}
36
46
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 ()];
39
48
}
40
49
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 ()];
43
51
}
44
52
}
45
53
46
- // @phpstan-ignore equal.notAllowed
47
- return new ConstantBooleanType ($ leftType ->getValue () == $ rightType ->getValue ()); // phpcs:ignore
54
+ return [$ leftType ->getValue (), $ rightType ->getValue ()];
48
55
}
49
56
50
57
}
0 commit comments