| Total Complexity | 42 |
| Total Lines | 377 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like OutputHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OutputHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 63 | class OutputHelper |
||
| 64 | { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The writer stream instance |
||
| 68 | * @var Writer |
||
| 69 | */ |
||
| 70 | protected Writer $writer; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Max width of command name |
||
| 74 | * @var int |
||
| 75 | */ |
||
| 76 | protected int $maxCommandWidth = 0; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Create new instance |
||
| 80 | * @param Writer|null $writer |
||
| 81 | */ |
||
| 82 | public function __construct(?Writer $writer = null) |
||
| 83 | { |
||
| 84 | $this->writer = $writer ? $writer : new Writer(); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Print stack trace and error message of an exception. |
||
| 89 | * @param Throwable $error |
||
| 90 | * @return void |
||
| 91 | */ |
||
| 92 | public function printTrace(Throwable $error): void |
||
| 93 | { |
||
| 94 | $errorClass = get_class($error); |
||
| 95 | |||
| 96 | $this->writer->colors(sprintf( |
||
| 97 | '%s <error>%s</end></eol> (thrown in <ok>%s</end>' |
||
| 98 | . '<comment>:%s)</end></eol></eol>', |
||
| 99 | $errorClass, |
||
| 100 | $error->getMessage(), |
||
| 101 | $error->getFile(), |
||
| 102 | $error->getLine(), |
||
| 103 | )); |
||
| 104 | |||
| 105 | if ($error instanceof ConsoleException) { |
||
| 106 | // Internal exception traces are not printed. |
||
| 107 | return; |
||
| 108 | } |
||
| 109 | |||
| 110 | $traceStr = '</eol></eol><info>Stack Trace:</end></eol></eol>'; |
||
| 111 | foreach ($error->getTrace() as $i => $trace) { |
||
| 112 | $trace += [ |
||
| 113 | 'class' => '', |
||
| 114 | 'type' => '', |
||
| 115 | 'function' => '', |
||
| 116 | 'file' => '', |
||
| 117 | 'line' => '', |
||
| 118 | 'args' => [] |
||
| 119 | ]; |
||
| 120 | $symbol = $trace['class'] . $trace['type'] . $trace['function']; |
||
| 121 | $arguments = $this->stringifyArgs($trace['args']); |
||
| 122 | |||
| 123 | $traceStr .= ' <comment>' . $i . ')</end> <error>' |
||
| 124 | . $symbol . '</end><comment>(' . $arguments . ')</end>'; |
||
| 125 | |||
| 126 | if ($trace['file'] !== '') { |
||
| 127 | $file = realpath($trace['file']); |
||
| 128 | $traceStr .= '</eol> <yellow>at ' . $file |
||
| 129 | . '</end><white>:' . $trace['line'] . '</end></eol>'; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | $this->writer->colors($traceStr); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Show arguments help |
||
| 138 | * @param array<\Platine\Console\Input\Argument> $items |
||
| 139 | * @param string $header |
||
| 140 | * @param string $footer |
||
| 141 | * @return $this |
||
| 142 | */ |
||
| 143 | public function showArgumentsHelp( |
||
| 144 | array $items, |
||
| 145 | string $header = '', |
||
| 146 | string $footer = '' |
||
| 147 | ): self { |
||
| 148 | $this->showHelp('Arguments', $items, $header, $footer); |
||
| 149 | |||
| 150 | return $this; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Show options help |
||
| 155 | * @param array<Option> $items |
||
| 156 | * @param string $header |
||
| 157 | * @param string $footer |
||
| 158 | * @return $this |
||
| 159 | */ |
||
| 160 | public function showOptionsHelp( |
||
| 161 | array $items, |
||
| 162 | string $header = '', |
||
| 163 | string $footer = '' |
||
| 164 | ): self { |
||
| 165 | $this->showHelp('Options', $items, $header, $footer); |
||
| 166 | |||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Show commands help |
||
| 172 | * @param array<Command> $items |
||
| 173 | * @param string $header |
||
| 174 | * @param string $footer |
||
| 175 | * @return $this |
||
| 176 | */ |
||
| 177 | public function showCommandsHelp( |
||
| 178 | array $items, |
||
| 179 | string $header = '', |
||
| 180 | string $footer = '' |
||
| 181 | ): self { |
||
| 182 | $this->maxCommandWidth = !empty($items) ? max(array_map(function (Command $item) { |
||
| 183 | return strlen($item->getName()); |
||
| 184 | }, $items)) : 0; |
||
| 185 | |||
| 186 | $this->showHelp('Commands', $items, $header, $footer); |
||
| 187 | |||
| 188 | return $this; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Show usage examples of a Command. |
||
| 193 | * It replaces $0 with actual command name |
||
| 194 | * and properly pads ` ## ` segments. |
||
| 195 | * @param string $description |
||
| 196 | * @param string $cmdName |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | public function showUsage(string $description, string $cmdName = ''): self |
||
| 200 | { |
||
| 201 | $usage = str_replace('$0', $cmdName ? $cmdName : '[cmd]', $description); |
||
| 202 | |||
| 203 | if (strpos($usage, ' ## ') === false) { |
||
| 204 | $this->writer->eol() |
||
| 205 | ->green('Usage Examples: ', true, null, Color::BOLD) |
||
| 206 | ->colors($usage)->eol(); |
||
| 207 | |||
| 208 | return $this; |
||
| 209 | } |
||
| 210 | |||
| 211 | $lines = explode("\n", str_replace( |
||
| 212 | ['</eol>', "\r\n"], |
||
| 213 | "\n", |
||
| 214 | $usage |
||
| 215 | )); |
||
| 216 | |||
| 217 | if (!is_array($lines)) { |
||
| 218 | return $this; |
||
| 219 | } |
||
| 220 | |||
| 221 | foreach ($lines as $i => &$pos) { |
||
| 222 | $replace = preg_replace('~</?\w+>~', '', $pos); |
||
| 223 | if ($replace !== null) { |
||
| 224 | $pos = strrpos($replace, ' ##'); |
||
| 225 | } |
||
| 226 | if ($pos === false) { |
||
| 227 | unset($lines[$i]); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | $maxLength = (int) max($lines) + 4; |
||
| 232 | $formatedUsage = (string) preg_replace_callback( |
||
| 233 | '~ ## ~', |
||
| 234 | function () use (&$lines, $maxLength) { |
||
| 235 | $sizeOfLine = 0; |
||
| 236 | $currentLine = array_shift($lines); |
||
| 237 | if ($currentLine !== null) { |
||
| 238 | $sizeOfLine = (int) $currentLine; |
||
| 239 | } |
||
| 240 | return str_pad('# ', $maxLength - $sizeOfLine, ' ', STR_PAD_LEFT); |
||
| 241 | }, |
||
| 242 | $usage |
||
| 243 | ); |
||
| 244 | |||
| 245 | $this->writer->eol() |
||
| 246 | ->green('Usage Examples: ', true, null, Color::BOLD) |
||
| 247 | ->colors($formatedUsage)->eol(); |
||
| 248 | |||
| 249 | return $this; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Show command not found error |
||
| 254 | * @param string $command |
||
| 255 | * @param array<int, string> $available |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | public function showCommandNotFound(string $command, array $available): self |
||
| 259 | { |
||
| 260 | $closest = []; |
||
| 261 | |||
| 262 | foreach ($available as $cmd) { |
||
| 263 | $lev = levenshtein($command, $cmd); |
||
| 264 | if ($lev > 0 || $lev < 5) { |
||
| 265 | $closest[$cmd] = $lev; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | $this->writer->writeError(sprintf( |
||
| 270 | 'Command "%s" not found', |
||
| 271 | $command |
||
| 272 | ), true); |
||
| 273 | |||
| 274 | if (!empty($closest)) { |
||
| 275 | asort($closest); |
||
| 276 | $choosen = key($closest); |
||
| 277 | |||
| 278 | $this->writer->eol()->bgRed( |
||
| 279 | sprintf('Did you mean %s ?', $choosen), |
||
| 280 | true |
||
| 281 | ); |
||
| 282 | } |
||
| 283 | |||
| 284 | return $this; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Convert arguments to string |
||
| 289 | * @param array<int, mixed> $args |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | protected function stringifyArgs(array $args): string |
||
| 293 | { |
||
| 294 | $holder = []; |
||
| 295 | foreach ($args as $arg) { |
||
| 296 | $holder[] = $this->stringifyArg($arg); |
||
| 297 | } |
||
| 298 | |||
| 299 | return implode(', ', $holder); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Convert one argument to string |
||
| 304 | * @param mixed $arg |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | protected function stringifyArg($arg): string |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Show help for given type (option, argument, command) |
||
| 328 | * with header and footer |
||
| 329 | * @param string $type |
||
| 330 | * @param array<Parameter|Command> $items |
||
| 331 | * @param string $header |
||
| 332 | * @param string $footer |
||
| 333 | * @return void |
||
| 334 | */ |
||
| 335 | protected function showHelp( |
||
| 336 | string $type, |
||
| 337 | array $items, |
||
| 338 | string $header = '', |
||
| 339 | string $footer = '' |
||
| 340 | ): void { |
||
| 341 | if ($header) { |
||
| 342 | $this->writer->bold($header, true); |
||
| 343 | } |
||
| 344 | |||
| 345 | $this->writer->eol()->green( |
||
| 346 | $type . ':', |
||
| 347 | true, |
||
| 348 | null, |
||
| 349 | Color::BOLD |
||
| 350 | ); |
||
| 351 | |||
| 352 | if (empty($items)) { |
||
| 353 | $this->writer->bold(' (n/a)', true); |
||
| 354 | |||
| 355 | return; |
||
| 356 | } |
||
| 357 | |||
| 358 | $space = 4; |
||
| 359 | $padLength = 0; |
||
| 360 | foreach ($this->sortItems($items, $padLength) as $item) { |
||
| 361 | $name = $this->getName($item); |
||
| 362 | $desc = str_replace( |
||
| 363 | ["\r\n", "\n"], |
||
| 364 | str_pad("\n", $padLength + $space + 3), |
||
| 365 | $item->getDescription() |
||
| 366 | ); |
||
| 367 | |||
| 368 | $this->writer->bold( |
||
| 369 | str_pad( |
||
| 370 | $name, |
||
| 371 | $padLength + $space |
||
| 372 | ), |
||
| 373 | ); |
||
| 374 | $this->writer->dim($desc, true); |
||
| 375 | } |
||
| 376 | |||
| 377 | if ($footer) { |
||
| 378 | $this->writer->eol()->yellow($footer, true); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Sort items by name. As a side effect sets max length of all names. |
||
| 384 | * @param array<Command|Parameter> $items |
||
| 385 | * @param int $max |
||
| 386 | * @return array<Command|Parameter> |
||
| 387 | */ |
||
| 388 | protected function sortItems(array $items, int &$max = 0): array |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Prepare name for different items. |
||
| 405 | * @param Parameter|Command $item |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | protected function getName($item): string |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get parameter label for humans readable |
||
| 423 | * @param Parameter $item |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | protected function getLabel(Parameter $item): string |
||
| 440 | } |
||
| 441 | } |
||
| 442 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths