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 ApiRoute 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 ApiRoute, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class ApiRoute extends ApiRouteSpec implements IRouter |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var callable[] |
||
27 | */ |
||
28 | public $onMatch; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $presenter; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private $actions = [ |
||
39 | 'POST' => FALSE, |
||
40 | 'GET' => FALSE, |
||
41 | 'PUT' => FALSE, |
||
42 | 'DELETE' => FALSE, |
||
43 | 'OPTIONS' => FALSE |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $default_actions = [ |
||
50 | 'POST' => 'create', |
||
51 | 'GET' => 'read', |
||
52 | 'PUT' => 'update', |
||
53 | 'DELETE' => 'delete', |
||
54 | 'OPTIONS' => 'options' |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | private $formats = [ |
||
61 | 'json' => 'application/json', |
||
62 | 'xml' => 'application/xml' |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | private $placeholder_order = []; |
||
69 | |||
70 | |||
71 | /** |
||
72 | * @param mixed $data |
||
73 | * @param string $presenter |
||
74 | * @param array $data |
||
75 | */ |
||
76 | public function __construct($path, $presenter = NULL, array $data = []) |
||
77 | { |
||
78 | /** |
||
79 | * Interface for setting route via annotation or directly |
||
80 | */ |
||
81 | if (!is_array($path)) { |
||
82 | $data['value'] = $path; |
||
83 | $data['presenter'] = $presenter; |
||
84 | |||
85 | if (empty($data['methods'])) { |
||
86 | $this->actions = $this->default_actions; |
||
87 | } else { |
||
88 | View Code Duplication | foreach ($data['methods'] as $method => $action) { |
|
|
|||
89 | if (is_string($method)) { |
||
90 | $this->setAction($action, $method); |
||
91 | } else { |
||
92 | $m = $action; |
||
93 | |||
94 | if (isset($this->default_actions[$m])) { |
||
95 | $this->setAction($this->default_actions[$m], $m); |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | |||
100 | unset($data['methods']); |
||
101 | } |
||
102 | } else { |
||
103 | $data = $path; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Set Path |
||
108 | */ |
||
109 | $this->setPath($data['value']); |
||
110 | unset($data['value']); |
||
111 | |||
112 | parent::__construct($data); |
||
113 | } |
||
114 | |||
115 | |||
116 | /** |
||
117 | * @param string $presenter |
||
118 | * @return void |
||
119 | */ |
||
120 | public function setPresenter($presenter) |
||
124 | |||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | public function getPresenter() |
||
133 | |||
134 | |||
135 | /** |
||
136 | * @param string $action |
||
137 | * @param string $method |
||
138 | * @return void |
||
139 | */ |
||
140 | public function setAction($action, $method = NULL) { |
||
151 | |||
152 | |||
153 | /** |
||
154 | * @param string $string |
||
155 | * @return string |
||
156 | */ |
||
157 | private function prepareForMatch($string) |
||
161 | |||
162 | |||
163 | /** |
||
164 | * Get all parameters from url mask |
||
165 | * @return array |
||
166 | */ |
||
167 | public function getPlacehodlerParameters() |
||
181 | |||
182 | |||
183 | /** |
||
184 | * Get required parameters from url mask |
||
185 | * @return array |
||
186 | */ |
||
187 | public function getRequiredParams() |
||
204 | |||
205 | |||
206 | /** |
||
207 | * @param Nette\Http\IRequest $httpRequest |
||
208 | * @return void |
||
209 | */ |
||
210 | public function resolveFormat(Nette\Http\IRequest $httpRequest) |
||
228 | |||
229 | |||
230 | /** |
||
231 | * @return string |
||
232 | */ |
||
233 | public function getFormatFull() |
||
237 | |||
238 | |||
239 | /** |
||
240 | * @param array $methods |
||
241 | */ |
||
242 | public function setMethods(array $methods) |
||
256 | |||
257 | |||
258 | /** |
||
259 | * @return array |
||
260 | */ |
||
261 | public function getMethods() |
||
265 | |||
266 | |||
267 | /** |
||
268 | * @param Nette\Http\IRequest $request |
||
269 | * @return string |
||
270 | */ |
||
271 | public function resolveMethod(Nette\Http\IRequest $request) { |
||
284 | |||
285 | |||
286 | /******************************************************************************** |
||
287 | * Interface IRouter * |
||
288 | ********************************************************************************/ |
||
289 | |||
290 | |||
291 | /** |
||
292 | * Maps HTTP request to a Request object. |
||
293 | * @return Request|NULL |
||
294 | */ |
||
295 | public function match(Nette\Http\IRequest $httpRequest) |
||
402 | |||
403 | |||
404 | /** |
||
405 | * Constructs absolute URL from Request object. |
||
406 | * @return string|NULL |
||
407 | */ |
||
408 | public function constructUrl(Request $request, Nette\Http\Url $url) |
||
454 | |||
455 | } |
||
456 |
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.