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 | 10 | "markdown", |
|
| 21 | "nl2br", |
||
| 22 | "purify", |
||
| 23 | ]; |
||
| 24 | 10 | ||
| 25 | 10 | ||
| 26 | 10 | ||
| 27 | 10 | /** |
|
| 28 | 10 | * Current document parsed. |
|
| 29 | 10 | */ |
|
| 30 | private $current; |
||
| 31 | |||
| 32 | 10 | ||
| 33 | 2 | ||
| 34 | 2 | /** |
|
| 35 | 8 | * Call each filter. |
|
| 36 | 8 | * |
|
| 37 | * @deprecated deprecated since version 1.2 in favour of parse(). |
||
| 38 | * |
||
| 39 | * @param string $text the text to filter. |
||
| 40 | 10 | * @param string|array $filters as comma separated list of filter, |
|
| 41 | * or filters sent in as array. |
||
| 42 | 10 | * |
|
| 43 | 1 | * @return string the formatted text. |
|
| 44 | */ |
||
| 45 | 9 | public function doFilter($text, $filters) |
|
| 76 | 3 | ||
| 77 | 3 | ||
| 78 | 3 | ||
| 79 | /** |
||
| 80 | 3 | * Return an array of all filters supported. |
|
| 81 | * |
||
| 82 | 3 | * @return array with strings of filters supported. |
|
| 83 | */ |
||
| 84 | public function getFilters() |
||
| 88 | |||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * Check if filter is supported. |
||
| 93 | * |
||
| 94 | * @param string $filter to use. |
||
| 95 | * |
||
| 96 | 1 | * @throws mos/TextFilter/Exception when filter does not exists. |
|
| 97 | * |
||
| 98 | 1 | * @return boolean true if filter exists, false othwerwise. |
|
| 99 | 1 | */ |
|
| 100 | public function hasFilter($filter) |
||
| 104 | 1 | ||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Call a specific filter and store its details. |
||
| 109 | * |
||
| 110 | * @param string $filter to use. |
||
| 111 | * |
||
| 112 | * @throws mos/TextFilter/Exception when filter does not exists. |
||
| 113 | * |
||
| 114 | * @return string the formatted text. |
||
| 115 | */ |
||
| 116 | private function parseFactory($filter) |
||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * Call each filter and return array with details of the formatted content. |
||
| 164 | 1 | * |
|
| 165 | * @param string $text the text to filter. |
||
| 166 | 1 | * @param array $filter array of filters to use. |
|
| 167 | * |
||
| 168 | * @throws mos/TextFilter/Exception when filterd does not exists. |
||
| 169 | * |
||
| 170 | * @return array with the formatted text and additional details. |
||
| 171 | */ |
||
| 172 | public function parse($text, $filter) |
||
| 184 | 1 | ||
| 185 | 1 | ||
| 186 | |||
| 187 | /** |
||
| 188 | 1 | * Extract front matter from text. |
|
| 189 | 1 | * |
|
| 190 | 1 | * @param string $text the text to be parsed. |
|
| 191 | * @param string $startToken the start token. |
||
| 192 | 1 | * @param string $stopToken the stop token. |
|
| 193 | * |
||
| 194 | 1 | * @return array with the formatted text and the front matter. |
|
| 195 | */ |
||
| 196 | private function extractFrontMatter($text, $startToken, $stopToken) |
||
| 218 | 1 | ||
| 219 | 1 | ||
| 220 | 1 | ||
| 221 | 1 | /** |
|
| 222 | 1 | * Extract JSON front matter from text. |
|
| 223 | 1 | * |
|
| 224 | 1 | * @param string $text the text to be parsed. |
|
| 225 | * |
||
| 226 | 1 | * @return array with the formatted text and the front matter. |
|
| 227 | 1 | */ |
|
| 228 | 1 | public function jsonFrontMatter($text) |
|
| 245 | |||
| 246 | |||
| 247 | 1 | ||
| 248 | 1 | /** |
|
| 249 | 1 | * Extract YAML front matter from text. |
|
| 250 | 1 | * |
|
| 251 | 1 | * @param string $text the text to be parsed. |
|
| 252 | * |
||
| 253 | 1 | * @return array with the formatted text and the front matter. |
|
| 254 | */ |
||
| 255 | public function yamlFrontMatter($text) |
||
| 273 | |||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * Helper, BBCode formatting converting to HTML. |
||
| 278 | * |
||
| 279 | * @param string $text The text to be converted. |
||
| 280 | * |
||
| 281 | * @return string the formatted text. |
||
| 282 | * |
||
| 283 | * @link http://dbwebb.se/coachen/reguljara-uttryck-i-php-ger-bbcode-formattering |
||
| 284 | */ |
||
| 285 | public function bbcode2html($text) |
||
| 307 | |||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * Make clickable links from URLs in text. |
||
| 312 | * |
||
| 313 | * @param string $text the text that should be formatted. |
||
| 314 | * |
||
| 315 | * @return string with formatted anchors. |
||
| 316 | * |
||
| 317 | * @link http://dbwebb.se/coachen/lat-php-funktion-make-clickable-automatiskt-skapa-klickbara-lankar |
||
| 318 | */ |
||
| 319 | public function makeClickable($text) |
||
| 329 | |||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * Format text according to HTML Purifier. |
||
| 334 | * |
||
| 335 | * @param string $text that should be formatted. |
||
| 336 | * |
||
| 337 | * @return string as the formatted html-text. |
||
| 338 | */ |
||
| 339 | public function purify($text) |
||
| 349 | |||
| 350 | |||
| 351 | |||
| 352 | /** |
||
| 353 | * Format text according to Markdown syntax. |
||
| 354 | * |
||
| 355 | * @param string $text the text that should be formatted. |
||
| 356 | * |
||
| 357 | * @return string as the formatted html-text. |
||
| 358 | */ |
||
| 359 | public function markdown($text) |
||
| 363 | |||
| 364 | |||
| 365 | |||
| 366 | /** |
||
| 367 | * For convenience access to nl2br |
||
| 368 | * |
||
| 369 | * @param string $text text to be converted. |
||
| 370 | * |
||
| 371 | * @return string the formatted text. |
||
| 372 | */ |
||
| 373 | public function nl2br($text) |
||
| 377 | |||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * Shortcode to to quicker format text as HTML. |
||
| 382 | * |
||
| 383 | * @param string $text text to be converted. |
||
| 384 | * |
||
| 385 | * @return string the formatted text. |
||
| 386 | */ |
||
| 387 | public function shortCode($text) |
||
| 409 | |||
| 410 | |||
| 411 | |||
| 412 | /** |
||
| 413 | * Init shortcode handling by preparing the option list to an array, for those using arguments. |
||
| 414 | * |
||
| 415 | * @param string $options for the shortcode. |
||
| 416 | * |
||
| 417 | * @return array with all the options. |
||
| 418 | */ |
||
| 419 | public static function shortCodeInit($options) |
||
| 437 | |||
| 438 | |||
| 439 | |||
| 440 | /** |
||
| 441 | * Shortcode for <figure>. |
||
| 442 | * |
||
| 443 | * Usage example: [FIGURE src="img/home/me.jpg" caption="Me" alt="Bild på mig" nolink="nolink"] |
||
| 444 | * |
||
| 445 | * @param string $options for the shortcode. |
||
| 446 | * |
||
| 447 | * @return array with all the options. |
||
| 448 | */ |
||
| 449 | public static function shortCodeFigure($options) |
||
| 496 | } |
||
| 497 |
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.