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\Attribute[] |
||
14 | */ |
||
15 | private $storage = array(); |
||
16 | |||
17 | /** |
||
18 | * {@inheritdoc} |
||
19 | */ |
||
20 | 24 | public function __construct(array $attributes = array()) |
|
21 | { |
||
22 | 24 | foreach ($attributes as $name => $value) { |
|
23 | 2 | $this->storage[$name] = new Attribute( |
|
24 | 2 | $name, |
|
25 | 2 | $this->ensureString($value) |
|
26 | ); |
||
27 | } |
||
28 | 24 | } |
|
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | 2 | public function offsetGet($name) |
|
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | 1 | public function offsetSet($name, $value = null) |
|
48 | { |
||
49 | 1 | $this->storage[$name] = new Attribute( |
|
50 | 1 | $name, |
|
51 | 1 | $this->ensureString($value) |
|
52 | ); |
||
53 | 1 | } |
|
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | 1 | public function offsetUnset($name) |
|
59 | { |
||
60 | 1 | unset($this->storage[$name]); |
|
61 | 1 | } |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | 2 | public function offsetExists($name) |
|
67 | { |
||
68 | 2 | return isset($this->storage[$name]); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | 14 | View Code Duplication | public function append($key, $value = '') |
|
|||
75 | { |
||
76 | $this->storage += array( |
||
77 | 14 | $key => new Attribute( |
|
78 | 14 | $key, |
|
79 | 14 | $this->ensureString($value) |
|
80 | ) |
||
81 | ); |
||
82 | |||
83 | 14 | foreach ($this->normalizeValue($value) as $value_item) { |
|
84 | 14 | $this->storage[$key]->append($value_item); |
|
85 | } |
||
86 | |||
87 | 14 | return $this; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 2 | public function remove($key, $value = '') |
|
94 | { |
||
95 | 2 | if (!isset($this->storage[$key])) { |
|
96 | 1 | return $this; |
|
97 | } |
||
98 | |||
99 | 1 | foreach ($this->normalizeValue($value) as $value_item) { |
|
100 | 1 | $this->storage[$key]->remove($value_item); |
|
101 | } |
||
102 | |||
103 | 1 | return $this; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | 2 | public function delete($name = array()) |
|
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 1 | public function without($key) |
|
122 | { |
||
123 | 1 | $attributes = clone $this; |
|
124 | |||
125 | 1 | return $attributes->delete($key); |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | 1 | public function replace($key, $value, $replacement) |
|
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | 1 | View Code Duplication | public function merge(array $data = array()) |
153 | { |
||
154 | 1 | foreach ($data as $key => $value) { |
|
155 | $this->storage += array( |
||
156 | 1 | $key => new Attribute( |
|
157 | 1 | $key |
|
158 | ) |
||
159 | ); |
||
160 | |||
161 | 1 | $this->storage[$key]->merge( |
|
162 | 1 | $this->normalizeValue($value) |
|
163 | ); |
||
164 | } |
||
165 | |||
166 | 1 | return $this; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | 1 | public function exists($key, $value = null) |
|
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | 3 | public function contains($key, $value) |
|
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | 7 | public function __toString() |
|
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | 13 | public function render() |
|
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | 2 | public function toArray() |
|
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | 1 | public function getStorage() |
|
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | 1 | public function getIterator() |
|
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | 1 | public function count() |
|
259 | |||
260 | /** |
||
261 | * Returns all storage elements as an array. |
||
262 | * |
||
263 | * @return \drupol\htmltag\Attribute[] |
||
264 | * An associative array of attributes. |
||
265 | */ |
||
266 | 14 | private function prepareValues() |
|
297 | |||
298 | /** |
||
299 | * Normalize a value. |
||
300 | * |
||
301 | * @param mixed $value |
||
302 | * The value to normalize. |
||
303 | * |
||
304 | * @return array |
||
305 | * The value normalized. |
||
306 | */ |
||
307 | 16 | private function normalizeValue($value) |
|
311 | |||
312 | /** |
||
313 | * Todo. |
||
314 | * |
||
315 | * @param mixed $value |
||
316 | * Todo. |
||
317 | * |
||
318 | * @return array |
||
319 | * The array, flattened. |
||
320 | */ |
||
321 | 16 | private function ensureFlatArray($value) |
|
366 | |||
367 | /** |
||
368 | * Todo. |
||
369 | * |
||
370 | * @param mixed $value |
||
371 | * Todo. |
||
372 | * |
||
373 | * @return string |
||
374 | * A string. |
||
375 | */ |
||
376 | 16 | private function ensureString($value) |
|
406 | } |
||
407 |
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.