| Total Complexity | 42 |
| Total Lines | 380 |
| 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 | * The writer stream instance |
||
| 67 | * @var Writer |
||
| 68 | */ |
||
| 69 | protected Writer $writer; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Max width of command name |
||
| 73 | * @var int |
||
| 74 | */ |
||
| 75 | protected int $maxCommandWidth = 0; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Create new instance |
||
| 79 | * @param Writer|null $writer |
||
| 80 | */ |
||
| 81 | public function __construct(?Writer $writer = null) |
||
| 82 | { |
||
| 83 | $this->writer = $writer ?? new Writer(); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Print stack trace and error message of an exception. |
||
| 88 | * @param Throwable $error |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | public function printTrace(Throwable $error): void |
||
| 92 | { |
||
| 93 | $errorClass = get_class($error); |
||
| 94 | |||
| 95 | $this->writer->colors(sprintf( |
||
| 96 | '%s <error>%s</end></eol> (thrown in <ok>%s</end>' |
||
| 97 | . '<comment>:%s)</end></eol></eol>', |
||
| 98 | $errorClass, |
||
| 99 | $error->getMessage(), |
||
| 100 | $error->getFile(), |
||
| 101 | $error->getLine(), |
||
| 102 | )); |
||
| 103 | |||
| 104 | if ($error instanceof ConsoleException) { |
||
| 105 | // Internal exception traces are not printed. |
||
| 106 | return; |
||
| 107 | } |
||
| 108 | |||
| 109 | $traceStr = '</eol></eol><info>Stack Trace:</end></eol></eol>'; |
||
| 110 | foreach ($error->getTrace() as $i => $trace) { |
||
| 111 | $trace += [ |
||
| 112 | 'class' => '', |
||
| 113 | 'type' => '', |
||
| 114 | 'function' => '', |
||
| 115 | 'file' => '', |
||
| 116 | 'line' => '', |
||
| 117 | 'args' => [] |
||
| 118 | ]; |
||
| 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 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 Option[] $items |
||
| 156 | * @param string $header |
||
| 157 | * @param string $footer |
||
| 158 | * @return $this |
||
| 159 | */ |
||
| 160 | public function showOptionsHelp( |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Show commands help |
||
| 172 | * @param Command[] $items |
||
| 173 | * @param string $header |
||
| 174 | * @param string $footer |
||
| 175 | * @return $this |
||
| 176 | */ |
||
| 177 | public function showCommandsHelp( |
||
| 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()->boldGreen('Usage Examples: ', true) |
||
| 205 | ->colors($usage)->eol(); |
||
| 206 | |||
| 207 | return $this; |
||
| 208 | } |
||
| 209 | |||
| 210 | $lines = explode("\n", str_replace( |
||
| 211 | ['</eol>', "\r\n"], |
||
| 212 | "\n", |
||
| 213 | $usage |
||
| 214 | )); |
||
| 215 | |||
| 216 | if (!is_array($lines)) { |
||
| 217 | return $this; |
||
| 218 | } |
||
| 219 | |||
| 220 | foreach ($lines as $i => &$pos) { |
||
| 221 | $replace = (string) preg_replace('~</?\w+>~', '', $pos); |
||
| 222 | $pos = strrpos($replace, ' ##'); |
||
| 223 | |||
| 224 | if ($pos === false) { |
||
| 225 | unset($lines[$i]); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | $maxLength = 0; |
||
| 230 | if (count($lines) > 0) { |
||
| 231 | $maxLength = (int) max($lines) + 4; |
||
| 232 | } |
||
| 233 | |||
| 234 | $formatedUsage = (string) preg_replace_callback( |
||
| 235 | '~ ## ~', |
||
| 236 | function () use (&$lines, $maxLength) { |
||
| 237 | $sizeOfLine = 0; |
||
| 238 | $currentLine = array_shift($lines); |
||
| 239 | if ($currentLine !== null) { |
||
| 240 | $sizeOfLine = (int) $currentLine; |
||
| 241 | } |
||
| 242 | return str_pad('# ', $maxLength - $sizeOfLine, ' ', STR_PAD_LEFT); |
||
| 243 | }, |
||
| 244 | $usage |
||
| 245 | ); |
||
| 246 | |||
| 247 | $this->writer->eol() |
||
| 248 | ->boldGreen('Usage Examples: ', true) |
||
| 249 | ->colors($formatedUsage)->eol(); |
||
| 250 | |||
| 251 | return $this; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Show command not found error |
||
| 256 | * @param string $command |
||
| 257 | * @param array<int, string> $available |
||
| 258 | * @return $this |
||
| 259 | */ |
||
| 260 | public function showCommandNotFound(string $command, array $available): self |
||
| 261 | { |
||
| 262 | $closest = []; |
||
| 263 | |||
| 264 | foreach ($available as $cmd) { |
||
| 265 | $lev = levenshtein($command, $cmd); |
||
| 266 | if ($lev > 0 && $lev < 5) { |
||
| 267 | $closest[$cmd] = $lev; |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | $this->writer->writeError(sprintf( |
||
| 272 | 'Command "%s" not found', |
||
| 273 | $command |
||
| 274 | ), true); |
||
| 275 | |||
| 276 | if (!empty($closest)) { |
||
| 277 | asort($closest); |
||
| 278 | $choosen = key($closest); |
||
| 279 | |||
| 280 | $this->writer->eol()->bgRed( |
||
| 281 | sprintf('Did you mean %s ?', $choosen), |
||
| 282 | true |
||
| 283 | ); |
||
| 284 | } |
||
| 285 | |||
| 286 | return $this; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Convert arguments to string |
||
| 291 | * @param array<int, mixed> $args |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | protected function stringifyArgs(array $args): string |
||
| 295 | { |
||
| 296 | $holder = []; |
||
| 297 | foreach ($args as $arg) { |
||
| 298 | $holder[] = $this->stringifyArg($arg); |
||
| 299 | } |
||
| 300 | |||
| 301 | return implode(', ', $holder); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Convert one argument to string |
||
| 306 | * @param mixed $arg |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | protected function stringifyArg(mixed $arg): string |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Show help for given type (option, argument, command) |
||
| 330 | * with header and footer |
||
| 331 | * @param string $type |
||
| 332 | * @param Parameter[]|Command[] $items |
||
| 333 | * @param string $header |
||
| 334 | * @param string $footer |
||
| 335 | * @return void |
||
| 336 | */ |
||
| 337 | protected function showHelp( |
||
| 338 | string $type, |
||
| 339 | array $items, |
||
| 340 | string $header = '', |
||
| 341 | string $footer = '' |
||
| 342 | ): void { |
||
| 343 | if (!empty($header)) { |
||
| 344 | $this->writer->bold($header, true); |
||
| 345 | } |
||
| 346 | |||
| 347 | $this->writer->eol()->boldGreen( |
||
| 348 | $type . ':', |
||
| 349 | true |
||
| 350 | ); |
||
| 351 | |||
| 352 | if (count($items) === 0) { |
||
| 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 (!empty($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 Command[]|Parameter[] $items |
||
| 385 | * @param int $max |
||
| 386 | * @return Command[]|Parameter[] |
||
| 387 | */ |
||
| 388 | protected function sortItems(array $items, int &$max = 0): array |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Prepare name for different items. |
||
| 408 | * @param Parameter|Command $item |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | protected function getName(Parameter|Command $item): string |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Get parameter label for humans readable |
||
| 426 | * @param Parameter $item |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | protected function getLabel(Parameter $item): string |
||
| 445 |