Skip to content

Commit 995e2de

Browse files
committed
fix: average coverage logic update
1 parent 9d937b7 commit 995e2de

File tree

5 files changed

+48
-12
lines changed

5 files changed

+48
-12
lines changed

Diff for: .gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea
2+
.phpunit.cache
3+
vendor
4+
.DS_Store
5+
coverage

Diff for: badges/output.svg

+21
Loading

Diff for: badges/php.svg

+2-2
Loading

Diff for: src/BadgeComposer.php

+19-9
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ class BadgeComposer
2929
private string $outputFile;
3030
private string $coverageName;
3131
private string $badgeTemplate = __DIR__ . '/../template/badge.svg';
32-
private int $totalCoverage = 0;
33-
private int $totalElements = 0;
34-
private int $checkedElements = 0;
32+
private array $totalCoverage = [];
33+
private int $totalConditionals = 0;
34+
private int $coveredConditionals = 0;
35+
private int $totalStatements = 0;
36+
private int $coveredStatements = 0;
37+
private int $totalMethods = 0;
38+
private int $coveredMethods = 0;
3539

3640
/**
3741
* @throws Exception
@@ -52,7 +56,7 @@ public function __construct(string $inputFiles, string $outputFile, string $cove
5256
*/
5357
public function getTotalCoverage(): int
5458
{
55-
return $this->totalCoverage;
59+
return array_sum($this->totalCoverage);
5660
}
5761

5862
/**
@@ -112,12 +116,18 @@ private function processFile(string $inputFile): void
112116
$xml = new SimpleXMLElement($content);
113117
$metrics = $xml->xpath('//metrics');
114118
foreach ($metrics as $metric) {
115-
$this->totalElements += (int) $metric['elements'];
116-
$this->checkedElements += (int) $metric['coveredelements'];
119+
$this->totalConditionals += (int) $metric['conditionals'];
120+
$this->coveredConditionals += (int) $metric['coveredconditionals'];
121+
$this->totalStatements += (int) $metric['statements'];
122+
$this->coveredStatements += (int) $metric['coveredstatements'];
123+
$this->totalMethods += (int) $metric['methods'];
124+
$this->coveredMethods += (int) $metric['coveredmethods'];
117125
}
118126

119-
$coverageRatio = $this->totalElements ? $this->checkedElements / $this->totalElements : 0;
120-
$this->totalCoverage += (int) round($coverageRatio * 100);
127+
$totalElements = $this->totalConditionals + $this->totalStatements + $this->totalMethods;
128+
$coveredElements = $this->coveredConditionals + $this->coveredStatements + $this->coveredMethods;
129+
$coverageRatio = $totalElements ? $coveredElements / $totalElements : 0;
130+
$this->totalCoverage[] = (int) round($coverageRatio * 100);
121131

122132
} catch (Throwable $e) {
123133
throw new Exception($e->getMessage());
@@ -131,7 +141,7 @@ private function processFile(string $inputFile): void
131141
*/
132142
private function finalizeCoverage(): void
133143
{
134-
$totalCoverage = $this->totalCoverage / count($this->inputFiles); // Average coverage across all files
144+
$totalCoverage = $this->getTotalCoverage() / count($this->inputFiles); // Average coverage across all files
135145
$template = file_get_contents($this->badgeTemplate);
136146

137147
if($template === false) {

Diff for: tests/BadgeComposerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function testProcessMultipleCloverFilesAndCalculateTheCoverage(): void
9595
$this->processFile($this->inputFile);
9696
$this->processFile($this->inputFile2);
9797

98-
$this->assertEquals(83, $this->badgeComposer->getTotalCoverage());
98+
$this->assertEquals(69, $this->badgeComposer->getTotalCoverage());
9999
}
100100

101101
/**

0 commit comments

Comments
 (0)