Skip to content

Commit 18c0b6e

Browse files
committed
OperatorRuleHelper - allow benevolent union types that contain numeric types
1 parent 1ec1d04 commit 18c0b6e

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/Rules/Operators/OperatorRuleHelper.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ function (Type $type) use ($acceptedType): bool {
6565
return true;
6666
}
6767

68-
return $acceptedType->isSuperTypeOf($type)->yes();
68+
$isSuperType = $acceptedType->isSuperTypeOf($type);
69+
if ($type instanceof \PHPStan\Type\BenevolentUnionType) {
70+
return !$isSuperType->no();
71+
}
72+
73+
return $isSuperType->yes();
6974
}
7075

7176
}

tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public function testRule(): void
3636
'Only numeric types are allowed in +, string given on the right side.',
3737
29,
3838
],
39+
[
40+
'Only numeric types are allowed in +, (array<int, string>|false) given on the left side.',
41+
110,
42+
],
3943
]);
4044
}
4145

tests/Rules/Operators/data/operators.php

+8
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,11 @@ function ($mixed, int $a, string $b) {
101101
$mixed + $b;
102102
$b + $mixed;
103103
};
104+
105+
function (array $array, int $int, $mixed) {
106+
foreach ($array as $i => $val) {
107+
$i + $int;
108+
}
109+
110+
explode($mixed, $mixed) + $int;
111+
};

0 commit comments

Comments
 (0)