88final 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}
0 commit comments