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 |
||
8 | class Attribute implements AttributeInterface |
||
9 | { |
||
10 | /** |
||
11 | * Store the attribute name. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | private $name; |
||
16 | |||
17 | /** |
||
18 | * Store the attribute value. |
||
19 | * |
||
20 | * @var array|null |
||
21 | */ |
||
22 | private $values; |
||
23 | |||
24 | /** |
||
25 | * Attribute constructor. |
||
26 | * |
||
27 | * @param string $name |
||
28 | * The attribute name. |
||
29 | * @param string $value |
||
30 | * The attribute value. |
||
31 | */ |
||
32 | 29 | public function __construct($name, $value = null) |
|
37 | |||
38 | /** |
||
39 | * {@inheritdoc} |
||
40 | */ |
||
41 | 15 | public function getName() |
|
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | 17 | public function getValueAsString() |
|
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | 21 | public function getValueAsArray() |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 20 | public function render() |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 13 | public function __toString() |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 20 | public function isBoolean() |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 29 | public function set($value = null) |
|
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | 21 | public function append($value) |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | 2 | public function merge(array $values) |
|
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | 3 | public function remove($value) |
|
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | 1 | public function replace($original, $replacement) |
|
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | 4 | public function contains($substring) |
|
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | 1 | public function setBoolean($boolean = true) |
|
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | 1 | public function delete() |
|
201 | |||
202 | /** |
||
203 | * Normalize a value. |
||
204 | * |
||
205 | * @param mixed $values |
||
206 | * The value to normalize. |
||
207 | * |
||
208 | * @return array |
||
209 | * The value normalized. |
||
210 | */ |
||
211 | 29 | private function normalizeValue($values) |
|
226 | |||
227 | /** |
||
228 | * Todo. |
||
229 | * |
||
230 | * @param array $arr |
||
231 | * Todo. |
||
232 | * |
||
233 | * @return mixed[] |
||
234 | * Todo. |
||
235 | */ |
||
236 | 29 | private function ensureFlatArray(array $arr) |
|
248 | |||
249 | /** |
||
250 | * Todo. |
||
251 | * |
||
252 | * @param mixed $value |
||
253 | * Todo. |
||
254 | * |
||
255 | * @return array |
||
256 | * Todo. |
||
257 | */ |
||
258 | 24 | private function ensureArray($value) |
|
286 | } |
||
287 |
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.