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 |
||
36 | class RoutingValue implements RoutingValueInterface |
||
37 | { |
||
38 | /** |
||
39 | * @var Context |
||
40 | */ |
||
41 | protected $context; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $contextName; |
||
47 | |||
48 | protected $value; |
||
49 | protected $prefix; |
||
50 | protected $postfix; |
||
51 | protected $valueNeedsEncoding = true; |
||
52 | protected $prefixNeedsEncoding = false; |
||
53 | protected $postfixNeedsEncoding = false; |
||
54 | |||
55 | protected static $arrayMap = array( |
||
56 | 'pre' => 'prefix', |
||
57 | 'val' => 'value', |
||
58 | 'post' => 'postfix', |
||
59 | ); |
||
60 | |||
61 | /** |
||
62 | * Constructor. |
||
63 | * |
||
64 | * @param mixed $value The value. |
||
65 | * @param bool $valueNeedsEncoding Whether or not the value needs encoding. |
||
66 | * |
||
67 | * @author Dominik del Bondio <[email protected]> |
||
68 | * @since 1.0.0 |
||
69 | */ |
||
70 | public function __construct($value, $valueNeedsEncoding = true) |
||
75 | |||
76 | /** |
||
77 | * Pre-serialization callback. |
||
78 | * |
||
79 | * Will set the name of the context instead of the instance, which will later |
||
80 | * be restored by __wakeup(). |
||
81 | * |
||
82 | * @author David Zülke <[email protected]> |
||
83 | * @since 1.0.0 |
||
84 | */ |
||
85 | View Code Duplication | public function __sleep() |
|
92 | |||
93 | /** |
||
94 | * Post-unserialization callback. |
||
95 | * |
||
96 | * Will restore the context instance based on their names set by __sleep(). |
||
97 | * |
||
98 | * @author David Zülke <[email protected]> |
||
99 | * @since 1.0.0 |
||
100 | */ |
||
101 | public function __wakeup() |
||
107 | |||
108 | /** |
||
109 | * Initialize the routing value. |
||
110 | * |
||
111 | * @param Context $context The Context. |
||
112 | * @param array $parameters An array of initialization parameters. |
||
113 | * |
||
114 | * @author Dominik del Bondio <[email protected]> |
||
115 | * @since 1.0.0 |
||
116 | */ |
||
117 | public function initialize(Context $context, array $parameters = array()) |
||
121 | |||
122 | /** |
||
123 | * Set the value. |
||
124 | * |
||
125 | * @param mixed $value The value. |
||
126 | * |
||
127 | * @author Dominik del Bondio <[email protected]> |
||
128 | * @author David Zülke <[email protected]> |
||
129 | * @since 1.0.0 |
||
130 | */ |
||
131 | public function setValue($value) |
||
136 | |||
137 | /** |
||
138 | * Retrieve the value. |
||
139 | * |
||
140 | * @return mixed the value |
||
141 | * |
||
142 | * @author Dominik del Bondio <[email protected]> |
||
143 | * @since 1.0.0 |
||
144 | */ |
||
145 | public function getValue() |
||
149 | |||
150 | /** |
||
151 | * Set the prefix. |
||
152 | * |
||
153 | * @param string $value The prefix. |
||
154 | * |
||
155 | * @author Dominik del Bondio <[email protected]> |
||
156 | * @author David Zülke <[email protected]> |
||
157 | * @since 1.0.0 |
||
158 | */ |
||
159 | public function setPrefix($value) |
||
164 | |||
165 | /** |
||
166 | * Retrieve the prefix. |
||
167 | * |
||
168 | * @return string The prefix. |
||
169 | * |
||
170 | * @author Dominik del Bondio <[email protected]> |
||
171 | * @since 1.0.0 |
||
172 | */ |
||
173 | public function getPrefix() |
||
177 | |||
178 | /** |
||
179 | * Check if a prefix is set. |
||
180 | * |
||
181 | * @return bool True, if a prefix is set, false otherwise. |
||
182 | * |
||
183 | * @author Dominik del Bondio <[email protected]> |
||
184 | * @since 1.0.0 |
||
185 | */ |
||
186 | public function hasPrefix() |
||
190 | |||
191 | /** |
||
192 | * Set the postfix. |
||
193 | * |
||
194 | * @param string $value The postfix. |
||
195 | * |
||
196 | * @author Dominik del Bondio <[email protected]> |
||
197 | * @author David Zülke <[email protected]> |
||
198 | * @since 1.0.0 |
||
199 | */ |
||
200 | public function setPostfix($value) |
||
205 | |||
206 | /** |
||
207 | * Retrieve the postfix. |
||
208 | * |
||
209 | * @return string The postfix. |
||
210 | * |
||
211 | * @author Dominik del Bondio <[email protected]> |
||
212 | * @since 1.0.0 |
||
213 | */ |
||
214 | public function getPostfix() |
||
218 | |||
219 | /** |
||
220 | * Check if a postfix is set. |
||
221 | * |
||
222 | * @return bool True, if a postfix is set, false otherwise. |
||
223 | * |
||
224 | * @author Dominik del Bondio <[email protected]> |
||
225 | * @since 1.0.0 |
||
226 | */ |
||
227 | public function hasPostfix() |
||
231 | |||
232 | /** |
||
233 | * Set whether or not the value needs to be encoded. |
||
234 | * |
||
235 | * @param bool $needsEncoding True, if the postfix needs encoding, false otherwise. |
||
236 | * |
||
237 | * @author Dominik del Bondio <[email protected]> |
||
238 | * @since 1.0.0 |
||
239 | */ |
||
240 | public function setValueNeedsEncoding($needsEncoding) |
||
245 | |||
246 | /** |
||
247 | * Retrieve whether or not the value needs to be encoded. |
||
248 | * |
||
249 | * @return bool True, if the value needs encoding, false otherwise. |
||
250 | * |
||
251 | * @author Dominik del Bondio <[email protected]> |
||
252 | * @since 1.0.0 |
||
253 | */ |
||
254 | public function getValueNeedsEncoding() |
||
258 | |||
259 | /** |
||
260 | * Set whether or not the prefix needs to be encoded. |
||
261 | * |
||
262 | * @param bool $needsEncoding True, if the prefix needs encoding, false otherwise. |
||
263 | * |
||
264 | * @author Dominik del Bondio <[email protected]> |
||
265 | * @since 1.0.0 |
||
266 | */ |
||
267 | public function setPrefixNeedsEncoding($needsEncoding) |
||
272 | |||
273 | /** |
||
274 | * Retrieve whether or not the prefix needs to be encoded. |
||
275 | * |
||
276 | * @return bool True, if the prefix needs encoding, false otherwise. |
||
277 | * |
||
278 | * @author Dominik del Bondio <[email protected]> |
||
279 | * @since 1.0.0 |
||
280 | */ |
||
281 | public function getPrefixNeedsEncoding() |
||
285 | |||
286 | /** |
||
287 | * Set whether or not the postfix needs to be encoded. |
||
288 | * |
||
289 | * @param bool $needsEncoding True, if the postfix needs encoding, false otherwise. |
||
290 | * |
||
291 | * @author Dominik del Bondio <[email protected]> |
||
292 | * @since 1.0.0 |
||
293 | */ |
||
294 | public function setPostfixNeedsEncoding($needsEncoding) |
||
299 | |||
300 | /** |
||
301 | * Retrieve whether or not the postfix needs to be encoded. |
||
302 | * |
||
303 | * @return bool True, if the postfix needs encoding, false otherwise. |
||
304 | * |
||
305 | * @author Dominik del Bondio <[email protected]> |
||
306 | * @since 1.0.0 |
||
307 | */ |
||
308 | public function getPostfixNeedsEncoding() |
||
312 | |||
313 | /** |
||
314 | * Check if this routing value is equal to the given parameter. |
||
315 | * |
||
316 | * @param mixed $other The value to compare $this against. |
||
317 | * |
||
318 | * @return bool Whether the value matches $this. |
||
319 | * |
||
320 | * @author Dominik del Bondio <[email protected]> |
||
321 | * @since 1.0.0 |
||
322 | */ |
||
323 | public function equals($other) |
||
333 | |||
334 | /** |
||
335 | * ArrayAccess method for isset(). |
||
336 | * |
||
337 | * @param mixed $offset The offset. |
||
338 | * |
||
339 | * @return bool Whether or not the given offset exists. |
||
340 | * |
||
341 | * @author Dominik del Bondio <[email protected]> |
||
342 | * @since 1.0.0 |
||
343 | */ |
||
344 | public function offsetExists($offset) |
||
348 | |||
349 | /** |
||
350 | * ArrayAccess method for getting a value. |
||
351 | * |
||
352 | * @param mixed $offset The offset. |
||
353 | * |
||
354 | * @return mixed The value, nor null if the value does not exist. |
||
355 | * |
||
356 | * @author Dominik del Bondio <[email protected]> |
||
357 | * @since 1.0.0 |
||
358 | */ |
||
359 | public function offsetGet($offset) |
||
365 | |||
366 | /** |
||
367 | * ArrayAccess method for setting a value. |
||
368 | * |
||
369 | * @param mixed $offset The offset. |
||
370 | * @param mixed $value The value. |
||
371 | * |
||
372 | * @author Dominik del Bondio <[email protected]> |
||
373 | * @since 1.0.0 |
||
374 | */ |
||
375 | public function offsetSet($offset, $value) |
||
381 | |||
382 | /** |
||
383 | * ArrayAccess method for unset(). |
||
384 | * |
||
385 | * @param mixed $offset The offset. |
||
386 | * |
||
387 | * @author Dominik del Bondio <[email protected]> |
||
388 | * @since 1.0.0 |
||
389 | */ |
||
390 | public function offsetUnset($offset) |
||
396 | |||
397 | /** |
||
398 | * Return the encoded value (without pre- or postfix) for BC. |
||
399 | * |
||
400 | * @return string The encoded value. |
||
401 | * |
||
402 | * @author Dominik del Bondio <[email protected]> |
||
403 | * @author David Zülke <[email protected]> |
||
404 | * @since 1.0.0 |
||
405 | */ |
||
406 | public function __toString() |
||
410 | } |
||
411 |
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.