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) |
|
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * Call each filter and return array with details of the formatted content. |
||
| 197 | * |
||
| 198 | * @param string $text the text to filter. |
||
| 199 | * @param array $filter array of filters to use. |
||
| 200 | * |
||
| 201 | * @throws mos/TextFilter/Exception when filterd does not exists. |
||
| 202 | * |
||
| 203 | * @return array with the formatted text and additional details. |
||
| 204 | */ |
||
| 205 | 4 | public function parse($text, $filter) |
|
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * Extract front matter from text. |
||
| 222 | * |
||
| 223 | * @param string $text the text to be parsed. |
||
| 224 | * @param string $startToken the start token. |
||
| 225 | * @param string $stopToken the stop token. |
||
| 226 | * |
||
| 227 | * @return array with the formatted text and the front matter. |
||
| 228 | */ |
||
| 229 | 3 | private function extractFrontMatter($text, $startToken, $stopToken) |
|
| 256 | |||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * Extract JSON front matter from text. |
||
| 261 | * |
||
| 262 | * @param string $text the text to be parsed. |
||
| 263 | * |
||
| 264 | * @return array with the formatted text and the front matter. |
||
| 265 | */ |
||
| 266 | 3 | public function jsonFrontMatter($text) |
|
| 283 | |||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * Extract YAML front matter from text. |
||
| 288 | * |
||
| 289 | * @param string $text the text to be parsed. |
||
| 290 | * |
||
| 291 | * @return array with the formatted text and the front matter. |
||
| 292 | */ |
||
| 293 | public function yamlFrontMatter($text) |
||
| 311 | |||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * Get the title from the first H1. |
||
| 316 | * |
||
| 317 | * @param string $text the text to be parsed. |
||
| 318 | * |
||
| 319 | * @return string|null with the title, if its found. |
||
| 320 | */ |
||
| 321 | 1 | public function getTitleFromFirstH1($text) |
|
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * Helper, BBCode formatting converting to HTML. |
||
| 337 | * |
||
| 338 | * @param string $text The text to be converted. |
||
| 339 | * |
||
| 340 | * @return string the formatted text. |
||
| 341 | * |
||
| 342 | * @link http://dbwebb.se/coachen/reguljara-uttryck-i-php-ger-bbcode-formattering |
||
| 343 | */ |
||
| 344 | 3 | public function bbcode2html($text) |
|
| 366 | |||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * Make clickable links from URLs in text. |
||
| 371 | * |
||
| 372 | * @param string $text the text that should be formatted. |
||
| 373 | * |
||
| 374 | * @return string with formatted anchors. |
||
| 375 | * |
||
| 376 | * @link http://dbwebb.se/coachen/lat-php-funktion-make-clickable-automatiskt-skapa-klickbara-lankar |
||
| 377 | */ |
||
| 378 | 1 | public function makeClickable($text) |
|
| 388 | |||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * Format text according to HTML Purifier. |
||
| 393 | * |
||
| 394 | * @param string $text that should be formatted. |
||
| 395 | * |
||
| 396 | * @return string as the formatted html-text. |
||
| 397 | */ |
||
| 398 | 1 | public function purify($text) |
|
| 408 | |||
| 409 | |||
| 410 | |||
| 411 | /** |
||
| 412 | * Format text according to Markdown syntax. |
||
| 413 | * |
||
| 414 | * @param string $text the text that should be formatted. |
||
| 415 | * |
||
| 416 | * @return string as the formatted html-text. |
||
| 417 | */ |
||
| 418 | 7 | public function markdown($text) |
|
| 422 | |||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * For convenience access to nl2br |
||
| 427 | * |
||
| 428 | * @param string $text text to be converted. |
||
| 429 | * |
||
| 430 | * @return string the formatted text. |
||
| 431 | */ |
||
| 432 | 1 | public function nl2br($text) |
|
| 436 | |||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * Shortcode to to quicker format text as HTML. |
||
| 441 | * |
||
| 442 | * @param string $text text to be converted. |
||
| 443 | * |
||
| 444 | * @return string the formatted text. |
||
| 445 | */ |
||
| 446 | 1 | public function shortCode($text) |
|
| 468 | |||
| 469 | |||
| 470 | |||
| 471 | /** |
||
| 472 | * Init shortcode handling by preparing the option list to an array, for those using arguments. |
||
| 473 | * |
||
| 474 | * @param string $options for the shortcode. |
||
| 475 | * |
||
| 476 | * @return array with all the options. |
||
| 477 | */ |
||
| 478 | 1 | public static function shortCodeInit($options) |
|
| 496 | |||
| 497 | |||
| 498 | |||
| 499 | /** |
||
| 500 | * Shortcode for <figure>. |
||
| 501 | * |
||
| 502 | * Usage example: [FIGURE src="img/home/me.jpg" caption="Me" alt="Bild på mig" nolink="nolink"] |
||
| 503 | * |
||
| 504 | * @param string $options for the shortcode. |
||
| 505 | * |
||
| 506 | * @return array with all the options. |
||
| 507 | */ |
||
| 508 | 1 | public static function shortCodeFigure($options) |
|
| 555 | } |
||
| 556 |
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.