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 |
||
7 | class WhitespacePlaceholder implements PlaceholderInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | protected $htmlPlaceholderTags = [ |
||
13 | 'plaintext', |
||
14 | 'textarea', |
||
15 | 'listing', |
||
16 | 'script', |
||
17 | 'style', |
||
18 | 'code', |
||
19 | 'cite', |
||
20 | 'pre', |
||
21 | 'xmp', |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * Replace critical content with a temporary placeholder. |
||
26 | * |
||
27 | * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context |
||
28 | * |
||
29 | * @return \ArjanSchouten\HtmlMinifier\MinifyContext |
||
30 | */ |
||
31 | 5 | public function process($context) |
|
41 | |||
42 | /** |
||
43 | * Whitespaces between inline html elements must be replaced with a placeholder because |
||
44 | * a browser is showing that whitespace. |
||
45 | * |
||
46 | * @param string $contents |
||
47 | * @param \ArjanSchouten\HtmlMinifier\PlaceholderContainer $placeholderContainer |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | 5 | View Code Duplication | protected function whitespaceBetweenInlineElements($contents, PlaceholderContainer $placeholderContainer) |
72 | |||
73 | /** |
||
74 | * Whitespaces in an inline element have a function so we replace it. |
||
75 | * |
||
76 | * @param string $contents |
||
77 | * @param \ArjanSchouten\HtmlMinifier\PlaceholderContainer $placeholderContainer |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | 5 | View Code Duplication | protected function whitespaceInInlineElements($contents, PlaceholderContainer $placeholderContainer) |
99 | |||
100 | /** |
||
101 | * Replace the whitespaces in inline elements with a placeholder. |
||
102 | * |
||
103 | * @param string $element |
||
104 | * @param \ArjanSchouten\HtmlMinifier\PlaceholderContainer $placeholderContainer |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | 1 | private function replaceWhitespacesInInlineElements($element, PlaceholderContainer $placeholderContainer) |
|
114 | |||
115 | /** |
||
116 | * @param string $contents |
||
117 | * @param \ArjanSchouten\HtmlMinifier\PlaceholderContainer $placeholderContainer |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | 5 | protected function replaceElementContents($contents, PlaceholderContainer $placeholderContainer) |
|
146 | |||
147 | /** |
||
148 | * Get the regular expression for matching inline elements. |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | 5 | private function getInlineElementsRegex() |
|
156 | } |
||
157 |
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.