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 |
||
11 | abstract class MetaBase extends ArrayObject |
||
12 | { |
||
13 | const META_ATTRIBUTE_NAME = 'name'; |
||
14 | const META_NAME_PREFIX = ''; |
||
15 | |||
16 | protected $page; |
||
17 | protected static $characterLimits = array(); |
||
18 | |||
19 | /** |
||
20 | * Constructor. |
||
21 | * |
||
22 | * @param Page $page |
||
23 | */ |
||
24 | public function __construct(Page $page) |
||
25 | { |
||
26 | $this->page = $page; |
||
27 | $this->generateTags(); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Convert all tags to html |
||
32 | * |
||
33 | * @return string |
||
34 | */ |
||
35 | public function __toString() |
||
36 | { |
||
37 | $html = []; |
||
38 | |||
39 | foreach ($this as $tag) { |
||
40 | if (is_array($tag)) { |
||
41 | $html = array_merge($html, $tag); |
||
42 | } else { |
||
43 | $html[] = $tag; |
||
44 | } |
||
45 | } |
||
46 | |||
47 | return implode("\n", $html); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Generate all tags. |
||
52 | * |
||
53 | * @return array |
||
54 | */ |
||
55 | abstract protected function generateTags(); |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | View Code Duplication | public function addMeta($name, $content) |
|
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | View Code Duplication | public function addLink($rel, $href) |
|
100 | |||
101 | /** |
||
102 | * Adds an array of metas. |
||
103 | * |
||
104 | * @param array $metas |
||
105 | */ |
||
106 | protected function addMetas(array $metas) |
||
114 | |||
115 | /** |
||
116 | * Adds an array of links. |
||
117 | * |
||
118 | * @param array $links |
||
119 | */ |
||
120 | protected function addLinks(array $links) |
||
128 | |||
129 | /** |
||
130 | * Escapes the value of an attribute. |
||
131 | * |
||
132 | * @param string $value |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | protected static function escape($value) |
||
140 | |||
141 | /** |
||
142 | * Generates the html code of a link |
||
143 | * |
||
144 | * @param string $rel |
||
145 | * @param string $href |
||
146 | */ |
||
147 | protected static function getHtmlLink($rel, $href) |
||
151 | |||
152 | /** |
||
153 | * Generates the html code of a meta |
||
154 | * |
||
155 | * @param string $name |
||
156 | * @param string $content |
||
157 | */ |
||
158 | protected static function getHtmlMeta($name, $content) |
||
164 | |||
165 | /** |
||
166 | * Filters attribute values to trim by length. |
||
167 | * |
||
168 | * @param string $name |
||
169 | * @param string $content |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | protected static function trim($name, $content) |
||
183 | } |
||
184 |
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.