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