1
|
|
|
<?php |
2
|
|
|
namespace exussum12\CoverageChecker; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
|
6
|
|
|
function findAutoLoader() |
7
|
|
|
{ |
8
|
|
|
$locations = [ |
9
|
|
|
__DIR__ . '/../vendor/autoload.php', |
10
|
|
|
__DIR__ . '/../autoload.php' |
11
|
|
|
]; |
12
|
|
|
|
13
|
|
|
$found = false; |
14
|
|
|
|
15
|
|
|
foreach ($locations as $file) { |
16
|
|
|
if (file_exists($file)) { |
17
|
|
|
require_once($file); |
18
|
|
|
$found = true; |
19
|
|
|
break; |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
// @codeCoverageIgnoreStart |
23
|
|
|
if (!$found) { |
24
|
|
|
error_log( |
25
|
|
|
"Can't find the autoload file," . |
26
|
|
|
"please make sure 'composer install' has been run" |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
exit(1); |
30
|
|
|
// @codeCoverageIgnoreEnd |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
function checkCallIsCorrect(ArgParser $args) |
35
|
|
|
{ |
36
|
|
|
if (!$args->getArg(1) || !$args->getArg(2)) { |
37
|
|
|
throw new Exception( |
38
|
|
|
"Missing arguments, please call with diff and check file\n" . |
39
|
|
|
"e.g. vendor/bin/diffFilter --phpcs diff.txt phpcs.json", |
40
|
|
|
1 |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @codeCoverageIgnore |
47
|
|
|
*/ |
48
|
|
|
function adjustForStdIn($argument) |
49
|
|
|
{ |
50
|
|
|
if ($argument == "-") { |
51
|
|
|
return "php://stdin"; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $argument; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
function getMinPercent($percent) |
58
|
|
|
{ |
59
|
|
|
$minimumPercentCovered = 100; |
60
|
|
|
|
61
|
|
|
if (is_numeric($percent)) { |
62
|
|
|
$minimumPercentCovered = min( |
63
|
|
|
$minimumPercentCovered, |
64
|
|
|
max(0, $percent) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $minimumPercentCovered; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
function handleOutput($lines, $minimumPercentCovered) |
72
|
|
|
{ |
73
|
|
|
$coveredLines = calculateLines($lines['coveredLines']); |
74
|
|
|
$uncoveredLines = calculateLines($lines['uncoveredLines']); |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
if ($coveredLines + $uncoveredLines == 0) { |
78
|
|
|
echo "No lines found!"; |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
$percentCovered = 100 * ($coveredLines / ($coveredLines + $uncoveredLines)); |
82
|
|
|
|
83
|
|
|
$extra = PHP_EOL; |
84
|
|
|
|
85
|
|
|
if ($lines['uncoveredLines']) { |
86
|
|
|
$extra = ', Missed lines '. |
87
|
|
|
$extra . |
88
|
|
|
print_r($lines['uncoveredLines'], true) |
89
|
|
|
; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
printf('%.2f%% Covered%s', $percentCovered, $extra); |
93
|
|
|
|
94
|
|
|
if ($percentCovered >= $minimumPercentCovered) { |
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
throw new Exception( |
99
|
|
|
"Failing due to coverage being lower than threshold", |
100
|
|
|
2 |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
function calculateLines($lines) |
105
|
|
|
{ |
106
|
|
|
return count($lines, COUNT_RECURSIVE) - count($lines); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
function addExceptionHandler() |
110
|
|
|
{ |
111
|
|
|
set_exception_handler( |
112
|
|
|
function (Exception $exception) { |
113
|
|
|
// @codeCoverageIgnoreStart |
114
|
|
|
error_log($exception->getMessage()); |
115
|
|
|
exit($exception->getCode()); |
116
|
|
|
// @codeCoverageIgnoreEnd |
117
|
|
|
} |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
function getFileChecker(ArgParser $args, array $argMappper, $filename) |
122
|
|
|
{ |
123
|
|
|
foreach ($argMappper as $arg => $class) { |
124
|
|
|
if ($args->getArg($arg)) { |
125
|
|
|
$class = __NAMESPACE__ . '\\' . $class; |
126
|
|
|
return new $class($filename); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
printOptions($argMappper); |
130
|
|
|
throw new Exception("Can not find file handler"); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
function printOptions(array $arguments) |
134
|
|
|
{ |
135
|
|
|
$tabWidth = 8; |
136
|
|
|
$defaultWidth = 80; |
137
|
|
|
|
138
|
|
|
$width = (`tput cols` ?: $defaultWidth); |
139
|
|
|
$width -= 2 * $tabWidth; |
140
|
|
|
foreach($arguments as $argument => $class) { |
141
|
|
|
$class = __NAMESPACE__ . '\\' . $class; |
142
|
|
|
|
143
|
|
|
$argument = '--' . $argument; |
144
|
|
|
if (strlen($argument) < $tabWidth) { |
145
|
|
|
$argument .= "\t"; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
printf( |
149
|
|
|
"%s\t%s\n", |
150
|
|
|
$argument, |
151
|
|
|
wordwrap( |
152
|
|
|
$class::getDescription(), |
153
|
|
|
$width, |
154
|
|
|
"\n\t\t", |
155
|
|
|
true |
156
|
|
|
) |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|