Skip to content
This repository was archived by the owner on Feb 19, 2020. It is now read-only.

Fix #18: Prevent division by zero exception #20

Merged
merged 1 commit into from
Jan 7, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Codacy/Coverage/Parser/CloverParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function makeReport()
$projectMetrics = $project->metrics;
$coveredStatements = intval($projectMetrics['coveredstatements']);
$statementsTotal = intval($projectMetrics['statements']);
$reportTotal = round(($coveredStatements / $statementsTotal) * 100);
$reportTotal = round($this->safeDivision($coveredStatements, $statementsTotal) * 100);
$fileReports = $this->makeFileReports($project);
$report = new CoverageReport($reportTotal, $fileReports);
return $report;
Expand Down Expand Up @@ -71,7 +71,7 @@ private function makeFileReportsFromFiles(\SimpleXMLElement $node, $fileReports)
if ($countStatement == 0) {
$fileTotal = 0;
} else {
$fileTotal = round(($countCoveredStatements / $countStatement) * 100);
$fileTotal = round($this->safeDivision($countCoveredStatements, $countStatement) * 100);
}
$fileName = $this->getRelativePath($file['name']);
$lineCoverage = $this->getLineCoverage($file);
Expand Down Expand Up @@ -135,4 +135,12 @@ private function getRelativePath(\SimpleXMLElement $fileName)

return $str;
}

private function safeDivision($a, $b)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
if ($b === 0) {
return 0;
}
return $a / $b;
}
}