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 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 | View Code Duplication | public function remove($key, $value = '') |
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | 2 | public function delete($name) |
|
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | 1 | public function without($key) |
|
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | 1 | public function replace($key, $value, $replacement) |
|
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | 1 | public function merge(array $data = array()) |
|
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | 1 | public function exists($key, $value = null) |
|
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | 3 | View Code Duplication | public function contains($key, $value) |
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | 4 | public function __toString() |
|
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | 15 | public function render() |
|
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | 2 | public function toArray() |
|
247 | |||
248 | /** |
||
249 | * {@inheritdoc} |
||
250 | */ |
||
251 | 1 | public function getStorage() |
|
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | 1 | public function getIterator() |
|
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | 1 | public function count() |
|
271 | |||
272 | /** |
||
273 | * Returns all storage elements as an array. |
||
274 | * |
||
275 | * @return \drupol\htmltag\AttributeInterface[] |
||
276 | * An associative array of attributes. |
||
277 | */ |
||
278 | 16 | private function prepareValues() |
|
307 | } |
||
308 |
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.