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
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -56,8 +56,8 @@ Few rules are enabled, but do nothing unless configured, those are marked with `
56
56
## Rules:
57
57
58
58
### allowComparingOnlyComparableTypes
59
-
- Denies using comparison operators `>,<,<=,>=,<=>` over anything other than `int|string|float|DateTimeInterface` or same size tuples containing comparable types. Null is not allowed.
60
-
- Mixing different types in those operators is also forbidden, only exception is comparing floats with integers
59
+
- Denies using comparison operators `>,<,<=,>=,<=>` over anything other than `int|string|float|DateTimeInterface|BcMath\Number` or same size tuples containing comparable types. Null is not allowed.
60
+
- Mixing different types in those operators is also forbidden, only exception is comparing floats with integers and integers with `BcMath\Number`
61
61
- 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)) {
63
-
$error = RuleErrorBuilder::message("Comparison {$leftTypeDescribed}{$node->getOperatorSigil()}{$rightTypeDescribed} contains non-comparable type, only int|float|string|DateTimeInterface or comparable tuple is allowed.")
63
+
$error = RuleErrorBuilder::message("Comparison {$leftTypeDescribed}{$node->getOperatorSigil()}{$rightTypeDescribed} contains non-comparable type, only int|float|string|DateTimeInterface|BcMath\Number or comparable tuple is allowed.")
Copy file name to clipboardExpand all lines: tests/Rule/data/AllowComparingOnlyComparableTypesRule/code.php
+24-13Lines changed: 24 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
3
3
namespaceAllowComparingOnlyComparableTypesRule;
4
4
5
+
useBcMath\Number;
5
6
useDateTime;
6
7
useDateTimeImmutable;
7
8
@@ -19,6 +20,7 @@ interface Bar {}
19
20
Foo&Bar$fooAndBar,
20
21
DateTime$dateTime,
21
22
DateTimeImmutable$dateTimeImmutable,
23
+
Number$number,
22
24
string$string,
23
25
int$int,
24
26
?int$nullableInt,
@@ -27,37 +29,46 @@ interface Bar {}
27
29
bool$bool,
28
30
$mixed,
29
31
) {
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.
32
+
$foos > $foo; // error: Comparison array > AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface|BcMath\Number or comparable tuple is allowed.
33
+
$nullableInt > $int; // error: Comparison int|null > int contains non-comparable type, only int|float|string|DateTimeInterface|BcMath\Number or comparable tuple is allowed.
34
+
null > $int; // error: Comparison null > int contains non-comparable type, only int|float|string|DateTimeInterface|BcMath\Number or comparable tuple is allowed.
35
+
$foo > $foo2; // error: Comparison AllowComparingOnlyComparableTypesRule\Foo > AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface|BcMath\Number or comparable tuple is allowed.
36
+
$foo > $fooOrBar; // error: Comparison AllowComparingOnlyComparableTypesRule\Foo > AllowComparingOnlyComparableTypesRule\Bar|AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface|BcMath\Number or comparable tuple is allowed.
37
+
$foo > $fooAndBar; // error: Comparison AllowComparingOnlyComparableTypesRule\Foo > AllowComparingOnlyComparableTypesRule\Bar&AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface|BcMath\Number or comparable tuple is allowed.
36
38
$string > 'foo';
37
39
$int > 2;
38
40
$float > 2;
39
41
$int > $intOrFloat;
40
42
$string > $intOrFloat; // error: Cannot compare different types in string > float|int.
41
-
$bool > true; // error: Comparison bool > true contains non-comparable type, only int|float|string|DateTimeInterface or comparable tuple is allowed.
43
+
$bool > true; // error: Comparison bool > true contains non-comparable type, only int|float|string|DateTimeInterface|BcMath\Number or comparable tuple is allowed.
42
44
$dateTime > $dateTimeImmutable;
43
-
$dateTime > $foo; // error: Comparison DateTime > AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface or comparable tuple is allowed.
45
+
$dateTime > $foo; // error: Comparison DateTime > AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface|BcMath\Number or comparable tuple is allowed.
44
46
45
47
$string > $int; // error: Cannot compare different types in string > int.
46
48
$float > $int;
47
49
$dateTime > $string; // error: Cannot compare different types in DateTime > string.
50
+
$number > $int;
51
+
$number > $float; // error: Cannot compare different types in BcMath\Number > float.
52
+
$number > $string; // error: Cannot compare different types in BcMath\Number > string.
53
+
$number > $intOrFloat; // error: Cannot compare different types in BcMath\Number > float|int.
54
+
$number > $foo; // error: Comparison BcMath\Number > AllowComparingOnlyComparableTypesRule\Foo contains non-comparable type, only int|float|string|DateTimeInterface|BcMath\Number or comparable tuple is allowed.
55
+
$number > $nullableInt; // error: Comparison BcMath\Number > int|null contains non-comparable type, only int|float|string|DateTimeInterface|BcMath\Number or comparable tuple is allowed.
0 commit comments