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 |
||
26 | class Dispatcher |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * The current request object |
||
31 | * |
||
32 | * @var Request |
||
33 | */ |
||
34 | protected $request; |
||
35 | |||
36 | /** |
||
37 | * The configuration object |
||
38 | * |
||
39 | * @var Config |
||
40 | */ |
||
41 | protected $config; |
||
42 | |||
43 | /** |
||
44 | * user, api |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $requestType = 'default'; |
||
49 | |||
50 | /** |
||
51 | * Dispatcher constructor. |
||
52 | * |
||
53 | * @param Request $request |
||
54 | * @param Config $config |
||
55 | */ |
||
56 | public function __construct(Request $request, Config $config) |
||
61 | |||
62 | /** |
||
63 | * Bootstrap for every route call |
||
64 | * |
||
65 | * @return Response|JsonResponse|mixed |
||
66 | * @throws MethodNotFoundException |
||
67 | * @throws ClassNotFoundException |
||
68 | * @throws DispatchFailureException |
||
69 | * @throws IncompatibleResponseException |
||
70 | */ |
||
71 | public function dispatch() |
||
127 | |||
128 | private function detectLanguageSwitch() |
||
132 | |||
133 | /** |
||
134 | * @param AbstractController $class |
||
135 | * @param string $action |
||
136 | * @return bool|int |
||
137 | */ |
||
138 | private function detectPermissions($class, $action) |
||
163 | |||
164 | /** |
||
165 | * @return bool|string |
||
166 | */ |
||
167 | private function resolveAssetsPath() |
||
191 | |||
192 | /** |
||
193 | * @param $file |
||
194 | * @return string |
||
195 | * @codeCoverageIgnore |
||
196 | */ |
||
197 | public function sendCssFileHeader($file) |
||
203 | |||
204 | /** |
||
205 | * @param $file |
||
206 | * @return string |
||
207 | * @codeCoverageIgnore |
||
208 | */ |
||
209 | public function sendJsFileHeader($file) |
||
215 | |||
216 | /** |
||
217 | * Get data for specific route path |
||
218 | * |
||
219 | * @param string $path |
||
220 | * |
||
221 | * @return array |
||
222 | * @throws MethodNotFoundException |
||
223 | */ |
||
224 | private function getRoute($path) |
||
244 | |||
245 | /** |
||
246 | * Determines if we have a direct/static route match |
||
247 | * |
||
248 | * @param string $uri The request uri |
||
249 | * @param array $data The result from ClassParser |
||
250 | * |
||
251 | * @return array |
||
252 | * @throws MethodNotFoundException |
||
253 | */ |
||
254 | private function getDirectMatch($uri, array $data) :array |
||
280 | |||
281 | /** |
||
282 | * Determines if we have a variable route match |
||
283 | * |
||
284 | * @param string $uri |
||
285 | * @param array $data |
||
286 | * |
||
287 | * @return array |
||
288 | * @throws MethodNotFoundException |
||
289 | */ |
||
290 | private function getVariableMatch($uri, array $data) :array |
||
325 | |||
326 | /** |
||
327 | * @return string |
||
328 | */ |
||
329 | private function getRestfulAction() |
||
355 | |||
356 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.