55use Exception ;
66use SimpleXMLElement ;
77use Throwable ;
8+ use function array_sum ;
89use function count ;
910use function explode ;
1011use 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 ) {
0 commit comments