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 | "titlefromheader", |
||
31 | "anchor4Header", |
||
32 | ]; |
||
33 | |||
34 | |||
35 | |||
36 | /** |
||
37 | * Current document parsed. |
||
38 | */ |
||
39 | private $current; |
||
40 | |||
41 | |||
42 | |||
43 | /** |
||
44 | * Hold meta information for filters to use. |
||
45 | */ |
||
46 | private $meta = []; |
||
47 | |||
48 | |||
49 | |||
50 | /** |
||
51 | * Call each filter. |
||
52 | * |
||
53 | * @deprecated deprecated since version 1.2 in favour of parse(). |
||
54 | * |
||
55 | * @param string $text the text to filter. |
||
56 | * @param string|array $filters as comma separated list of filter, |
||
57 | * or filters sent in as array. |
||
58 | * |
||
59 | * @return string the formatted text. |
||
60 | */ |
||
61 | public function doFilter($text, $filters) |
||
91 | |||
92 | |||
93 | |||
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 | |||
108 | /** |
||
109 | * Return an array of all filters supported. |
||
110 | * |
||
111 | * @return array with strings of filters supported. |
||
112 | */ |
||
113 | 1 | public function getFilters() |
|
117 | |||
118 | |||
119 | |||
120 | /** |
||
121 | * Check if filter is supported. |
||
122 | * |
||
123 | * @param string $filter to use. |
||
124 | * |
||
125 | * @throws mos/TextFilter/Exception when filter does not exists. |
||
126 | * |
||
127 | * @return boolean true if filter exists, false othwerwise. |
||
128 | */ |
||
129 | 2 | public function hasFilter($filter) |
|
133 | |||
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 | 3 | private function addToFrontmatter($matter) |
|
157 | |||
158 | |||
159 | |||
160 | /** |
||
161 | * Call a specific filter and store its details. |
||
162 | * |
||
163 | * @param string $filter to use. |
||
164 | * |
||
165 | * @throws mos/TextFilter/Exception when filter does not exists. |
||
166 | * |
||
167 | * @return string the formatted text. |
||
168 | */ |
||
169 | 6 | private function parseFactory($filter) |
|
232 | |||
233 | |||
234 | |||
235 | /** |
||
236 | * Call each filter and return array with details of the formatted content. |
||
237 | * |
||
238 | * @param string $text the text to filter. |
||
239 | * @param array $filter array of filters to use. |
||
240 | * |
||
241 | * @throws mos/TextFilter/Exception when filterd does not exists. |
||
242 | * |
||
243 | * @return array with the formatted text and additional details. |
||
244 | */ |
||
245 | 8 | public function parse($text, $filter) |
|
259 | |||
260 | |||
261 | |||
262 | /** |
||
263 | * Add excerpt as short version of text if available. |
||
264 | * |
||
265 | * @param object &$current same structure as returned by parse(). |
||
266 | * |
||
267 | * @return void. |
||
268 | */ |
||
269 | 2 | public function addExcerpt($current) |
|
275 | |||
276 | |||
277 | |||
278 | /** |
||
279 | * Extract front matter from text. |
||
280 | * |
||
281 | * @param string $text the text to be parsed. |
||
282 | * @param string $startToken the start token. |
||
283 | * @param string $stopToken the stop token. |
||
284 | * |
||
285 | * @return array with the formatted text and the front matter. |
||
286 | */ |
||
287 | 3 | private function extractFrontMatter($text, $startToken, $stopToken) |
|
314 | |||
315 | |||
316 | |||
317 | /** |
||
318 | * Extract JSON front matter from text. |
||
319 | * |
||
320 | * @param string $text the text to be parsed. |
||
321 | * |
||
322 | * @return array with the formatted text and the front matter. |
||
323 | */ |
||
324 | 3 | View Code Duplication | public function jsonFrontMatter($text) |
341 | |||
342 | |||
343 | |||
344 | /** |
||
345 | * Extract YAML front matter from text. |
||
346 | * |
||
347 | * @param string $text the text to be parsed. |
||
348 | * |
||
349 | * @return array with the formatted text and the front matter. |
||
350 | */ |
||
351 | View Code Duplication | public function yamlFrontMatter($text) |
|
368 | |||
369 | |||
370 | |||
371 | /** |
||
372 | * Get the title from the first H1. |
||
373 | * |
||
374 | * @param string $text the text to be parsed. |
||
375 | * |
||
376 | * @return string|null with the title, if its found. |
||
377 | */ |
||
378 | 1 | View Code Duplication | public function getTitleFromFirstH1($text) |
389 | |||
390 | |||
391 | |||
392 | /** |
||
393 | * Get the title from the first header. |
||
394 | * |
||
395 | * @param string $text the text to be parsed. |
||
396 | * |
||
397 | * @return string|null with the title, if its found. |
||
398 | */ |
||
399 | View Code Duplication | public function getTitleFromFirstHeader($text) |
|
410 | |||
411 | |||
412 | |||
413 | /** |
||
414 | * Helper, BBCode formatting converting to HTML. |
||
415 | * |
||
416 | * @param string $text The text to be converted. |
||
417 | * |
||
418 | * @return string the formatted text. |
||
419 | * |
||
420 | * @link http://dbwebb.se/coachen/reguljara-uttryck-i-php-ger-bbcode-formattering |
||
421 | */ |
||
422 | 3 | public function bbcode2html($text) |
|
444 | |||
445 | |||
446 | |||
447 | /** |
||
448 | * Make clickable links from URLs in text. |
||
449 | * |
||
450 | * @param string $text the text that should be formatted. |
||
451 | * |
||
452 | * @return string with formatted anchors. |
||
453 | * |
||
454 | * @link http://dbwebb.se/coachen/lat-php-funktion-make-clickable-automatiskt-skapa-klickbara-lankar |
||
455 | */ |
||
456 | 1 | public function makeClickable($text) |
|
466 | |||
467 | |||
468 | |||
469 | /** |
||
470 | * Syntax highlighter using GeSHi http://qbnz.com/highlighter/. |
||
471 | * |
||
472 | * @param string $text text to be converted. |
||
473 | * @param string $language which language to use for highlighting syntax. |
||
474 | * |
||
475 | * @return string the formatted text. |
||
476 | */ |
||
477 | 2 | public function syntaxHighlightGeSHi($text, $language = "text") |
|
499 | |||
500 | |||
501 | |||
502 | /** |
||
503 | * Format text according to HTML Purifier. |
||
504 | * |
||
505 | * @param string $text that should be formatted. |
||
506 | * |
||
507 | * @return string as the formatted html-text. |
||
508 | */ |
||
509 | 1 | public function purify($text) |
|
519 | |||
520 | |||
521 | |||
522 | /** |
||
523 | * Format text according to Markdown syntax. |
||
524 | * |
||
525 | * @param string $text the text that should be formatted. |
||
526 | * |
||
527 | * @return string as the formatted html-text. |
||
528 | */ |
||
529 | 7 | public function markdown($text) |
|
533 | |||
534 | |||
535 | |||
536 | /** |
||
537 | * For convenience access to nl2br |
||
538 | * |
||
539 | * @param string $text text to be converted. |
||
540 | * |
||
541 | * @return string the formatted text. |
||
542 | */ |
||
543 | 1 | public function nl2br($text) |
|
547 | |||
548 | |||
549 | |||
550 | /** |
||
551 | * Support SmartyPants for better typography. |
||
552 | * |
||
553 | * @param string text text to be converted. |
||
554 | * @return string the formatted text. |
||
555 | */ |
||
556 | /* public static function SmartyPants($text) { |
||
557 | require_once(__DIR__.'/php_smartypants_1.5.1e/smartypants.php'); |
||
558 | return SmartyPants($text); |
||
559 | } |
||
560 | */ |
||
561 | |||
562 | |||
563 | /** |
||
564 | * Support enhanced SmartyPants/Typographer for better typography. |
||
565 | * |
||
566 | * @param string text text to be converted. |
||
567 | * @return string the formatted text. |
||
568 | */ |
||
569 | /* public static function Typographer($text) { |
||
570 | require_once(__DIR__.'/php_smartypants_typographer_1.0/smartypants.php'); |
||
571 | $ret = SmartyPants($text); |
||
572 | return $ret; |
||
573 | } |
||
574 | */ |
||
575 | } |
||
576 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.