You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -139,7 +139,7 @@ parameters:
139
139
## Rules:
140
140
141
141
### allowComparingOnlyComparableTypes
142
-
- Denies using comparison operators `>,<,<=,>=,<=>` over anything other than `int|string|float|DateTimeInterface`. Null is not allowed.
142
+
- Denies using comparison operators `>,<,<=,>=,<=>` over anything other than `int|string|float|DateTimeInterface` or same size tuples containing comparable types. Null is not allowed.
143
143
- Mixing different types in those operators is also forbidden, only exception is comparing floats with integers
144
144
- Mainly targets to accidental comparisons of objects, enums or arrays which is valid in PHP, but very tricky
if (!$this->isComparable($leftType) || !$this->isComparable($rightType)) {
59
-
$error = RuleErrorBuilder::message("Comparison {$leftTypeDescribed}{$node->getOperatorSigil()}{$rightTypeDescribed} contains non-comparable type, only int|float|string|DateTimeInterface is allowed.")
60
+
$error = RuleErrorBuilder::message("Comparison {$leftTypeDescribed}{$node->getOperatorSigil()}{$rightTypeDescribed} contains non-comparable type, only int|float|string|DateTimeInterface or comparable tuple is allowed.")
Copy file name to clipboardExpand all lines: tests/Rule/data/AllowComparingOnlyComparableTypesRule/code.php
+24-8Lines changed: 24 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -25,23 +25,39 @@ interface Bar {}
25
25
int|float$intOrFloat,
26
26
float$float,
27
27
bool$bool,
28
+
mixed$mixed,
28
29
) {
29
-
$foos > $foo; // error: Comparison array > AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
30
-
$nullableInt > $int; // error: Comparison int|null > int contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
31
-
null > $int; // error: Comparison null > int contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
32
-
$foo > $foo2; // error: Comparison AllowComparingOnlyComparableTypesRule\Foo > AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
33
-
$foo > $fooOrBar; // error: Comparison AllowComparingOnlyComparableTypesRule\Foo > AllowComparingOnlyComparableTypesRule\Bar|AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
34
-
$foo > $fooAndBar; // error: Comparison AllowComparingOnlyComparableTypesRule\Foo > AllowComparingOnlyComparableTypesRule\Bar&AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
30
+
$foos > $foo; // error: Comparison array > AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface or comparable tuple is allowed.
31
+
$nullableInt > $int; // error: Comparison int|null > int contains non-comparable type, only int|float|string|DateTimeInterface or comparable tuple is allowed.
32
+
null > $int; // error: Comparison null > int contains non-comparable type, only int|float|string|DateTimeInterface or comparable tuple is allowed.
33
+
$foo > $foo2; // error: Comparison AllowComparingOnlyComparableTypesRule\Foo > AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface or comparable tuple is allowed.
34
+
$foo > $fooOrBar; // error: Comparison AllowComparingOnlyComparableTypesRule\Foo > AllowComparingOnlyComparableTypesRule\Bar|AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface or comparable tuple is allowed.
35
+
$foo > $fooAndBar; // error: Comparison AllowComparingOnlyComparableTypesRule\Foo > AllowComparingOnlyComparableTypesRule\Bar&AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface or comparable tuple is allowed.
35
36
$string > 'foo';
36
37
$int > 2;
37
38
$float > 2;
38
39
$int > $intOrFloat;
39
40
$string > $intOrFloat; // error: Cannot compare different types in string > float|int.
40
-
$bool > true; // error: Comparison bool > true contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
41
+
$bool > true; // error: Comparison bool > true contains non-comparable type, only int|float|string|DateTimeInterface or comparable tuple is allowed.
41
42
$dateTime > $dateTimeImmutable;
42
-
$dateTime > $foo; // error: Comparison DateTime > AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface is allowed.
43
+
$dateTime > $foo; // error: Comparison DateTime > AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface or comparable tuple is allowed.
43
44
44
45
$string > $int; // error: Cannot compare different types in string > int.
45
46
$float > $int;
46
47
$dateTime > $string; // error: Cannot compare different types in DateTime > string.
0 commit comments