Complex classes like TShortcode 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 TShortcode, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | trait TShortcode |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Shortcode to quicker format text as HTML. |
||
| 13 | * |
||
| 14 | * @param string $text text to be converted. |
||
| 15 | * |
||
| 16 | * @return string the formatted text. |
||
| 17 | */ |
||
| 18 | 1 | public function shortCode($text) |
|
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * Init shortcode handling by preparing the option list to an array, |
||
| 116 | * for those using arguments. |
||
| 117 | * |
||
| 118 | * @param string $options for the shortcode. |
||
| 119 | * |
||
| 120 | * @return array with all the options. |
||
| 121 | */ |
||
| 122 | public static function shortCodeInit($options) |
||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Shortcode for [YOUTUBE]. |
||
| 145 | * |
||
| 146 | * Usage example: [YOUTUBE src=id-for-the-tube width=630 caption=""] |
||
| 147 | * |
||
| 148 | * @param string $options for the shortcode. |
||
| 149 | * |
||
| 150 | * @return array with all the options. |
||
| 151 | */ |
||
| 152 | public static function shortCodeYoutube($options) |
||
| 191 | 1 | ||
| 192 | 1 | ||
| 193 | 1 | ||
| 194 | 1 | /** |
|
| 195 | 1 | * Shortcode for [CODEPEN]. |
|
| 196 | * |
||
| 197 | * Usage example: [CODEPEN src=id-for-the-pen user="mosbth" |
||
| 198 | 1 | * tab="js,result" caption="caption"] |
|
| 199 | 1 | * |
|
| 200 | 1 | * @param string $options for the shortcode. |
|
| 201 | 1 | * |
|
| 202 | 1 | * @return array with all the options. |
|
| 203 | */ |
||
| 204 | 1 | public static function shortCodeCodepen($options) |
|
| 243 | |||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * Shortcode for <figure>. |
||
| 248 | * |
||
| 249 | * Usage example: [FIGURE src="img/home/me.jpg" caption="Me" alt="Bild på mig" nolink="nolink"] |
||
| 250 | * |
||
| 251 | * @param string $options for the shortcode. |
||
| 252 | * |
||
| 253 | * @return array with all the options. |
||
| 254 | */ |
||
| 255 | public static function shortCodeFigure($options) |
||
| 306 | |||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * Shortcode for [asciinema]. |
||
| 311 | * |
||
| 312 | * @param string $code the code to process. |
||
| 313 | * @param string $options for the shortcode. |
||
| 314 | * @return array with all the options. |
||
| 315 | */ |
||
| 316 | public static function shortCodeAsciinema($options) |
||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * Shortcode for [book]. |
||
| 349 | * |
||
| 350 | * @param string $code the code to process. |
||
| 351 | * @param string $options for the shortcode. |
||
| 352 | * @return array with all the options. |
||
| 353 | */ |
||
| 354 | public static function shortCodeBook($options) |
||
| 381 | |||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * Shortcode for including a SVG-image inside a <figure>. |
||
| 386 | * |
||
| 387 | * @param string $code the code to process. |
||
| 388 | * @param string $options for the shortcode. |
||
| 389 | * @return array with all the options. |
||
| 390 | */ |
||
| 391 | /* public static function ShortCodeSVGFigure($options) { |
||
| 392 | // Merge incoming options with default and expose as variables |
||
| 393 | $options= array_merge( |
||
| 394 | [ |
||
| 395 | "id" => null, |
||
| 396 | "class" => null, |
||
| 397 | "src" => null, |
||
| 398 | "title" => null, |
||
| 399 | "alt" => null, |
||
| 400 | "caption" => null, |
||
| 401 | "href" => null, |
||
| 402 | "nolink" => false, |
||
| 403 | //'path' => null, |
||
| 404 | ], |
||
| 405 | self::ShortCodeInit($options) |
||
| 406 | ); |
||
| 407 | extract($options, EXTR_SKIP); |
||
| 408 | |||
| 409 | $id = $id ? " id=\"$id\"" : null; |
||
| 410 | $class = $class ? " class=\"figure $class\"" : " class=\"figure\""; |
||
| 411 | $title = $title ? " title=\"$title\"" : null; |
||
| 412 | |||
| 413 | if (!$alt && $caption) { |
||
| 414 | $alt = $caption; |
||
| 415 | } |
||
| 416 | |||
| 417 | if (!$href) { |
||
| 418 | $pos = strpos($src, "?"); |
||
| 419 | $href = $pos ? substr($src, 0, $pos) : $src; |
||
| 420 | } |
||
| 421 | |||
| 422 | $start = null; |
||
| 423 | $end = null; |
||
| 424 | if (!$nolink) { |
||
| 425 | $start = "<a href=\"{$href}\">"; |
||
| 426 | $end = "</a>"; |
||
| 427 | } |
||
| 428 | |||
| 429 | // Import the file containing the svg-image |
||
| 430 | /* |
||
| 431 | $svg = null; |
||
| 432 | |||
| 433 | if($path[0] != '/') { |
||
| 434 | $path = self::$dir . '/' . $path; |
||
| 435 | } |
||
| 436 | |||
| 437 | if(is_file($path)) { |
||
| 438 | $svg = file_get_contents($path); |
||
| 439 | } |
||
| 440 | else { |
||
| 441 | $svg = "No such file: $path"; |
||
| 442 | } |
||
| 443 | $html = <<<EOD |
||
| 444 | <figure{$id}{$class}> |
||
| 445 | {$svg} |
||
| 446 | <figcaption markdown=1>{$caption}</figcaption> |
||
| 447 | </figure> |
||
| 448 | EOD;*/ |
||
| 449 | /* |
||
| 450 | $html = <<<EOD |
||
| 451 | <figure{$id}{$class}> |
||
| 452 | {$start}<img src="{$src}" alt="{$alt}"{$title}/>{$end} |
||
| 453 | <figcaption markdown=1>{$caption}</figcaption> |
||
| 454 | </figure> |
||
| 455 | EOD; |
||
| 456 | |||
| 457 | return $html;*/ |
||
| 458 | /* } |
||
| 459 | */ |
||
| 460 | |||
| 461 | |||
| 462 | |||
| 463 | /** |
||
| 464 | * Shorttags to to quicker format text as HTML. |
||
| 465 | * |
||
| 466 | * @param string text text to be converted. |
||
| 467 | * @return string the formatted text. |
||
| 468 | */ |
||
| 469 | /*public static function ShortTags($text) { |
||
| 470 | $callback = function($matches) { |
||
| 471 | switch($matches[1]) { |
||
| 472 | case 'IMG': |
||
| 473 | $caption = t('Image: '); |
||
| 474 | $pos = strpos($matches[2], '?'); |
||
| 475 | $href = $pos ? substr($matches[2], 0, $pos) : $matches[2]; |
||
| 476 | $src = htmlspecialchars($matches[2]); |
||
| 477 | return <<<EOD |
||
| 478 | <figure> |
||
| 479 | <a href='{$href}'><img src='{$src}' alt='{$matches[3]}' /></a> |
||
| 480 | <figcaption markdown=1>{$caption}{$matches[3]}</figcaption> |
||
| 481 | </figure> |
||
| 482 | EOD; |
||
| 483 | |||
| 484 | case 'IMG2': |
||
| 485 | $caption = null; //t('Image: '); |
||
| 486 | $pos = strpos($matches[2], '?'); |
||
| 487 | $href = $pos ? substr($matches[2], 0, $pos) : $matches[2]; |
||
| 488 | $src = htmlspecialchars($matches[2]); |
||
| 489 | return <<<EOD |
||
| 490 | <figure class="{$matches[4]}"> |
||
| 491 | <a href='{$href}'><img src='{$src}' alt='{$matches[3]}' /></a> |
||
| 492 | <figcaption markdown=1>{$caption}{$matches[3]}</figcaption> |
||
| 493 | </figure> |
||
| 494 | EOD; |
||
| 495 | case 'BOOK': |
||
| 496 | $isbn = $matches[2]; |
||
| 497 | $stores = array( |
||
| 498 | 'BTH' => "http://bth.summon.serialssolutions.com/?#!/search?ho=t&q={$isbn}", |
||
| 499 | 'Libris' => "http://libris.kb.se/hitlist?q={$isbn}", |
||
| 500 | 'Google Books' => "http://books.google.com/books?q={$isbn}", |
||
| 501 | 'Bokus' => "http://www.bokus.com/bok/{$isbn}", |
||
| 502 | 'Adlibris' => "http://www.adlibris.com/se/product.aspx?isbn={$isbn}", |
||
| 503 | 'Amazon' => "http://www.amazon.com/s/ref=nb_ss?url=field-keywords={$isbn}", |
||
| 504 | 'Barnes&Noble' => "http://search.barnesandnoble.com/booksearch/ISBNInquiry.asp?r=1&IF=N&EAN={$isbn}", |
||
| 505 | ); |
||
| 506 | $html = null; |
||
| 507 | foreach($stores as $key => $val) { |
||
| 508 | $html .= "<a href='$val'>$key</a> • "; |
||
| 509 | } |
||
| 510 | return substr($html, 0, -8); |
||
| 511 | break; |
||
| 512 | |||
| 513 | case 'YOUTUBE': |
||
| 514 | $caption = t('Figure: '); |
||
| 515 | $height = ceil($matches[3] / (16/9)); |
||
| 516 | return <<<EOD |
||
| 517 | <figure> |
||
| 518 | <iframe width='{$matches[3]}' height='{$height}' src="http://www.youtube.com/embed/{$matches[2]}" frameborder="0" |
||
| 519 | allowfullscreen></iframe> |
||
| 520 | <figcaption>{$caption}{$matches[4]}</figcaption> |
||
| 521 | </figure> |
||
| 522 | EOD; |
||
| 523 | break; |
||
| 524 | |||
| 525 | case 'syntax=': return CTextFilter::SyntaxHighlightGeSHi($matches[3], $matches[2]); break; |
||
| 526 | case '```': return CTextFilter::SyntaxHighlightGeSHi($matches[3], $matches[2]); break; |
||
| 527 | //case 'syntax=': return "<pre>" . highlight_string($matches[3], true) . "</pre>"; break; |
||
| 528 | //case 'INCL': include($matches[2]); break; |
||
| 529 | case 'INFO': return "<div class='info' markdown=1>"; break; |
||
| 530 | case '/INFO': return "</div>"; break; |
||
| 531 | case 'BASEURL': return CLydia::Instance()->request->base_url; break; |
||
| 532 | case 'FIGURE': return CTextFilter::ShortCodeFigure($matches[2]); break; |
||
| 533 | case 'FIGURE-SVG': return CTextFilter::ShortCodeSVGFigure($matches[2]); break; |
||
| 534 | case 'ASCIINEMA': return CTextFilter::ShortCodeAsciinema($matches[2]); break; |
||
| 535 | default: return "{$matches[1]} IS UNKNOWN SHORTTAG."; break; |
||
| 536 | } |
||
| 537 | }; |
||
| 538 | $patterns = array( |
||
| 539 | '#\[(BASEURL)\]#', |
||
| 540 | //'/\[(AUTHOR) name=(.+) email=(.+) url=(.+)\]/', |
||
| 541 | '/\[(FIGURE)[\s+](.+)\]/', |
||
| 542 | '/\[(FIGURE-SVG)[\s+](.+)\]/', |
||
| 543 | '/\[(ASCIINEMA)[\s+](.+)\]/', |
||
| 544 | '/\[(IMG) src=(.+) alt=(.+)\]/', |
||
| 545 | '/\[(IMG2) src=(.+) alt="(.+)" class="(.+)"\]/', |
||
| 546 | '/\[(BOOK) isbn=(.+)\]/', |
||
| 547 | '/\[(YOUTUBE) src=(.+) width=(.+) caption=(.+)\]/', |
||
| 548 | '/~~~(syntax=)(php|html|html5|css|sql|javascript|bash)\n([^~]+)\n~~~/s', |
||
| 549 | '/(```)(php|html|html5|css|sql|javascript|bash|text|txt|python)\n([^`]+)\n```/s', |
||
| 550 | //'/\[(INCL)/s*([^\]+)/', |
||
| 551 | '#\[(INFO)\]#', '#\[(/INFO)\]#', |
||
| 552 | ); |
||
| 553 | |||
| 554 | $ret = preg_replace_callback($patterns, $callback, $text); |
||
| 555 | return $ret; |
||
| 556 | } |
||
| 557 | */ |
||
| 558 | } |
||
| 559 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.