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 Dispatcher 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 Dispatcher, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Dispatcher |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * The current request object |
||
29 | * |
||
30 | * @var Request |
||
31 | */ |
||
32 | protected $request; |
||
33 | |||
34 | /** |
||
35 | * The configuration object |
||
36 | * |
||
37 | * @var Config |
||
38 | */ |
||
39 | protected $config; |
||
40 | |||
41 | /** |
||
42 | * user, api |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $requestType = 'default'; |
||
47 | |||
48 | /** |
||
49 | * Dispatcher constructor. |
||
50 | * |
||
51 | * @param Request $request |
||
52 | * @param Config $config |
||
53 | */ |
||
54 | public function __construct(Request $request, Config $config) |
||
59 | |||
60 | /** |
||
61 | * Bootstrap for every route call |
||
62 | * |
||
63 | * @return Response|JsonResponse|mixed |
||
64 | * @throws MethodNotFoundException |
||
65 | * @throws IncompatibleResponseException |
||
66 | */ |
||
67 | public function dispatch() |
||
126 | |||
127 | /** |
||
128 | * @return bool |
||
129 | */ |
||
130 | private function _setLanguageFromUri() |
||
146 | |||
147 | /** |
||
148 | * @return bool|string |
||
149 | */ |
||
150 | private function _resolveAssetsPath() |
||
174 | |||
175 | /** |
||
176 | * @param $file |
||
177 | * @return string |
||
178 | * @codeCoverageIgnore |
||
179 | */ |
||
180 | public function sendCssFileHeader($file) |
||
186 | |||
187 | /** |
||
188 | * @param $file |
||
189 | * @return string |
||
190 | * @codeCoverageIgnore |
||
191 | */ |
||
192 | public function sendJsFileHeader($file) |
||
198 | |||
199 | /** |
||
200 | * Get data for specific route path |
||
201 | * |
||
202 | * @param string $path |
||
203 | * |
||
204 | * @return array |
||
205 | * @throws MethodNotFoundException |
||
206 | */ |
||
207 | private function _getRoute($path) |
||
227 | |||
228 | /** |
||
229 | * Determines if we have a direct/static route match |
||
230 | * |
||
231 | * @param string $uri The request uri |
||
232 | * @param array $data The result from ClassParser |
||
233 | * |
||
234 | * @return array |
||
235 | * @throws MethodNotFoundException |
||
236 | */ |
||
237 | private function _getDirectMatch($uri, array $data) :array |
||
267 | |||
268 | /** |
||
269 | * Determines if we have a variable route match |
||
270 | * |
||
271 | * @param string $uri |
||
272 | * @param array $data |
||
273 | * |
||
274 | * @return array |
||
275 | */ |
||
276 | private function _getVariableMatch($uri, array $data) :array |
||
313 | |||
314 | |||
315 | /** |
||
316 | * @return string |
||
317 | * @codeCoverageIgnore |
||
318 | */ |
||
319 | private function _getRestfulAction() |
||
345 | |||
346 | } |
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.