Complex classes like CTextFilter 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 CTextFilter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class CTextFilter |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Supported filters. |
||
| 13 | */ |
||
| 14 | private $filters = [ |
||
| 15 | "jsonfrontmatter", |
||
| 16 | "yamlfrontmatter", |
||
| 17 | "bbcode", |
||
| 18 | "clickable", |
||
| 19 | "shortcode", |
||
| 20 | "markdown", |
||
| 21 | "nl2br", |
||
| 22 | "purify", |
||
| 23 | "titlefromh1", |
||
| 24 | ]; |
||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | /** |
||
| 29 | * Current document parsed. |
||
| 30 | */ |
||
| 31 | private $current; |
||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * Call each filter. |
||
| 37 | * |
||
| 38 | * @deprecated deprecated since version 1.2 in favour of parse(). |
||
| 39 | * |
||
| 40 | * @param string $text the text to filter. |
||
| 41 | * @param string|array $filters as comma separated list of filter, |
||
| 42 | * or filters sent in as array. |
||
| 43 | * |
||
| 44 | * @return string the formatted text. |
||
| 45 | */ |
||
| 46 | public function doFilter($text, $filters) |
||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * Return an array of all filters supported. |
||
| 82 | * |
||
| 83 | * @return array with strings of filters supported. |
||
| 84 | */ |
||
| 85 | 1 | public function getFilters() |
|
| 89 | |||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * Check if filter is supported. |
||
| 94 | * |
||
| 95 | * @param string $filter to use. |
||
| 96 | * |
||
| 97 | * @throws mos/TextFilter/Exception when filter does not exists. |
||
| 98 | * |
||
| 99 | * @return boolean true if filter exists, false othwerwise. |
||
| 100 | */ |
||
| 101 | 2 | public function hasFilter($filter) |
|
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * Add array items to frontmatter. |
||
| 110 | * |
||
| 111 | * @param array|null $matter key value array with items to add |
||
| 112 | * or null if empty. |
||
| 113 | * |
||
| 114 | * @return $this |
||
| 115 | */ |
||
| 116 | 3 | private function addToFrontmatter($matter) |
|
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * Call a specific filter and store its details. |
||
| 134 | * |
||
| 135 | * @param string $filter to use. |
||
| 136 | * |
||
| 137 | * @throws mos/TextFilter/Exception when filter does not exists. |
||
| 138 | * |
||
| 139 | * @return string the formatted text. |
||
| 140 | */ |
||
| 141 | 4 | private function parseFactory($filter) |
|
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * Call each filter and return array with details of the formatted content. |
||
| 195 | * |
||
| 196 | * @param string $text the text to filter. |
||
| 197 | * @param array $filter array of filters to use. |
||
| 198 | * |
||
| 199 | * @throws mos/TextFilter/Exception when filterd does not exists. |
||
| 200 | * |
||
| 201 | * @return array with the formatted text and additional details. |
||
| 202 | */ |
||
| 203 | 4 | public function parse($text, $filter) |
|
| 215 | |||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * Extract front matter from text. |
||
| 220 | * |
||
| 221 | * @param string $text the text to be parsed. |
||
| 222 | * @param string $startToken the start token. |
||
| 223 | * @param string $stopToken the stop token. |
||
| 224 | * |
||
| 225 | * @return array with the formatted text and the front matter. |
||
| 226 | */ |
||
| 227 | 2 | private function extractFrontMatter($text, $startToken, $stopToken) |
|
| 248 | |||
| 249 | |||
| 250 | |||
| 251 | /** |
||
| 252 | * Extract JSON front matter from text. |
||
| 253 | * |
||
| 254 | * @param string $text the text to be parsed. |
||
| 255 | * |
||
| 256 | * @return array with the formatted text and the front matter. |
||
| 257 | */ |
||
| 258 | 2 | public function jsonFrontMatter($text) |
|
| 275 | |||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * Extract YAML front matter from text. |
||
| 280 | * |
||
| 281 | * @param string $text the text to be parsed. |
||
| 282 | * |
||
| 283 | * @return array with the formatted text and the front matter. |
||
| 284 | */ |
||
| 285 | public function yamlFrontMatter($text) |
||
| 303 | |||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * Get the title from the first H1. |
||
| 308 | * |
||
| 309 | * @param string $text the text to be parsed. |
||
| 310 | * |
||
| 311 | * @return string|null with the title, if its found. |
||
| 312 | */ |
||
| 313 | 1 | public function getTitleFromFirstH1($text) |
|
| 324 | |||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * Helper, BBCode formatting converting to HTML. |
||
| 329 | * |
||
| 330 | * @param string $text The text to be converted. |
||
| 331 | * |
||
| 332 | * @return string the formatted text. |
||
| 333 | * |
||
| 334 | * @link http://dbwebb.se/coachen/reguljara-uttryck-i-php-ger-bbcode-formattering |
||
| 335 | */ |
||
| 336 | 3 | public function bbcode2html($text) |
|
| 358 | |||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * Make clickable links from URLs in text. |
||
| 363 | * |
||
| 364 | * @param string $text the text that should be formatted. |
||
| 365 | * |
||
| 366 | * @return string with formatted anchors. |
||
| 367 | * |
||
| 368 | * @link http://dbwebb.se/coachen/lat-php-funktion-make-clickable-automatiskt-skapa-klickbara-lankar |
||
| 369 | */ |
||
| 370 | 1 | public function makeClickable($text) |
|
| 380 | |||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * Format text according to HTML Purifier. |
||
| 385 | * |
||
| 386 | * @param string $text that should be formatted. |
||
| 387 | * |
||
| 388 | * @return string as the formatted html-text. |
||
| 389 | */ |
||
| 390 | 1 | public function purify($text) |
|
| 400 | |||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * Format text according to Markdown syntax. |
||
| 405 | * |
||
| 406 | * @param string $text the text that should be formatted. |
||
| 407 | * |
||
| 408 | * @return string as the formatted html-text. |
||
| 409 | */ |
||
| 410 | 6 | public function markdown($text) |
|
| 414 | |||
| 415 | |||
| 416 | |||
| 417 | /** |
||
| 418 | * For convenience access to nl2br |
||
| 419 | * |
||
| 420 | * @param string $text text to be converted. |
||
| 421 | * |
||
| 422 | * @return string the formatted text. |
||
| 423 | */ |
||
| 424 | 1 | public function nl2br($text) |
|
| 428 | |||
| 429 | |||
| 430 | |||
| 431 | /** |
||
| 432 | * Shortcode to to quicker format text as HTML. |
||
| 433 | * |
||
| 434 | * @param string $text text to be converted. |
||
| 435 | * |
||
| 436 | * @return string the formatted text. |
||
| 437 | */ |
||
| 438 | 1 | public function shortCode($text) |
|
| 460 | |||
| 461 | |||
| 462 | |||
| 463 | /** |
||
| 464 | * Init shortcode handling by preparing the option list to an array, for those using arguments. |
||
| 465 | * |
||
| 466 | * @param string $options for the shortcode. |
||
| 467 | * |
||
| 468 | * @return array with all the options. |
||
| 469 | */ |
||
| 470 | 1 | public static function shortCodeInit($options) |
|
| 488 | |||
| 489 | |||
| 490 | |||
| 491 | /** |
||
| 492 | * Shortcode for <figure>. |
||
| 493 | * |
||
| 494 | * Usage example: [FIGURE src="img/home/me.jpg" caption="Me" alt="Bild på mig" nolink="nolink"] |
||
| 495 | * |
||
| 496 | * @param string $options for the shortcode. |
||
| 497 | * |
||
| 498 | * @return array with all the options. |
||
| 499 | */ |
||
| 500 | 1 | public static function shortCodeFigure($options) |
|
| 547 | } |
||
| 548 |
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.