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 | "geshi", |
||
| 22 | "nl2br", |
||
| 23 | "purify", |
||
| 24 | "titlefromh1", |
||
| 25 | ]; |
||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | /** |
||
| 30 | * Current document parsed. |
||
| 31 | */ |
||
| 32 | private $current; |
||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * Call each filter. |
||
| 38 | * |
||
| 39 | * @deprecated deprecated since version 1.2 in favour of parse(). |
||
| 40 | * |
||
| 41 | * @param string $text the text to filter. |
||
| 42 | * @param string|array $filters as comma separated list of filter, |
||
| 43 | * or filters sent in as array. |
||
| 44 | * |
||
| 45 | * @return string the formatted text. |
||
| 46 | */ |
||
| 47 | public function doFilter($text, $filters) |
||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * Return an array of all filters supported. |
||
| 83 | * |
||
| 84 | * @return array with strings of filters supported. |
||
| 85 | */ |
||
| 86 | 1 | public function getFilters() |
|
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * Check if filter is supported. |
||
| 95 | * |
||
| 96 | * @param string $filter to use. |
||
| 97 | * |
||
| 98 | * @throws mos/TextFilter/Exception when filter does not exists. |
||
| 99 | * |
||
| 100 | * @return boolean true if filter exists, false othwerwise. |
||
| 101 | */ |
||
| 102 | 2 | public function hasFilter($filter) |
|
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * Add array items to frontmatter. |
||
| 111 | * |
||
| 112 | * @param array|null $matter key value array with items to add |
||
| 113 | * or null if empty. |
||
| 114 | * |
||
| 115 | * @return $this |
||
| 116 | */ |
||
| 117 | 3 | private function addToFrontmatter($matter) |
|
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | /** |
||
| 134 | * Call a specific filter and store its details. |
||
| 135 | * |
||
| 136 | * @param string $filter to use. |
||
| 137 | * |
||
| 138 | * @throws mos/TextFilter/Exception when filter does not exists. |
||
| 139 | * |
||
| 140 | * @return string the formatted text. |
||
| 141 | */ |
||
| 142 | 6 | private function parseFactory($filter) |
|
| 195 | |||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * Call each filter and return array with details of the formatted content. |
||
| 200 | * |
||
| 201 | * @param string $text the text to filter. |
||
| 202 | * @param array $filter array of filters to use. |
||
| 203 | * |
||
| 204 | * @throws mos/TextFilter/Exception when filterd does not exists. |
||
| 205 | * |
||
| 206 | * @return array with the formatted text and additional details. |
||
| 207 | */ |
||
| 208 | 6 | public function parse($text, $filter) |
|
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * Extract front matter from text. |
||
| 225 | * |
||
| 226 | * @param string $text the text to be parsed. |
||
| 227 | * @param string $startToken the start token. |
||
| 228 | * @param string $stopToken the stop token. |
||
| 229 | * |
||
| 230 | * @return array with the formatted text and the front matter. |
||
| 231 | */ |
||
| 232 | 3 | private function extractFrontMatter($text, $startToken, $stopToken) |
|
| 259 | |||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * Extract JSON front matter from text. |
||
| 264 | * |
||
| 265 | * @param string $text the text to be parsed. |
||
| 266 | * |
||
| 267 | * @return array with the formatted text and the front matter. |
||
| 268 | */ |
||
| 269 | 3 | public function jsonFrontMatter($text) |
|
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * Extract YAML front matter from text. |
||
| 291 | * |
||
| 292 | * @param string $text the text to be parsed. |
||
| 293 | * |
||
| 294 | * @return array with the formatted text and the front matter. |
||
| 295 | */ |
||
| 296 | public function yamlFrontMatter($text) |
||
| 314 | |||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Get the title from the first H1. |
||
| 319 | * |
||
| 320 | * @param string $text the text to be parsed. |
||
| 321 | * |
||
| 322 | * @return string|null with the title, if its found. |
||
| 323 | */ |
||
| 324 | 1 | public function getTitleFromFirstH1($text) |
|
| 335 | |||
| 336 | |||
| 337 | |||
| 338 | /** |
||
| 339 | * Helper, BBCode formatting converting to HTML. |
||
| 340 | * |
||
| 341 | * @param string $text The text to be converted. |
||
| 342 | * |
||
| 343 | * @return string the formatted text. |
||
| 344 | * |
||
| 345 | * @link http://dbwebb.se/coachen/reguljara-uttryck-i-php-ger-bbcode-formattering |
||
| 346 | */ |
||
| 347 | 3 | public function bbcode2html($text) |
|
| 369 | |||
| 370 | |||
| 371 | |||
| 372 | /** |
||
| 373 | * Make clickable links from URLs in text. |
||
| 374 | * |
||
| 375 | * @param string $text the text that should be formatted. |
||
| 376 | * |
||
| 377 | * @return string with formatted anchors. |
||
| 378 | * |
||
| 379 | * @link http://dbwebb.se/coachen/lat-php-funktion-make-clickable-automatiskt-skapa-klickbara-lankar |
||
| 380 | */ |
||
| 381 | 1 | public function makeClickable($text) |
|
| 391 | |||
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * Syntax highlighter using GeSHi http://qbnz.com/highlighter/. |
||
| 396 | * |
||
| 397 | * @param string $text text to be converted. |
||
| 398 | * @param string $language which language to use for highlighting syntax. |
||
| 399 | * |
||
| 400 | * @return string the formatted text. |
||
| 401 | */ |
||
| 402 | 2 | public function syntaxHighlightGeSHi($text, $language = "text") |
|
| 415 | |||
| 416 | |||
| 417 | |||
| 418 | /** |
||
| 419 | * Format text according to HTML Purifier. |
||
| 420 | * |
||
| 421 | * @param string $text that should be formatted. |
||
| 422 | * |
||
| 423 | * @return string as the formatted html-text. |
||
| 424 | */ |
||
| 425 | 1 | public function purify($text) |
|
| 435 | |||
| 436 | |||
| 437 | |||
| 438 | /** |
||
| 439 | * Format text according to Markdown syntax. |
||
| 440 | * |
||
| 441 | * @param string $text the text that should be formatted. |
||
| 442 | * |
||
| 443 | * @return string as the formatted html-text. |
||
| 444 | */ |
||
| 445 | 7 | public function markdown($text) |
|
| 449 | |||
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * For convenience access to nl2br |
||
| 454 | * |
||
| 455 | * @param string $text text to be converted. |
||
| 456 | * |
||
| 457 | * @return string the formatted text. |
||
| 458 | */ |
||
| 459 | 1 | public function nl2br($text) |
|
| 463 | |||
| 464 | |||
| 465 | |||
| 466 | /** |
||
| 467 | * Shortcode to to quicker format text as HTML. |
||
| 468 | * |
||
| 469 | * @param string $text text to be converted. |
||
| 470 | * |
||
| 471 | * @return string the formatted text. |
||
| 472 | */ |
||
| 473 | 2 | public function shortCode($text) |
|
| 513 | |||
| 514 | |||
| 515 | |||
| 516 | /** |
||
| 517 | * Init shortcode handling by preparing the option list to an array, for those using arguments. |
||
| 518 | * |
||
| 519 | * @param string $options for the shortcode. |
||
| 520 | * |
||
| 521 | * @return array with all the options. |
||
| 522 | */ |
||
| 523 | 1 | public static function shortCodeInit($options) |
|
| 541 | |||
| 542 | |||
| 543 | |||
| 544 | /** |
||
| 545 | * Shortcode for <figure>. |
||
| 546 | * |
||
| 547 | * Usage example: [FIGURE src="img/home/me.jpg" caption="Me" alt="Bild på mig" nolink="nolink"] |
||
| 548 | * |
||
| 549 | * @param string $options for the shortcode. |
||
| 550 | * |
||
| 551 | * @return array with all the options. |
||
| 552 | */ |
||
| 553 | 1 | public static function shortCodeFigure($options) |
|
| 600 | } |
||
| 601 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: