1
1
<?php
2
2
3
- $ inputFile = $ argv [1 ];
3
+ $ inputFiles = explode ( ' , ' , $ argv [1 ]) ;
4
4
$ outputFile = $ argv [2 ];
5
5
$ coverageName = isset ($ argv [3 ]) ? $ argv [3 ] : 'coverage ' ;
6
6
7
- if (! file_exists ( $ inputFile )) {
8
- throw new InvalidArgumentException ( ' Invalid input file provided ' ) ;
9
- }
7
+ $ totalCoverage = 0 ;
8
+ $ totalElements = 0 ;
9
+ $ checkedElements = 0 ;
10
10
11
- try {
12
- $ xml = new SimpleXMLElement (file_get_contents ($ inputFile ));
11
+ foreach ($ inputFiles as $ inputFile ) {
12
+ if (!file_exists ($ inputFile )) {
13
+ throw new InvalidArgumentException ('Invalid input file provided: ' . $ inputFile );
14
+ }
13
15
14
- $ metrics = $ xml ->xpath ('//metrics ' );
15
- $ totalElements = 0 ;
16
- $ checkedElements = 0 ;
16
+ try {
17
+ $ xml = new SimpleXMLElement (file_get_contents ($ inputFile ));
17
18
18
- foreach ($ metrics as $ metric ) {
19
- $ totalElements += (int ) $ metric ['elements ' ];
20
- $ checkedElements += (int ) $ metric ['coveredelements ' ];
21
- }
19
+ $ metrics = $ xml ->xpath ('//metrics ' );
22
20
23
- $ coverage = (int )(($ totalElements === 0 ) ? 0 : ($ checkedElements / $ totalElements ) * 100 );
21
+ foreach ($ metrics as $ metric ) {
22
+ $ totalElements += (int ) $ metric ['elements ' ];
23
+ $ checkedElements += (int ) $ metric ['coveredelements ' ];
24
+ }
24
25
25
- $ template = file_get_contents (__DIR__ . '/templates/badge.svg ' );
26
+ $ coverage = round (($ totalElements === 0 ) ? 0 : ($ checkedElements / $ totalElements ) * 100 );
27
+ $ totalCoverage += $ coverage ;
28
+ } catch (Exception $ e ) {
29
+ echo $ e ->getMessage ();
30
+ }
31
+ }
26
32
27
- $ template = str_replace ( ' {{ total }} ' , $ coverage, $ template );
33
+ $ totalCoverage = $ totalCoverage / count ( $ inputFiles ); // Average coverage across all files
28
34
29
- $ template = str_replace ( ' {{ coverage }} ' , $ coverageName , $ template );
35
+ $ template = file_get_contents ( __DIR__ . ' /templates/badge.svg ' );
30
36
31
- $ color = '#a4a61d ' ; // Default Gray
32
- if ($ coverage < 40 ) {
33
- $ color = '#e05d44 ' ; // Red
34
- } elseif ($ coverage < 60 ) {
35
- $ color = '#fe7d37 ' ; // Orange
36
- } elseif ($ coverage < 75 ) {
37
- $ color = '#dfb317 ' ; // Yellow
38
- } elseif ($ coverage < 90 ) {
39
- $ color = '#a4a61d ' ; // Yellow-Green
40
- } elseif ($ coverage < 95 ) {
41
- $ color = '#97CA00 ' ; // Green
42
- } elseif ($ coverage <= 100 ) {
43
- $ color = '#4c1 ' ; // Bright Green
44
- }
37
+ $ template = str_replace ('{{ total }} ' , $ totalCoverage , $ template );
45
38
46
- $ template = str_replace ('{{ total }} ' , $ coverage , $ template );
47
- $ template = str_replace ('{{ color }} ' , $ color , $ template );
39
+ $ template = str_replace ('{{ coverage }} ' , $ coverageName , $ template );
48
40
49
- file_put_contents ($ outputFile , $ template );
50
- } catch (Exception $ e ) {
51
- echo $ e ->getMessage ();
41
+ $ color = '#a4a61d ' ; // Default Gray
42
+ if ($ totalCoverage < 40 ) {
43
+ $ color = '#e05d44 ' ; // Red
44
+ } elseif ($ totalCoverage < 60 ) {
45
+ $ color = '#fe7d37 ' ; // Orange
46
+ } elseif ($ totalCoverage < 75 ) {
47
+ $ color = '#dfb317 ' ; // Yellow
48
+ } elseif ($ totalCoverage < 90 ) {
49
+ $ color = '#a4a61d ' ; // Yellow-Green
50
+ } elseif ($ totalCoverage < 95 ) {
51
+ $ color = '#97CA00 ' ; // Green
52
+ } elseif ($ totalCoverage <= 100 ) {
53
+ $ color = '#4c1 ' ; // Bright Green
52
54
}
53
55
56
+ $ template = str_replace ('{{ total }} ' , $ totalCoverage , $ template );
57
+ $ template = str_replace ('{{ color }} ' , $ color , $ template );
54
58
59
+ file_put_contents ($ outputFile , $ template );
0 commit comments