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 Attributes 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 Attributes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Attributes implements \ArrayAccess, \IteratorAggregate |
||
9 | { |
||
10 | /** |
||
11 | * Stores the attribute data. |
||
12 | * |
||
13 | * @var \drupol\htmltag\Attribute[] |
||
14 | */ |
||
15 | private $storage = array(); |
||
16 | |||
17 | /** |
||
18 | * {@inheritdoc} |
||
19 | */ |
||
20 | 7 | public function __construct(array $attributes = array()) |
|
24 | |||
25 | /** |
||
26 | * Normalize a value. |
||
27 | * |
||
28 | * @param mixed $value |
||
29 | * The value to normalize. |
||
30 | * |
||
31 | * @return array |
||
32 | * The value normalized. |
||
33 | */ |
||
34 | 3 | private function normalizeValue($value) |
|
38 | |||
39 | /** |
||
40 | * Todo. |
||
41 | * |
||
42 | * @param mixed $value |
||
43 | * Todo. |
||
44 | * |
||
45 | * @return array |
||
46 | * The array, flattened. |
||
47 | */ |
||
48 | 3 | private function ensureFlatArray($value) |
|
85 | |||
86 | /** |
||
87 | * Todo. |
||
88 | * |
||
89 | * @param mixed $value |
||
90 | * Todo. |
||
91 | * |
||
92 | * @return string |
||
93 | * A string. |
||
94 | */ |
||
95 | 3 | private function ensureString($value) |
|
125 | |||
126 | /** |
||
127 | * Set attributes. |
||
128 | * |
||
129 | * @param array|Attributes $attributes |
||
130 | * The attributes. |
||
131 | * |
||
132 | * @return $this |
||
133 | */ |
||
134 | 7 | public function setAttributes($attributes = array()) |
|
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | 1 | public function &offsetGet($name) |
|
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function offsetSet($name, $value = null) |
||
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | public function offsetUnset($name) |
||
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | public function offsetExists($name) |
||
187 | |||
188 | /** |
||
189 | * Sets values for an attribute key. |
||
190 | * |
||
191 | * @param string $attribute |
||
192 | * Name of the attribute. |
||
193 | * @param string|array|bool $value |
||
194 | * Value(s) to set for the given attribute key. |
||
195 | * |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function setAttribute($attribute, $value = false) |
||
204 | |||
205 | /** |
||
206 | * Append a value into an attribute. |
||
207 | * |
||
208 | * @param string $key |
||
209 | * The attribute's name. |
||
210 | * @param string|array|bool $value |
||
211 | * The attribute's value. |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | 3 | View Code Duplication | public function append($key, $value = null) |
232 | |||
233 | /** |
||
234 | * Remove a value from a specific attribute. |
||
235 | * |
||
236 | * @param string $key |
||
237 | * The attribute's name. |
||
238 | * @param string|array|bool $value |
||
239 | * The attribute's value. |
||
240 | * |
||
241 | * @return $this |
||
242 | */ |
||
243 | 2 | public function remove($key, $value = false) |
|
257 | |||
258 | /** |
||
259 | * Delete an attribute. |
||
260 | * |
||
261 | * @param string|array $name |
||
262 | * The name of the attribute key to delete. |
||
263 | * |
||
264 | * @return $this |
||
265 | */ |
||
266 | 1 | public function delete($name = array()) |
|
276 | |||
277 | /** |
||
278 | * Return the attributes. |
||
279 | * |
||
280 | * @param string $key |
||
281 | * The attributes's name. |
||
282 | * @param array|string $value |
||
283 | * The attribute's value. |
||
284 | * |
||
285 | * @return \drupol\htmltag\Attributes |
||
286 | */ |
||
287 | public function without($key, $value) |
||
293 | |||
294 | /** |
||
295 | * Replace a value with another. |
||
296 | * |
||
297 | * @param string $key |
||
298 | * The attributes's name. |
||
299 | * @param string $value |
||
300 | * The attribute's value. |
||
301 | * @param array|string $replacement |
||
302 | * The replacement value. |
||
303 | * |
||
304 | * @return $this |
||
305 | */ |
||
306 | View Code Duplication | public function replace($key, $value, $replacement) |
|
323 | |||
324 | /** |
||
325 | * Merge attributes. |
||
326 | * |
||
327 | * @param array $data |
||
328 | * The data to merge. |
||
329 | * |
||
330 | * @return $this |
||
331 | */ |
||
332 | View Code Duplication | public function merge(array $data = array()) |
|
349 | |||
350 | /** |
||
351 | * Check if an attribute exists and if a value if provided check it as well. |
||
352 | * |
||
353 | * @param string $key |
||
354 | * Attribute name. |
||
355 | * @param string $value |
||
356 | * Todo. |
||
357 | * |
||
358 | * @return bool |
||
359 | * Whereas an attribute exists. |
||
360 | */ |
||
361 | public function exists($key, $value = null) |
||
375 | |||
376 | /** |
||
377 | * Check if attribute contains a value. |
||
378 | * |
||
379 | * @param string $key |
||
380 | * Attribute name. |
||
381 | * @param string $value |
||
382 | * Attribute value. |
||
383 | * |
||
384 | * @return bool |
||
385 | * Whereas an attribute contains a value. |
||
386 | */ |
||
387 | public function contains($key, $value) |
||
395 | |||
396 | /** |
||
397 | * {@inheritdoc} |
||
398 | */ |
||
399 | 4 | public function __toString() |
|
412 | |||
413 | /** |
||
414 | * Returns all storage elements as an array. |
||
415 | * |
||
416 | * @return \drupol\htmltag\Attribute[] |
||
417 | * An associative array of attributes. |
||
418 | */ |
||
419 | 4 | private function prepareValues() |
|
458 | |||
459 | /** |
||
460 | * Returns all storage elements as an array. |
||
461 | * |
||
462 | * @return array |
||
463 | * An associative array of attributes. |
||
464 | */ |
||
465 | public function toArray() |
||
482 | |||
483 | /** |
||
484 | * Get storage. |
||
485 | * |
||
486 | * @return \drupol\htmltag\Attribute[] |
||
487 | * Todo. |
||
488 | */ |
||
489 | public function getStorage() |
||
493 | |||
494 | /** |
||
495 | * {@inheritdoc} |
||
496 | */ |
||
497 | public function getIterator() |
||
501 | } |
||
502 |
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.