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 |
||
| 25 | class Dispatcher |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The current request object |
||
| 30 | * |
||
| 31 | * @var Request |
||
| 32 | */ |
||
| 33 | protected $request; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The configuration object |
||
| 37 | * |
||
| 38 | * @var Config |
||
| 39 | */ |
||
| 40 | protected $config; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * user, api |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $requestType = 'default'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Dispatcher constructor. |
||
| 51 | * |
||
| 52 | * @param Request $request |
||
| 53 | * @param Config $config |
||
| 54 | */ |
||
| 55 | public function __construct(Request $request, Config $config) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Bootstrap for every route call |
||
| 63 | * |
||
| 64 | * @return Response|JsonResponse|mixed |
||
| 65 | * @throws MethodNotFoundException |
||
| 66 | * @throws ClassNotFoundException |
||
| 67 | * @throws DispatchFailureException |
||
| 68 | * @throws IncompatibleResponseException |
||
| 69 | */ |
||
| 70 | public function dispatch() |
||
| 115 | |||
| 116 | private function detectLanguageSwitch() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return bool|string |
||
| 123 | */ |
||
| 124 | private function resolveAssetsPath() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param $file |
||
| 151 | * @return string |
||
| 152 | * @codeCoverageIgnore |
||
| 153 | */ |
||
| 154 | public function sendCssFileHeader($file) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param $file |
||
| 163 | * @return string |
||
| 164 | * @codeCoverageIgnore |
||
| 165 | */ |
||
| 166 | public function sendJsFileHeader($file) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get data for specific route path |
||
| 175 | * |
||
| 176 | * @param string $path |
||
| 177 | * |
||
| 178 | * @return array |
||
| 179 | * @throws MethodNotFoundException |
||
| 180 | */ |
||
| 181 | private function getRoute($path) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Determines if we have a direct/static route match |
||
| 204 | * |
||
| 205 | * @param string $uri The request uri |
||
| 206 | * @param array $data The result from ClassParser |
||
| 207 | * |
||
| 208 | * @return array |
||
| 209 | * @throws MethodNotFoundException |
||
| 210 | */ |
||
| 211 | private function getDirectMatch($uri, array $data) :array |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Determines if we have a variable route match |
||
| 240 | * |
||
| 241 | * @param string $uri |
||
| 242 | * @param array $data |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | * @throws MethodNotFoundException |
||
| 246 | */ |
||
| 247 | private function getVariableMatch($uri, array $data) :array |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | private function getRestfulAction() |
||
| 312 | |||
| 313 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.