diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..658385a --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.idea +.phpunit.cache +vendor +.DS_Store +coverage \ No newline at end of file diff --git a/badges/output.svg b/badges/output.svg new file mode 100644 index 0000000..908161a --- /dev/null +++ b/badges/output.svg @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + unit + unit + 0% + 0% + + \ No newline at end of file diff --git a/badges/php.svg b/badges/php.svg index b1d20cb..3105b6f 100644 --- a/badges/php.svg +++ b/badges/php.svg @@ -15,7 +15,7 @@ PHP PHP - 73% - 73% + 74% + 74% \ No newline at end of file diff --git a/src/BadgeComposer.php b/src/BadgeComposer.php index 8a89e8b..2a3427c 100644 --- a/src/BadgeComposer.php +++ b/src/BadgeComposer.php @@ -5,6 +5,7 @@ use Exception; use SimpleXMLElement; use Throwable; +use function array_sum; use function count; use function explode; use function file_exists; @@ -29,9 +30,10 @@ 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 = []; /** * @throws Exception @@ -52,7 +54,7 @@ public function __construct(string $inputFiles, string $outputFile, string $cove */ public function getTotalCoverage(): int { - return $this->totalCoverage; + return array_sum($this->totalCoverage); } /** @@ -111,13 +113,27 @@ 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->totalElements += (int) $metric['elements']; - $this->checkedElements += (int) $metric['coveredelements']; + $totalConditionals = (int) $metric['conditionals']; + $coveredConditionals = (int) $metric['coveredconditionals']; + $totalStatements = (int) $metric['statements']; + $coveredStatements = (int) $metric['coveredstatements']; + $totalMethods = (int) $metric['methods']; + $coveredMethods = (int) $metric['coveredmethods']; } - $coverageRatio = $this->totalElements ? $this->checkedElements / $this->totalElements : 0; - $this->totalCoverage += (int) round($coverageRatio * 100); + $totalElements = $totalConditionals + $totalStatements + $totalMethods; + $coveredElements = $coveredConditionals + $coveredStatements + $coveredMethods; + $coverageRatio = $totalElements ? $coveredElements / $totalElements : 0; + $this->totalCoverage[] = (int) round($coverageRatio * 100); } catch (Throwable $e) { throw new Exception($e->getMessage()); @@ -131,14 +147,14 @@ 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) { 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); diff --git a/tests/BadgeComposerTest.php b/tests/BadgeComposerTest.php index ea994e2..19b9392 100644 --- a/tests/BadgeComposerTest.php +++ b/tests/BadgeComposerTest.php @@ -95,7 +95,7 @@ public function testProcessMultipleCloverFilesAndCalculateTheCoverage(): void $this->processFile($this->inputFile); $this->processFile($this->inputFile2); - $this->assertEquals(83, $this->badgeComposer->getTotalCoverage()); + $this->assertEquals(43, $this->badgeComposer->getTotalCoverage()); } /**