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 Route 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 Route, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Comodojo\Dispatcher\Router; |
||
30 | class Route implements Serializable { |
||
31 | |||
32 | /** |
||
33 | * @const string |
||
34 | */ |
||
35 | const REDIRECT_REFRESH = 'REFRESH'; |
||
36 | |||
37 | /** |
||
38 | * @const string |
||
39 | */ |
||
40 | const REDIRECT_LOCATION = 'LOCATION'; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $type; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $classname; |
||
51 | |||
52 | /** |
||
53 | * @var int |
||
54 | */ |
||
55 | protected $redirect_code; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $redirect_location; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $redirect_message; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $redirect_type = self::REDIRECT_LOCATION; |
||
71 | |||
72 | /** |
||
73 | * @var int |
||
74 | */ |
||
75 | protected $error_code; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $error_message; |
||
81 | |||
82 | /** |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $parameters = []; |
||
86 | |||
87 | /** |
||
88 | * @var array |
||
89 | */ |
||
90 | protected $service = []; |
||
91 | |||
92 | /** |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $request = []; |
||
96 | |||
97 | /** |
||
98 | * @var array |
||
99 | */ |
||
100 | protected $query = []; |
||
101 | |||
102 | 2 | public function getType() { |
|
107 | |||
108 | 2 | public function setType($type) { |
|
115 | |||
116 | 2 | public function getClassName() { |
|
121 | |||
122 | 2 | public function setClassName($class) { |
|
129 | |||
130 | public function getRedirectCode() { |
||
135 | |||
136 | public function setRedirectCode($code) { |
||
147 | |||
148 | public function getRedirectLocation() { |
||
153 | |||
154 | public function setRedirectLocation($location) { |
||
161 | |||
162 | public function getRedirectMessage() { |
||
167 | |||
168 | public function setRedirectMessage($message) { |
||
175 | |||
176 | public function getRedirectType() { |
||
181 | |||
182 | public function setRedirectType($type) { |
||
193 | |||
194 | public function getErrorCode() { |
||
199 | |||
200 | public function setErrorCode($code) { |
||
211 | |||
212 | public function getErrorMessage() { |
||
217 | |||
218 | public function setErrorMessage($message) { |
||
225 | |||
226 | public function getParameter($key) { |
||
233 | |||
234 | public function getParameters() { |
||
239 | |||
240 | public function setParameter($key, $value) { |
||
247 | |||
248 | 1 | public function setParameters($parameters) { |
|
255 | |||
256 | 1 | public function getRequestParameter($key) { |
|
263 | |||
264 | public function getService() { |
||
269 | |||
270 | 1 | public function getServiceName() { |
|
275 | |||
276 | public function setService($service) { |
||
283 | |||
284 | 1 | public function addService($service) { |
|
291 | |||
292 | 1 | public function getRequestParameters() { |
|
297 | |||
298 | 1 | public function setRequestParameter($key, $value) { |
|
305 | |||
306 | public function setRequestParameters($parameters) { |
||
313 | |||
314 | 1 | public function setQuery($key, $regex, $required = false) { |
|
326 | |||
327 | 1 | View Code Duplication | public function isQueryRequired($key) { |
334 | |||
335 | 1 | View Code Duplication | public function getQueryRegex($key) { |
342 | |||
343 | public function getQueries() { |
||
348 | |||
349 | public function setQueries($query) { |
||
356 | |||
357 | 1 | public function path($path) { |
|
386 | |||
387 | /** |
||
388 | * Return the serialized data |
||
389 | * |
||
390 | * @return string |
||
391 | */ |
||
392 | public function serialize() { |
||
404 | |||
405 | /** |
||
406 | * Return the unserialized object |
||
407 | * |
||
408 | * @param string $data Serialized data |
||
409 | * |
||
410 | */ |
||
411 | public function unserialize($data) { |
||
423 | |||
424 | } |
||
425 |
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.