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 | "htmlentities", |
||
29 | "purify", |
||
30 | "titlefromh1", |
||
31 | "titlefromheader", |
||
32 | "anchor4Header", |
||
33 | ]; |
||
34 | |||
35 | |||
36 | |||
37 | /** |
||
38 | * Current document parsed. |
||
39 | */ |
||
40 | private $current; |
||
41 | |||
42 | |||
43 | |||
44 | /** |
||
45 | * Hold meta information for filters to use. |
||
46 | */ |
||
47 | private $meta = []; |
||
48 | |||
49 | |||
50 | |||
51 | /** |
||
52 | * Call each filter. |
||
53 | * |
||
54 | * @deprecated deprecated since version 1.2 in favour of parse(). |
||
55 | * |
||
56 | * @param string $text the text to filter. |
||
57 | * @param string|array $filters as comma separated list of filter, |
||
58 | * or filters sent in as array. |
||
59 | * |
||
60 | * @return string the formatted text. |
||
61 | */ |
||
62 | public function doFilter($text, $filters) |
||
92 | |||
93 | |||
94 | |||
95 | /** |
||
96 | * Set meta information that some filters can use. |
||
97 | * |
||
98 | * @param array $meta values for filters to use. |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | public function setMeta($meta) |
||
106 | |||
107 | |||
108 | |||
109 | /** |
||
110 | * Return an array of all filters supported. |
||
111 | * |
||
112 | * @return array with strings of filters supported. |
||
113 | */ |
||
114 | 1 | public function getFilters() |
|
118 | |||
119 | |||
120 | |||
121 | /** |
||
122 | * Check if filter is supported. |
||
123 | * |
||
124 | * @param string $filter to use. |
||
125 | * |
||
126 | * @throws mos/TextFilter/Exception when filter does not exists. |
||
127 | * |
||
128 | * @return boolean true if filter exists, false othwerwise. |
||
129 | */ |
||
130 | 2 | public function hasFilter($filter) |
|
134 | |||
135 | |||
136 | |||
137 | /** |
||
138 | * Add array items to frontmatter. |
||
139 | * |
||
140 | * @param array|null $matter key value array with items to add |
||
141 | * or null if empty. |
||
142 | * |
||
143 | * @return $this |
||
144 | */ |
||
145 | 3 | private function addToFrontmatter($matter) |
|
158 | |||
159 | |||
160 | |||
161 | /** |
||
162 | * Call a specific filter and store its details. |
||
163 | * |
||
164 | * @param string $filter to use. |
||
165 | * |
||
166 | * @throws mos/TextFilter/Exception when filter does not exists. |
||
167 | * |
||
168 | * @return string the formatted text. |
||
169 | */ |
||
170 | 4 | private function parseFactory($filter) |
|
235 | |||
236 | |||
237 | |||
238 | /** |
||
239 | * Call each filter and return array with details of the formatted content. |
||
240 | * |
||
241 | * @param string $text the text to filter. |
||
242 | * @param array $filter array of filters to use. |
||
243 | * |
||
244 | * @throws mos/TextFilter/Exception when filterd does not exists. |
||
245 | * |
||
246 | * @return array with the formatted text and additional details. |
||
247 | */ |
||
248 | 6 | public function parse($text, $filter) |
|
262 | |||
263 | |||
264 | |||
265 | /** |
||
266 | * Add excerpt as short version of text if available. |
||
267 | * |
||
268 | * @param object &$current same structure as returned by parse(). |
||
269 | * |
||
270 | * @return void. |
||
271 | */ |
||
272 | 2 | public function addExcerpt($current) |
|
278 | |||
279 | |||
280 | |||
281 | /** |
||
282 | * Extract front matter from text. |
||
283 | * |
||
284 | * @param string $text the text to be parsed. |
||
285 | * @param string $startToken the start token. |
||
286 | * @param string $stopToken the stop token. |
||
287 | * |
||
288 | * @return array with the formatted text and the front matter. |
||
289 | */ |
||
290 | 3 | private function extractFrontMatter($text, $startToken, $stopToken) |
|
317 | |||
318 | |||
319 | |||
320 | /** |
||
321 | * Extract JSON front matter from text. |
||
322 | * |
||
323 | * @param string $text the text to be parsed. |
||
324 | * |
||
325 | * @return array with the formatted text and the front matter. |
||
326 | */ |
||
327 | 3 | public function jsonFrontMatter($text) |
|
344 | |||
345 | |||
346 | |||
347 | /** |
||
348 | * Extract YAML front matter from text. |
||
349 | * |
||
350 | * @param string $text the text to be parsed. |
||
351 | * |
||
352 | * @return array with the formatted text and the front matter. |
||
353 | */ |
||
354 | public function yamlFrontMatter($text) |
||
367 | |||
368 | |||
369 | |||
370 | /** |
||
371 | * Extract YAML front matter from text, use one of several available |
||
372 | * implementations of a YAML parser. |
||
373 | * |
||
374 | * @param string $text the text to be parsed. |
||
375 | * |
||
376 | * @throws: Exception when parsing frontmatter fails. |
||
377 | * |
||
378 | * @return array with the formatted text and the front matter. |
||
379 | */ |
||
380 | public function yamlParse($text) |
||
407 | |||
408 | |||
409 | |||
410 | /** |
||
411 | * Get the title from the first H1. |
||
412 | * |
||
413 | * @param string $text the text to be parsed. |
||
414 | * |
||
415 | * @return string|null with the title, if its found. |
||
416 | */ |
||
417 | View Code Duplication | public function getTitleFromFirstH1($text) |
|
428 | 3 | ||
429 | 3 | ||
430 | 3 | ||
431 | 3 | /** |
|
432 | 3 | * Get the title from the first header. |
|
433 | * |
||
434 | 3 | * @param string $text the text to be parsed. |
|
435 | * |
||
436 | * @return string|null with the title, if its found. |
||
437 | 3 | */ |
|
438 | 3 | View Code Duplication | public function getTitleFromFirstHeader($text) |
449 | |||
450 | |||
451 | |||
452 | /** |
||
453 | * Helper, BBCode formatting converting to HTML. |
||
454 | * |
||
455 | * @param string $text The text to be converted. |
||
456 | * |
||
457 | * @return string the formatted text. |
||
458 | * |
||
459 | 1 | * @link http://dbwebb.se/coachen/reguljara-uttryck-i-php-ger-bbcode-formattering |
|
460 | */ |
||
461 | 1 | public function bbcode2html($text) |
|
483 | |||
484 | |||
485 | |||
486 | /** |
||
487 | * Make clickable links from URLs in text. |
||
488 | * |
||
489 | * @param string $text the text that should be formatted. |
||
490 | * |
||
491 | * @return string with formatted anchors. |
||
492 | * |
||
493 | * @link http://dbwebb.se/coachen/lat-php-funktion-make-clickable-automatiskt-skapa-klickbara-lankar |
||
494 | */ |
||
495 | public function makeClickable($text) |
||
505 | |||
506 | |||
507 | |||
508 | /** |
||
509 | * Syntax highlighter using GeSHi http://qbnz.com/highlighter/. |
||
510 | * |
||
511 | * @param string $text text to be converted. |
||
512 | * @param string $language which language to use for highlighting syntax. |
||
513 | * |
||
514 | * @return string the formatted text. |
||
515 | */ |
||
516 | /* |
||
517 | public function syntaxHighlightGeSHi($text, $language = "text") |
||
518 | { |
||
519 | $language = $language ?: "text"; |
||
520 | //$language = ($language === 'html') ? 'html4strict' : $language; |
||
521 | $language = ($language === 'html') ? 'html5' : $language; |
||
522 | |||
523 | $geshi = new \GeSHi($text, $language); |
||
524 | $geshi->set_overall_class('geshi'); |
||
525 | $geshi->enable_classes('geshi'); |
||
526 | //$geshi->set_header_type(GESHI_HEADER_PRE_VALID); |
||
527 | //$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); |
||
528 | $code = $geshi->parse_code(); |
||
529 | |||
530 | //echo "<pre>$language\n$code\n", $geshi->get_stylesheet(false) , "</pre>"; exit; |
||
531 | |||
532 | // Replace last </pre>, -strlen(" </pre>") == 12 |
||
533 | $length = strlen(" </pre>"); |
||
534 | if (substr($code, -$length) == " </pre>") { |
||
535 | $code = substr_replace($code, "</pre>", -$length); |
||
536 | } |
||
537 | |||
538 | 1 | return $code; |
|
539 | } |
||
540 | 1 | */ |
|
541 | 1 | ||
542 | |||
543 | |||
544 | 1 | /** |
|
545 | * Syntax highlighter using highlight.php, a port of highlight.js |
||
546 | 1 | * https://packagist.org/packages/scrivo/highlight.php. |
|
547 | * |
||
548 | * @param string $text text to be converted. |
||
549 | * @param string $language which language to use for highlighting syntax. |
||
550 | * |
||
551 | * @return string the formatted text. |
||
552 | */ |
||
553 | public function syntaxHighlightJs($text, $language = "text") |
||
564 | 8 | ||
565 | 8 | ||
566 | |||
567 | /** |
||
568 | * Format text according to HTML Purifier. |
||
569 | * |
||
570 | * @param string $text that should be formatted. |
||
571 | * |
||
572 | * @return string as the formatted html-text. |
||
573 | */ |
||
574 | public function purify($text) |
||
584 | |||
585 | |||
586 | |||
587 | /** |
||
588 | * Format text according to Markdown syntax. |
||
589 | * |
||
590 | * @param string $text the text that should be formatted. |
||
591 | * |
||
592 | * @return string as the formatted html-text. |
||
593 | */ |
||
594 | public function markdown($text) |
||
603 | |||
604 | |||
605 | |||
606 | /** |
||
607 | * For convenience access to nl2br |
||
608 | * |
||
609 | * @param string $text text to be converted. |
||
610 | * |
||
611 | * @return string the formatted text. |
||
612 | */ |
||
613 | public function nl2br($text) |
||
617 | |||
618 | |||
619 | |||
620 | /** |
||
621 | * For convenience access to htmlentities |
||
622 | * |
||
623 | * @param string $text text to be converted. |
||
624 | * |
||
625 | * @return string the formatted text. |
||
626 | */ |
||
627 | public function htmlentities($text) |
||
631 | } |
||
632 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.