Skip to content

Commit

Permalink
fix: use local vars
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanrajpac committed Jun 26, 2024
1 parent 8ba268f commit 07c8ee2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
34 changes: 19 additions & 15 deletions src/BadgeComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use function file_put_contents;
use function round;
use function str_replace;
use function var_dump;

/**
* Class BadgeComposer
Expand All @@ -34,12 +35,6 @@ class BadgeComposer
* @var int[]
*/
private array $totalCoverage = [];
private int $totalConditionals = 0;
private int $coveredConditionals = 0;
private int $totalStatements = 0;
private int $coveredStatements = 0;
private int $totalMethods = 0;
private int $coveredMethods = 0;

/**
* @throws Exception
Expand Down Expand Up @@ -119,17 +114,25 @@ private function processFile(string $inputFile): void
}
$xml = new SimpleXMLElement($content);
$metrics = $xml->xpath('//metrics');

$totalConditionals = 0;
$totalStatements = 0;
$totalMethods = 0;
$coveredStatements = 0;
$coveredConditionals = 0;
$coveredMethods = 0;

foreach ($metrics as $metric) {
$this->totalConditionals += (int) $metric['conditionals'];
$this->coveredConditionals += (int) $metric['coveredconditionals'];
$this->totalStatements += (int) $metric['statements'];
$this->coveredStatements += (int) $metric['coveredstatements'];
$this->totalMethods += (int) $metric['methods'];
$this->coveredMethods += (int) $metric['coveredmethods'];
$totalConditionals = (int) $metric['conditionals'];
$coveredConditionals = (int) $metric['coveredconditionals'];
$totalStatements = (int) $metric['statements'];
$coveredStatements = (int) $metric['coveredstatements'];
$totalMethods = (int) $metric['methods'];
$coveredMethods = (int) $metric['coveredmethods'];
}

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

Expand All @@ -145,14 +148,15 @@ private function processFile(string $inputFile): void
*/
private function finalizeCoverage(): void
{
var_dump($this->totalCoverage);
$totalCoverage = $this->getTotalCoverage() / count($this->inputFiles); // Average coverage across all files
$template = file_get_contents($this->badgeTemplate);

if($template === false) {
throw new Exception('Error reading badge template file');
}

$template = str_replace('{{ total }}', (string) $totalCoverage, $template);
$template = str_replace('{{ total }}', (string) round($totalCoverage), $template);

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

Expand Down
2 changes: 1 addition & 1 deletion tests/BadgeComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function testProcessMultipleCloverFilesAndCalculateTheCoverage(): void
$this->processFile($this->inputFile);
$this->processFile($this->inputFile2);

$this->assertEquals(69, $this->badgeComposer->getTotalCoverage());
$this->assertEquals(43, $this->badgeComposer->getTotalCoverage());
}

/**
Expand Down

0 comments on commit 07c8ee2

Please sign in to comment.