Skip to content

Commit 58e73b3

Browse files
authored
Merge pull request #20 from codebtech/fix/coverage-avg
fix: average coverage logic update
2 parents c87e91c + 8ba268f commit 58e73b3

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea
2+
.phpunit.cache
3+
vendor
4+
.DS_Store
5+
coverage

badges/output.svg

Lines changed: 21 additions & 0 deletions
Loading

badges/php.svg

Lines changed: 2 additions & 2 deletions
Loading

src/BadgeComposer.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use SimpleXMLElement;
77
use Throwable;
8+
use function array_sum;
89
use function count;
910
use function explode;
1011
use function file_exists;
@@ -29,9 +30,16 @@ class BadgeComposer
2930
private string $outputFile;
3031
private string $coverageName;
3132
private string $badgeTemplate = __DIR__ . '/../template/badge.svg';
32-
private int $totalCoverage = 0;
33-
private int $totalElements = 0;
34-
private int $checkedElements = 0;
33+
/**
34+
* @var int[]
35+
*/
36+
private array $totalCoverage = [];
37+
private int $totalConditionals = 0;
38+
private int $coveredConditionals = 0;
39+
private int $totalStatements = 0;
40+
private int $coveredStatements = 0;
41+
private int $totalMethods = 0;
42+
private int $coveredMethods = 0;
3543

3644
/**
3745
* @throws Exception
@@ -52,7 +60,7 @@ public function __construct(string $inputFiles, string $outputFile, string $cove
5260
*/
5361
public function getTotalCoverage(): int
5462
{
55-
return $this->totalCoverage;
63+
return array_sum($this->totalCoverage);
5664
}
5765

5866
/**
@@ -112,12 +120,18 @@ private function processFile(string $inputFile): void
112120
$xml = new SimpleXMLElement($content);
113121
$metrics = $xml->xpath('//metrics');
114122
foreach ($metrics as $metric) {
115-
$this->totalElements += (int) $metric['elements'];
116-
$this->checkedElements += (int) $metric['coveredelements'];
123+
$this->totalConditionals += (int) $metric['conditionals'];
124+
$this->coveredConditionals += (int) $metric['coveredconditionals'];
125+
$this->totalStatements += (int) $metric['statements'];
126+
$this->coveredStatements += (int) $metric['coveredstatements'];
127+
$this->totalMethods += (int) $metric['methods'];
128+
$this->coveredMethods += (int) $metric['coveredmethods'];
117129
}
118130

119-
$coverageRatio = $this->totalElements ? $this->checkedElements / $this->totalElements : 0;
120-
$this->totalCoverage += (int) round($coverageRatio * 100);
131+
$totalElements = $this->totalConditionals + $this->totalStatements + $this->totalMethods;
132+
$coveredElements = $this->coveredConditionals + $this->coveredStatements + $this->coveredMethods;
133+
$coverageRatio = $totalElements ? $coveredElements / $totalElements : 0;
134+
$this->totalCoverage[] = (int) round($coverageRatio * 100);
121135

122136
} catch (Throwable $e) {
123137
throw new Exception($e->getMessage());
@@ -131,7 +145,7 @@ private function processFile(string $inputFile): void
131145
*/
132146
private function finalizeCoverage(): void
133147
{
134-
$totalCoverage = $this->totalCoverage / count($this->inputFiles); // Average coverage across all files
148+
$totalCoverage = $this->getTotalCoverage() / count($this->inputFiles); // Average coverage across all files
135149
$template = file_get_contents($this->badgeTemplate);
136150

137151
if($template === false) {

tests/BadgeComposerTest.php

Lines changed: 1 addition & 1 deletion
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)