5
5
use Exception ;
6
6
use SimpleXMLElement ;
7
7
use Throwable ;
8
+ use function array_sum ;
8
9
use function count ;
9
10
use function explode ;
10
11
use function file_exists ;
@@ -29,9 +30,16 @@ class BadgeComposer
29
30
private string $ outputFile ;
30
31
private string $ coverageName ;
31
32
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 ;
35
43
36
44
/**
37
45
* @throws Exception
@@ -52,7 +60,7 @@ public function __construct(string $inputFiles, string $outputFile, string $cove
52
60
*/
53
61
public function getTotalCoverage (): int
54
62
{
55
- return $ this ->totalCoverage ;
63
+ return array_sum ( $ this ->totalCoverage ) ;
56
64
}
57
65
58
66
/**
@@ -112,12 +120,18 @@ private function processFile(string $inputFile): void
112
120
$ xml = new SimpleXMLElement ($ content );
113
121
$ metrics = $ xml ->xpath ('//metrics ' );
114
122
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 ' ];
117
129
}
118
130
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 );
121
135
122
136
} catch (Throwable $ e ) {
123
137
throw new Exception ($ e ->getMessage ());
@@ -131,7 +145,7 @@ private function processFile(string $inputFile): void
131
145
*/
132
146
private function finalizeCoverage (): void
133
147
{
134
- $ totalCoverage = $ this ->totalCoverage / count ($ this ->inputFiles ); // Average coverage across all files
148
+ $ totalCoverage = $ this ->getTotalCoverage () / count ($ this ->inputFiles ); // Average coverage across all files
135
149
$ template = file_get_contents ($ this ->badgeTemplate );
136
150
137
151
if ($ template === false ) {
0 commit comments