|
1 | 1 | <?php
|
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
2 | 5 | // Inspired by: https://ocramius.github.io/blog/automated-code-coverage-check-for-github-pull-requests-with-travis/
|
3 |
| -if (!isset($argv[1]) || !file_exists($argv[1])) { |
4 |
| - echo 'Invalid input file provided' . PHP_EOL; |
5 |
| - exit(1); |
| 6 | + |
| 7 | +const XPATH_METRICS = '//metrics'; |
| 8 | +const STATUS_OK = 0; |
| 9 | +const STATUS_ERROR = 1; |
| 10 | + |
| 11 | +function formatCoverage(float $number): string |
| 12 | +{ |
| 13 | + return sprintf('%0.2f %%', $number); |
6 | 14 | }
|
7 | 15 |
|
8 |
| -if (!isset($argv[2])) { |
9 |
| - echo 'An integer checked percentage must be given as second parameter'. PHP_EOL; |
10 |
| - exit(1); |
| 16 | +function loadMetrics(string $file): array |
| 17 | +{ |
| 18 | + $xml = new SimpleXMLElement(file_get_contents($file)); |
| 19 | + |
| 20 | + return $xml->xpath(XPATH_METRICS); |
11 | 21 | }
|
12 | 22 |
|
13 |
| -$onlyEchoPercentage = false; |
| 23 | +function printStatus(string $msg, int $exitCode = STATUS_OK) |
| 24 | +{ |
| 25 | + echo $msg.PHP_EOL; |
| 26 | + exit($exitCode); |
| 27 | +} |
14 | 28 |
|
15 |
| -if (isset($argv[3]) && $argv[3] === 'only-percentage') { |
16 |
| - $onlyEchoPercentage = true; |
| 29 | +if (! isset($argv[1]) || ! file_exists($argv[1])) { |
| 30 | + printStatus("Invalid input file {$argv[1]} provided.", STATUS_ERROR); |
17 | 31 | }
|
18 | 32 |
|
19 |
| -$inputFile = $argv[1]; |
20 |
| -$percentage = min(100, max(0, (float)$argv[2])); |
| 33 | +if (! isset($argv[2])) { |
| 34 | + printStatus( |
| 35 | + 'An integer checked percentage must be given as second parameter.', |
| 36 | + STATUS_ERROR |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +$onlyEchoPercentage = isset($argv[3]) && $argv[3] === '--only-percentage'; |
21 | 41 |
|
22 |
| -$xml = new SimpleXMLElement(file_get_contents($inputFile)); |
23 |
| -$metrics = $xml->xpath('//metrics'); |
| 42 | +$inputFile = $argv[1]; |
| 43 | +$percentage = min(100, max(0, (float) $argv[2])); |
24 | 44 |
|
25 | 45 | $elements = 0;
|
26 | 46 | $coveredElements = 0;
|
|
29 | 49 | $methods = 0;
|
30 | 50 | $coveredmethods = 0;
|
31 | 51 |
|
32 |
| -foreach ($metrics as $metric) { |
33 |
| - $elements += (int)$metric['elements']; |
34 |
| - $coveredElements += (int)$metric['coveredelements']; |
35 |
| - $statements += (int)$metric['statements']; |
36 |
| - $coveredstatements += (int)$metric['coveredstatements']; |
37 |
| - $methods += (int)$metric['methods']; |
38 |
| - $coveredmethods += (int)$metric['coveredmethods']; |
| 52 | +foreach (loadMetrics($inputFile) as $metric) { |
| 53 | + $elements += (int) $metric['elements']; |
| 54 | + $coveredElements += (int) $metric['coveredelements']; |
| 55 | + $statements += (int) $metric['statements']; |
| 56 | + $coveredstatements += (int) $metric['coveredstatements']; |
| 57 | + $methods += (int) $metric['methods']; |
| 58 | + $coveredmethods += (int) $metric['coveredmethods']; |
39 | 59 | }
|
40 | 60 |
|
41 | 61 | // See calculation: https://confluence.atlassian.com/pages/viewpage.action?pageId=79986990
|
42 |
| -$TPC = ($coveredstatements + $coveredmethods + $coveredElements) / ($statements + $methods + $elements) * 100; |
| 62 | +$coveredMetrics = $coveredstatements + $coveredmethods + $coveredElements; |
| 63 | +$totalMetrics = $statements + $methods + $elements; |
| 64 | + |
| 65 | +if ($totalMetrics === 0) { |
| 66 | + printStatus('Insufficient data for calculation. Please add more code.', STATUS_ERROR); |
| 67 | +} |
| 68 | + |
| 69 | +$totalPercentageCoverage = $coveredMetrics / $totalMetrics * 100; |
43 | 70 |
|
44 |
| -if ($TPC < $percentage && ! $onlyEchoPercentage) { |
45 |
| - echo 'Total code coverage is ' . sprintf('%0.2f', $TPC) . '%, which is below the accepted ' . $percentage . '%' . PHP_EOL; |
46 |
| - exit(1); |
| 71 | +if ($totalPercentageCoverage < $percentage && ! $onlyEchoPercentage) { |
| 72 | + printStatus( |
| 73 | + 'Total code coverage is '.formatCoverage($totalPercentageCoverage).' which is below the accepted '.$percentage.'%', |
| 74 | + STATUS_ERROR |
| 75 | + ); |
47 | 76 | }
|
48 | 77 |
|
49 |
| -if ($TPC < $percentage && $onlyEchoPercentage) { |
50 |
| - echo sprintf('%0.2f', $TPC) . PHP_EOL; |
51 |
| - exit(1); |
| 78 | +if ($totalPercentageCoverage < $percentage && $onlyEchoPercentage) { |
| 79 | + printStatus(formatCoverage($totalPercentageCoverage), STATUS_ERROR); |
52 | 80 | }
|
53 | 81 |
|
54 | 82 | if ($onlyEchoPercentage) {
|
55 |
| - echo sprintf('%0.2f', $TPC) . PHP_EOL; |
56 |
| - exit(0); |
| 83 | + printStatus(formatCoverage($totalPercentageCoverage)); |
57 | 84 | }
|
58 | 85 |
|
59 |
| -echo 'Total code coverage is ' . sprintf('%0.2f', $TPC) . '% - OK!' . PHP_EOL; |
| 86 | +printStatus('Total code coverage is '.formatCoverage($totalPercentageCoverage).' – OK!'); |
0 commit comments