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 CookieJar 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 CookieJar, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class CookieJar implements ArrayAccess, IteratorAggregate, Countable |
||
26 | { |
||
27 | /** |
||
28 | * @var CookieJar parent jar, for inheritance |
||
29 | * @since 2.0.0 |
||
30 | */ |
||
31 | protected $parent; |
||
32 | |||
33 | /** |
||
34 | * @var CookieJar child jars, to maintain a chain |
||
35 | * @since 2.0.0 |
||
36 | */ |
||
37 | protected $children = array(); |
||
38 | |||
39 | /** |
||
40 | * @var bool wether we want to use parent cascading |
||
41 | * @since 2.0.0 |
||
42 | */ |
||
43 | protected $parentEnabled = false; |
||
44 | |||
45 | /** |
||
46 | * @var array the cookie jar |
||
47 | * @since 2.0.0 |
||
48 | */ |
||
49 | protected $jar = array(); |
||
50 | |||
51 | /** |
||
52 | * @var array configuration defaults |
||
53 | */ |
||
54 | protected $config = array( |
||
55 | 'expiration' => 0, // int, Cookie expiration |
||
56 | 'path' => '/', // string, Cookie path |
||
57 | 'domain' => null, // string, Cookie domain |
||
58 | 'secure' => false, // bool, Send only over HTTPS |
||
59 | 'http_only' => false, // only accessible via HTTP client-side |
||
60 | ); |
||
61 | |||
62 | /** |
||
63 | * Constructor |
||
64 | * |
||
65 | * @param array $data container data |
||
66 | * @param boolean $readOnly wether the container is read-only |
||
|
|||
67 | * @since 2.0.0 |
||
68 | */ |
||
69 | public function __construct(Array $config = array(), Array $data = array(), $wrapper = null) |
||
80 | |||
81 | /** |
||
82 | * Get the parent of this jar |
||
83 | * |
||
84 | * @return CookieJar |
||
85 | * @since 2.0.0 |
||
86 | */ |
||
87 | public function getParent() |
||
91 | |||
92 | /** |
||
93 | * Set the parent of this cookie jar, to support inheritance |
||
94 | * |
||
95 | * @param CookieJar $parent the parent cookie jar object |
||
96 | * @return $this |
||
97 | * @since 2.0.0 |
||
98 | */ |
||
99 | public function setParent(CookieJar $parent = null) |
||
115 | |||
116 | /** |
||
117 | * Enable the use of the parent jar, if set |
||
118 | * |
||
119 | * @return $this |
||
120 | * @since 2.0.0 |
||
121 | */ |
||
122 | public function enableParent() |
||
131 | |||
132 | /** |
||
133 | * Disable the use of the parent object |
||
134 | * |
||
135 | * @return $this |
||
136 | * @since 2.0.0 |
||
137 | */ |
||
138 | public function disableParent() |
||
144 | |||
145 | /** |
||
146 | * Check if this jar has a parent |
||
147 | * |
||
148 | * @return bool |
||
149 | * @since 2.0.0 |
||
150 | */ |
||
151 | public function hasParent() |
||
155 | |||
156 | /** |
||
157 | * Check if we have this cookie in the jar |
||
158 | * |
||
159 | * @param string $key |
||
160 | * @return bool |
||
161 | * @since 2.0.0 |
||
162 | */ |
||
163 | public function has($key) |
||
175 | |||
176 | /** |
||
177 | * Remove a cookie from the cookie jar |
||
178 | * |
||
179 | * @param string $key key to delete |
||
180 | * @return boolean delete success boolean |
||
181 | * @since 2.0.0 |
||
182 | */ |
||
183 | View Code Duplication | public function delete($key) |
|
196 | |||
197 | /** |
||
198 | * Get a cookie's value from the cookie jar |
||
199 | * |
||
200 | * @param string $key |
||
201 | * @param mixed $default |
||
202 | * @return mixed |
||
203 | * @since 2.0.0 |
||
204 | */ |
||
205 | View Code Duplication | public function get($key, $default = null) |
|
218 | |||
219 | /** |
||
220 | * Set a cookie to a new value |
||
221 | * |
||
222 | * @param string $key |
||
223 | * @param mixed $value |
||
224 | * @throws \InvalidArgumentException |
||
225 | * @since 2.0.0 |
||
226 | */ |
||
227 | public function set($key, $value = null) |
||
261 | |||
262 | /** |
||
263 | * Send all cookies to the client |
||
264 | * |
||
265 | * @return bool |
||
266 | * @since 2.0.0 |
||
267 | */ |
||
268 | public function send() |
||
292 | |||
293 | /** |
||
294 | * Merge arrays into the container. |
||
295 | * |
||
296 | * @param array $arg array to merge with |
||
297 | * @return $this |
||
298 | * @throws RuntimeException |
||
299 | * @since 2.0.0 |
||
300 | */ |
||
301 | public function merge($arg) |
||
320 | |||
321 | /** |
||
322 | * Allows the ArrayIterator to fetch parent data |
||
323 | * |
||
324 | * @since 2.0.0 |
||
325 | */ |
||
326 | protected function getJar() |
||
337 | |||
338 | /** |
||
339 | * Register a child of this cookie jar, to support inheritance |
||
340 | * |
||
341 | * @param CookieJar $child the child cookie jar object |
||
342 | * |
||
343 | * @since 2.0.0 |
||
344 | */ |
||
345 | public function setChild(CookieJar $child) |
||
352 | |||
353 | /** |
||
354 | * Allow usage of isset() on the cookie jar as an array |
||
355 | * |
||
356 | * @param string $key |
||
357 | * |
||
358 | * @return bool |
||
359 | * |
||
360 | * @since 2.0.0 |
||
361 | */ |
||
362 | public function offsetExists($key) |
||
367 | |||
368 | /** |
||
369 | * Allow fetching values as an array |
||
370 | * |
||
371 | * @param string $key |
||
372 | * @return mixed |
||
373 | * @throws OutOfBoundsException |
||
374 | * @since 2.0.0 |
||
375 | */ |
||
376 | public function offsetGet($key) |
||
391 | |||
392 | /** |
||
393 | * Allow setting values like an array |
||
394 | * |
||
395 | * @param string $key |
||
396 | * @param mixed $value |
||
397 | * @since 2.0.0 |
||
398 | */ |
||
399 | public function offsetSet($key, $value) |
||
406 | |||
407 | /** |
||
408 | * Allow unsetting values like an array |
||
409 | * |
||
410 | * @param string $key |
||
411 | * @since 2.0.0 |
||
412 | */ |
||
413 | public function offsetUnset($key) |
||
424 | |||
425 | /** |
||
426 | * IteratorAggregate implementation |
||
427 | * |
||
428 | * @return IteratorAggregate iterator |
||
429 | * @since 2.0.0 |
||
430 | */ |
||
431 | public function getIterator() |
||
435 | |||
436 | /** |
||
437 | * Countable implementation |
||
438 | * |
||
439 | * @return int number of items stored in the container |
||
440 | * @since 2.0.0 |
||
441 | */ |
||
442 | public function count() |
||
451 | } |
||
452 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.