Skip to content

Commit 9e2ac7a

Browse files
authored
Merge pull request #21 from codebtech/fix/coverage-avg
Use local vars instead of class props
2 parents 58e73b3 + 74cd8db commit 9e2ac7a

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

src/BadgeComposer.php

+17-15
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ class BadgeComposer
3434
* @var int[]
3535
*/
3636
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;
4337

4438
/**
4539
* @throws Exception
@@ -119,17 +113,25 @@ private function processFile(string $inputFile): void
119113
}
120114
$xml = new SimpleXMLElement($content);
121115
$metrics = $xml->xpath('//metrics');
116+
117+
$totalConditionals = 0;
118+
$totalStatements = 0;
119+
$totalMethods = 0;
120+
$coveredStatements = 0;
121+
$coveredConditionals = 0;
122+
$coveredMethods = 0;
123+
122124
foreach ($metrics as $metric) {
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'];
125+
$totalConditionals = (int) $metric['conditionals'];
126+
$coveredConditionals = (int) $metric['coveredconditionals'];
127+
$totalStatements = (int) $metric['statements'];
128+
$coveredStatements = (int) $metric['coveredstatements'];
129+
$totalMethods = (int) $metric['methods'];
130+
$coveredMethods = (int) $metric['coveredmethods'];
129131
}
130132

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

@@ -152,7 +154,7 @@ private function finalizeCoverage(): void
152154
throw new Exception('Error reading badge template file');
153155
}
154156

155-
$template = str_replace('{{ total }}', (string) $totalCoverage, $template);
157+
$template = str_replace('{{ total }}', (string) round($totalCoverage), $template);
156158

157159
$template = str_replace('{{ coverage }}', $this->coverageName, $template);
158160

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(69, $this->badgeComposer->getTotalCoverage());
98+
$this->assertEquals(43, $this->badgeComposer->getTotalCoverage());
9999
}
100100

101101
/**

0 commit comments

Comments
 (0)