Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | use TTextUtilities, |
||
12 | TShortcode; |
||
13 | |||
14 | |||
15 | |||
16 | /** |
||
17 | * Supported filters. |
||
18 | */ |
||
19 | private $filters = [ |
||
20 | "jsonfrontmatter", |
||
21 | "yamlfrontmatter", |
||
22 | "bbcode", |
||
23 | "clickable", |
||
24 | "shortcode", |
||
25 | "markdown", |
||
26 | "geshi", |
||
27 | "nl2br", |
||
28 | "purify", |
||
29 | "titlefromh1", |
||
30 | "anchor4Header", |
||
31 | ]; |
||
32 | |||
33 | |||
34 | |||
35 | /** |
||
36 | * Current document parsed. |
||
37 | */ |
||
38 | private $current; |
||
39 | |||
40 | |||
41 | |||
42 | /** |
||
43 | * Hold meta information for filters to use. |
||
44 | */ |
||
45 | private $meta = []; |
||
46 | |||
47 | |||
48 | |||
49 | /** |
||
50 | * Call each filter. |
||
51 | * |
||
52 | * @deprecated deprecated since version 1.2 in favour of parse(). |
||
53 | * |
||
54 | * @param string $text the text to filter. |
||
55 | * @param string|array $filters as comma separated list of filter, |
||
56 | * or filters sent in as array. |
||
57 | * |
||
58 | * @return string the formatted text. |
||
59 | */ |
||
60 | public function doFilter($text, $filters) |
||
91 | 1 | ||
92 | |||
93 | 1 | ||
94 | /** |
||
95 | * Set meta information that some filters can use. |
||
96 | * |
||
97 | * @param array $meta values for filters to use. |
||
98 | * |
||
99 | * @return void |
||
100 | */ |
||
101 | public function setMeta($meta) |
||
105 | |||
106 | |||
107 | 2 | ||
108 | /** |
||
109 | 2 | * Return an array of all filters supported. |
|
110 | * |
||
111 | * @return array with strings of filters supported. |
||
112 | */ |
||
113 | public function getFilters() |
||
117 | |||
118 | |||
119 | |||
120 | /** |
||
121 | * Check if filter is supported. |
||
122 | 3 | * |
|
123 | * @param string $filter to use. |
||
124 | 3 | * |
|
125 | 2 | * @throws mos/TextFilter/Exception when filter does not exists. |
|
126 | * |
||
127 | * @return boolean true if filter exists, false othwerwise. |
||
128 | 2 | */ |
|
129 | 2 | public function hasFilter($filter) |
|
133 | 2 | ||
134 | |||
135 | |||
136 | /** |
||
137 | * Add array items to frontmatter. |
||
138 | * |
||
139 | * @param array|null $matter key value array with items to add |
||
140 | * or null if empty. |
||
141 | * |
||
142 | * @return $this |
||
143 | */ |
||
144 | private function addToFrontmatter($matter) |
||
157 | 6 | ||
158 | 6 | ||
159 | 6 | ||
160 | /** |
||
161 | * Call a specific filter and store its details. |
||
162 | 6 | * |
|
163 | * @param string $filter to use. |
||
164 | 6 | * |
|
165 | 3 | * @throws mos/TextFilter/Exception when filter does not exists. |
|
166 | 3 | * |
|
167 | 3 | * @return string the formatted text. |
|
168 | 3 | */ |
|
169 | private function parseFactory($filter) |
||
224 | |||
225 | 8 | ||
226 | 8 | ||
227 | 8 | /** |
|
228 | 8 | * Call each filter and return array with details of the formatted content. |
|
229 | * |
||
230 | 8 | * @param string $text the text to filter. |
|
231 | * @param array $filter array of filters to use. |
||
232 | * |
||
233 | * @throws mos/TextFilter/Exception when filterd does not exists. |
||
234 | * |
||
235 | * @return array with the formatted text and additional details. |
||
236 | */ |
||
237 | public function parse($text, $filter) |
||
251 | |||
252 | |||
253 | |||
254 | /** |
||
255 | * Add excerpt as short version of text if available. |
||
256 | 3 | * |
|
257 | 3 | * @param object &$current same structure as returned by parse(). |
|
258 | 3 | * |
|
259 | * @return void. |
||
|
|||
260 | 3 | */ |
|
261 | 2 | public function addExcerpt($current) |
|
267 | 3 | ||
268 | |||
269 | 3 | ||
270 | /** |
||
271 | * Extract front matter from text. |
||
272 | * |
||
273 | * @param string $text the text to be parsed. |
||
274 | * @param string $startToken the start token. |
||
275 | * @param string $stopToken the stop token. |
||
276 | * |
||
277 | * @return array with the formatted text and the front matter. |
||
278 | */ |
||
279 | private function extractFrontMatter($text, $startToken, $stopToken) |
||
306 | |||
307 | |||
308 | |||
309 | /** |
||
310 | * Extract JSON front matter from text. |
||
311 | * |
||
312 | * @param string $text the text to be parsed. |
||
313 | * |
||
314 | * @return array with the formatted text and the front matter. |
||
315 | */ |
||
316 | View Code Duplication | public function jsonFrontMatter($text) |
|
333 | |||
334 | |||
335 | |||
336 | 1 | /** |
|
337 | * Extract YAML front matter from text. |
||
338 | 1 | * |
|
339 | 1 | * @param string $text the text to be parsed. |
|
340 | * |
||
341 | 1 | * @return array with the formatted text and the front matter. |
|
342 | 1 | */ |
|
343 | 1 | View Code Duplication | public function yamlFrontMatter($text) |
360 | |||
361 | |||
362 | 3 | ||
363 | 3 | /** |
|
364 | 3 | * Get the title from the first H1. |
|
365 | 3 | * |
|
366 | 3 | * @param string $text the text to be parsed. |
|
367 | * |
||
368 | 3 | * @return string|null with the title, if its found. |
|
369 | */ |
||
370 | public function getTitleFromFirstH1($text) |
||
381 | |||
382 | |||
383 | |||
384 | /** |
||
385 | * Helper, BBCode formatting converting to HTML. |
||
386 | * |
||
387 | * @param string $text The text to be converted. |
||
388 | * |
||
389 | * @return string the formatted text. |
||
390 | * |
||
391 | * @link http://dbwebb.se/coachen/reguljara-uttryck-i-php-ger-bbcode-formattering |
||
392 | */ |
||
393 | 1 | public function bbcode2html($text) |
|
415 | |||
416 | 2 | ||
417 | 2 | ||
418 | 2 | /** |
|
419 | 2 | * Make clickable links from URLs in text. |
|
420 | 2 | * |
|
421 | * @param string $text the text that should be formatted. |
||
422 | * |
||
423 | * @return string with formatted anchors. |
||
424 | * |
||
425 | 2 | * @link http://dbwebb.se/coachen/lat-php-funktion-make-clickable-automatiskt-skapa-klickbara-lankar |
|
426 | */ |
||
427 | public function makeClickable($text) |
||
437 | 1 | ||
438 | |||
439 | 1 | ||
440 | 1 | /** |
|
441 | * Syntax highlighter using GeSHi http://qbnz.com/highlighter/. |
||
442 | * |
||
443 | 1 | * @param string $text text to be converted. |
|
444 | * @param string $language which language to use for highlighting syntax. |
||
445 | 1 | * |
|
446 | * @return string the formatted text. |
||
447 | */ |
||
448 | public function syntaxHighlightGeSHi($text, $language = "text") |
||
465 | |||
466 | |||
467 | |||
468 | /** |
||
469 | * Format text according to HTML Purifier. |
||
470 | * |
||
471 | 1 | * @param string $text that should be formatted. |
|
472 | * |
||
473 | 1 | * @return string as the formatted html-text. |
|
474 | */ |
||
475 | public function purify($text) |
||
485 | 2 | ||
486 | |||
487 | |||
488 | /** |
||
489 | * Format text according to Markdown syntax. |
||
490 | * |
||
491 | * @param string $text the text that should be formatted. |
||
492 | * |
||
493 | * @return string as the formatted html-text. |
||
494 | */ |
||
495 | public function markdown($text) |
||
499 | |||
500 | |||
501 | 2 | ||
502 | 2 | /** |
|
503 | 2 | * For convenience access to nl2br |
|
504 | * |
||
505 | 2 | * @param string $text text to be converted. |
|
506 | 2 | * |
|
507 | 2 | * @return string the formatted text. |
|
508 | 2 | */ |
|
509 | public function nl2br($text) |
||
513 | |||
514 | 1 | ||
515 | 1 | ||
516 | /** |
||
517 | * Support SmartyPants for better typography. |
||
518 | * |
||
519 | * @param string text text to be converted. |
||
520 | * @return string the formatted text. |
||
521 | 2 | */ |
|
522 | /* public static function SmartyPants($text) { |
||
523 | 2 | require_once(__DIR__.'/php_smartypants_1.5.1e/smartypants.php'); |
|
524 | return SmartyPants($text); |
||
525 | } |
||
526 | */ |
||
527 | |||
528 | |||
529 | /** |
||
530 | * Support enhanced SmartyPants/Typographer for better typography. |
||
531 | * |
||
532 | * @param string text text to be converted. |
||
533 | * @return string the formatted text. |
||
534 | */ |
||
535 | 1 | /* public static function Typographer($text) { |
|
536 | require_once(__DIR__.'/php_smartypants_typographer_1.0/smartypants.php'); |
||
537 | 1 | $ret = SmartyPants($text); |
|
538 | return $ret; |
||
539 | 1 | } |
|
540 | 1 | */ |
|
541 | } |
||
542 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.