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 AttributesInterface |
||
9 | { |
||
10 | /** |
||
11 | * Stores the attribute data. |
||
12 | * |
||
13 | * @var \drupol\htmltag\AttributeInterface[] |
||
14 | */ |
||
15 | private $storage = array(); |
||
16 | |||
17 | /** |
||
18 | * The attribute factory. |
||
19 | * |
||
20 | * @var \drupol\htmltag\AttributeFactoryInterface |
||
21 | */ |
||
22 | private $attributeFactory; |
||
23 | |||
24 | /** |
||
25 | * Attributes constructor. |
||
26 | * |
||
27 | * @param \drupol\htmltag\AttributeFactoryInterface $attributeFactory |
||
28 | * The attribute factory. |
||
29 | * @param mixed[] $attributes |
||
30 | * The input attributes. |
||
31 | */ |
||
32 | 26 | public function __construct(AttributeFactoryInterface $attributeFactory, $attributes = array()) |
|
37 | |||
38 | /** |
||
39 | * {@inheritdoc} |
||
40 | */ |
||
41 | 26 | public function import($attributes = array()) |
|
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 8 | public function set($name, $value = null) |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 2 | public function offsetGet($name) |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 1 | public function offsetSet($name, $value = null) |
|
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 1 | public function offsetUnset($name) |
|
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | 2 | public function offsetExists($name) |
|
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | 14 | public function append($key, $value = null) |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 2 | public function remove($key, $value = '') |
|
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | 2 | public function delete($name) |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 1 | public function without($key) |
|
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | 1 | public function replace($key, $value, $replacement) |
|
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | 1 | public function merge(array $data = array()) |
|
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | 1 | public function exists($key, $value = null) |
|
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | 3 | public function contains($key, $value) |
|
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | 4 | public function __toString() |
|
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | 15 | public function render() |
|
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | 2 | public function toArray() |
|
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | 1 | public function getStorage() |
|
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | */ |
||
265 | 1 | public function getIterator() |
|
269 | |||
270 | /** |
||
271 | * {@inheritdoc} |
||
272 | */ |
||
273 | 1 | public function count() |
|
277 | |||
278 | /** |
||
279 | * Returns all storage elements as an array. |
||
280 | * |
||
281 | * @return \drupol\htmltag\AttributeInterface[] |
||
282 | * An associative array of attributes. |
||
283 | */ |
||
284 | 16 | private function prepareValues() |
|
313 | |||
314 | /** |
||
315 | * Normalize a value. |
||
316 | * |
||
317 | * @param mixed $value |
||
318 | * The value to normalize. |
||
319 | * |
||
320 | * @return array |
||
321 | * The value normalized. |
||
322 | */ |
||
323 | 4 | private function normalizeValue($value) |
|
327 | |||
328 | /** |
||
329 | * Todo. |
||
330 | * |
||
331 | * @param mixed $value |
||
332 | * Todo. |
||
333 | * |
||
334 | * @return array |
||
335 | * The array, flattened. |
||
336 | */ |
||
337 | 4 | View Code Duplication | private function ensureFlatArray($value) |
381 | } |
||
382 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.