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

Commit b2fdbb9

Browse files
committed
Merge pull request #20 from codacy/safe-division
Fix #18: Prevent division by zero exception
2 parents d505188 + d5f6278 commit b2fdbb9

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/Codacy/Coverage/Parser/CloverParser.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function makeReport()
2525
$projectMetrics = $project->metrics;
2626
$coveredStatements = intval($projectMetrics['coveredstatements']);
2727
$statementsTotal = intval($projectMetrics['statements']);
28-
$reportTotal = round(($coveredStatements / $statementsTotal) * 100);
28+
$reportTotal = round($this->safeDivision($coveredStatements, $statementsTotal) * 100);
2929
$fileReports = $this->makeFileReports($project);
3030
$report = new CoverageReport($reportTotal, $fileReports);
3131
return $report;
@@ -71,7 +71,7 @@ private function makeFileReportsFromFiles(\SimpleXMLElement $node, $fileReports)
7171
if ($countStatement == 0) {
7272
$fileTotal = 0;
7373
} else {
74-
$fileTotal = round(($countCoveredStatements / $countStatement) * 100);
74+
$fileTotal = round($this->safeDivision($countCoveredStatements, $countStatement) * 100);
7575
}
7676
$fileName = $this->getRelativePath($file['name']);
7777
$lineCoverage = $this->getLineCoverage($file);
@@ -135,4 +135,12 @@ private function getRelativePath(\SimpleXMLElement $fileName)
135135

136136
return $str;
137137
}
138+
139+
private function safeDivision($a, $b)
140+
{
141+
if ($b === 0) {
142+
return 0;
143+
}
144+
return $a / $b;
145+
}
138146
}

0 commit comments

Comments
 (0)