Skip to content

fix: average coverage logic update #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
.phpunit.cache
vendor
.DS_Store
coverage
21 changes: 21 additions & 0 deletions badges/output.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions badges/php.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 23 additions & 9 deletions src/BadgeComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use SimpleXMLElement;
use Throwable;
use function array_sum;
use function count;
use function explode;
use function file_exists;
Expand All @@ -29,9 +30,16 @@ class BadgeComposer
private string $outputFile;
private string $coverageName;
private string $badgeTemplate = __DIR__ . '/../template/badge.svg';
private int $totalCoverage = 0;
private int $totalElements = 0;
private int $checkedElements = 0;
/**
* @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 All @@ -52,7 +60,7 @@ public function __construct(string $inputFiles, string $outputFile, string $cove
*/
public function getTotalCoverage(): int
{
return $this->totalCoverage;
return array_sum($this->totalCoverage);
}

/**
Expand Down Expand Up @@ -112,12 +120,18 @@ private function processFile(string $inputFile): void
$xml = new SimpleXMLElement($content);
$metrics = $xml->xpath('//metrics');
foreach ($metrics as $metric) {
$this->totalElements += (int) $metric['elements'];
$this->checkedElements += (int) $metric['coveredelements'];
$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'];
}

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

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

if($template === false) {
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(83, $this->badgeComposer->getTotalCoverage());
$this->assertEquals(69, $this->badgeComposer->getTotalCoverage());
}

/**
Expand Down