Skip to content

Commit

Permalink
test @throws tag
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Sep 10, 2024
1 parent 227db6c commit 1b2037c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Rules/Operators/InvalidBinaryOperationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public function processNode(Node $node, Scope $scope): array
|| $node instanceof Node\Expr\BinaryOp\Mod
)
&& !$this->isDivisionByZeroCaught($node)
&& !$this->hasDivisionByZeroThrowsTag($scope)
) {
$zeroType = new ConstantIntegerType(0);
if ($zeroType->isSuperTypeOf($rightType)->maybe()) {
Expand Down Expand Up @@ -169,4 +170,19 @@ private function isDivisionByZeroCaught(Node $node): bool
return $tryCatchType->isSuperTypeOf(new ObjectType(DivisionByZeroError::class))->yes();
}

private function hasDivisionByZeroThrowsTag(Scope $scope): bool
{
$function = $scope->getFunction();
if ($function === null) {
return false;
}

$throwsType = $function->getThrowType();
if ($throwsType === null) {
return false;
}

return $throwsType->isSuperTypeOf(new ObjectType(DivisionByZeroError::class))->yes();
}

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Operators/data/invalid-division.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ function (int $i, int $x): void {
}
};

/**
* @throws \DivisionByZeroError
*/
function throws(int $i, int $x) {
return $i / $x;
};

0 comments on commit 1b2037c

Please sign in to comment.