Skip to content

Commit

Permalink
OperatorRuleHelper - allow benevolent union types that contain numeri…
Browse files Browse the repository at this point in the history
…c types
  • Loading branch information
ondrejmirtes committed Jul 6, 2018
1 parent 1ec1d04 commit 18c0b6e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Rules/Operators/OperatorRuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ function (Type $type) use ($acceptedType): bool {
return true;
}

return $acceptedType->isSuperTypeOf($type)->yes();
$isSuperType = $acceptedType->isSuperTypeOf($type);
if ($type instanceof \PHPStan\Type\BenevolentUnionType) {
return !$isSuperType->no();
}

return $isSuperType->yes();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function testRule(): void
'Only numeric types are allowed in +, string given on the right side.',
29,
],
[
'Only numeric types are allowed in +, (array<int, string>|false) given on the left side.',
110,
],
]);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Rules/Operators/data/operators.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,11 @@ function ($mixed, int $a, string $b) {
$mixed + $b;
$b + $mixed;
};

function (array $array, int $int, $mixed) {
foreach ($array as $i => $val) {
$i + $int;
}

explode($mixed, $mixed) + $int;
};

0 comments on commit 18c0b6e

Please sign in to comment.