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