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:
1 | <?php |
||
13 | trait HtmlTrait |
||
14 | { |
||
15 | /** |
||
16 | * @var array HTML elements considered as inline elements. |
||
17 | * @see http://www.w3.org/wiki/HTML/Elements#Text-level_semantics |
||
18 | */ |
||
19 | protected $inlineHtmlElements = [ |
||
20 | 'a', 'abbr', 'acronym', |
||
21 | 'b', 'basefont', 'bdo', 'big', 'br', 'button', 'blink', |
||
22 | 'cite', 'code', |
||
23 | 'del', 'dfn', |
||
24 | 'em', |
||
25 | 'font', |
||
26 | 'i', 'img', 'ins', 'input', 'iframe', |
||
27 | 'kbd', |
||
28 | 'label', 'listing', |
||
29 | 'map', 'mark', |
||
30 | 'nobr', |
||
31 | 'object', |
||
32 | 'q', |
||
33 | 'rp', 'rt', 'ruby', |
||
34 | 's', 'samp', 'script', 'select', 'small', 'spacer', 'span', 'strong', 'sub', 'sup', |
||
35 | 'tt', 'var', |
||
36 | 'u', |
||
37 | 'wbr', |
||
38 | 'time', |
||
39 | ]; |
||
40 | /** |
||
41 | * @var array HTML elements known to be self-closing. |
||
42 | */ |
||
43 | protected $selfClosingHtmlElements = [ |
||
44 | 'br', 'hr', 'img', 'input', 'nobr', |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * identify a line as the beginning of a HTML block. |
||
49 | */ |
||
50 | 205 | protected function identifyHtml($line, $lines, $current) |
|
75 | |||
76 | /** |
||
77 | * Consume lines for an HTML block |
||
78 | */ |
||
79 | 19 | protected function consumeHtml($lines, $current) |
|
111 | |||
112 | /** |
||
113 | * Renders an HTML block |
||
114 | */ |
||
115 | 19 | protected function renderHtml($block) |
|
119 | |||
120 | /** |
||
121 | * Parses an & or a html entity definition. |
||
122 | * @marker & |
||
123 | */ |
||
124 | 10 | View Code Duplication | protected function parseEntity($text) |
133 | |||
134 | /** |
||
135 | * renders a html entity. |
||
136 | */ |
||
137 | 19 | protected function renderInlineHtml($block) |
|
141 | |||
142 | /** |
||
143 | * Parses inline HTML. |
||
144 | * @marker < |
||
145 | */ |
||
146 | 17 | protected function parseInlineHtml($text) |
|
159 | |||
160 | /** |
||
161 | * Escapes `>` characters. |
||
162 | * @marker > |
||
163 | */ |
||
164 | 7 | protected function parseGt($text) |
|
168 | } |
||
169 |
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.