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 | * Dispatcher default options. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected static $defaults = [ |
||
34 | 'stringHandlerPattern' => '::', |
||
35 | 'autoWire' => true |
||
36 | ]; |
||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private $options; |
||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $providedArguments; |
||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | private $constructParams; |
||
49 | |||
50 | /** |
||
51 | * Handler Class Namespaces. |
||
52 | * |
||
53 | * @var array. |
||
54 | */ |
||
55 | private $namespaces = []; |
||
56 | |||
57 | /** |
||
58 | * Dispatcher constructor. |
||
59 | * |
||
60 | * @param array $options |
||
61 | */ |
||
62 | public function __construct(array $options = array()) |
||
67 | |||
68 | /** |
||
69 | * Get response from handler string. |
||
70 | * |
||
71 | * @param $request |
||
72 | * @param string|\Closure $handler |
||
73 | * @param array $constructor |
||
74 | * @return ResponseInterface |
||
75 | * @throws \Exception |
||
76 | */ |
||
77 | public function dispatch(ServerRequestInterface $request, $handler, array $constructor = array()) |
||
103 | |||
104 | /*** |
||
105 | * Get Controller/Method from handler string. |
||
106 | * |
||
107 | * @param $handler |
||
108 | * @return mixed |
||
109 | * @throws DispatchException |
||
110 | */ |
||
111 | protected function _processStringHandler($handler) |
||
127 | |||
128 | /** |
||
129 | * Find class from namespace. |
||
130 | * |
||
131 | * @param string $class |
||
132 | * @return string |
||
133 | * @throws DispatchException |
||
134 | */ |
||
135 | protected function _findClass($class) |
||
149 | |||
150 | /** |
||
151 | * Find class construct parameters from provided arguments. |
||
152 | * |
||
153 | * @param string $controllerName |
||
154 | * @param array $args |
||
155 | * @return array |
||
156 | * @throws DispatchException |
||
157 | */ |
||
158 | protected function _getParams(string $controllerName, array $args) |
||
181 | |||
182 | /** |
||
183 | * Reflect Controller construct and get parameters. |
||
184 | * |
||
185 | * @param $controllerName |
||
186 | * @internal |
||
187 | * |
||
188 | * @return \ReflectionParameter[] |
||
189 | */ |
||
190 | protected function _getConstructParams($controllerName) |
||
201 | |||
202 | /** |
||
203 | * Find controller constructor params based on $arg[index] |
||
204 | * |
||
205 | * @param $args |
||
206 | * @throws DispatchException |
||
207 | */ |
||
208 | protected function _getParamsFromVariableName($args) |
||
222 | |||
223 | /** |
||
224 | * Find missing parameters based on Class name or type hint. |
||
225 | * |
||
226 | * @param $args |
||
227 | */ |
||
228 | protected function _getParamsFromTypeHint($args) |
||
255 | |||
256 | /** |
||
257 | * Process provided arguments. |
||
258 | * |
||
259 | * @param array $args |
||
260 | * @return array |
||
261 | */ |
||
262 | protected function _processConstructArgs(array $args = []) |
||
278 | |||
279 | /** |
||
280 | * Call controller with provided request and return Response. |
||
281 | * |
||
282 | * @param ServerRequestInterface $request Server Request |
||
283 | * @param string $controller Controller class. |
||
284 | * @param string $method Controller method. |
||
285 | * |
||
286 | * @return ResponseInterface |
||
287 | * @throws DispatchException |
||
288 | */ |
||
289 | protected function _callController(ServerRequestInterface $request, string $controller, string $method) |
||
307 | |||
308 | /** |
||
309 | * Checks controller instance against whitelist. |
||
310 | * |
||
311 | * @param $controller |
||
312 | * @param $options |
||
313 | * @throws DispatchException |
||
314 | */ |
||
315 | View Code Duplication | protected function _checkWhiteList($controller, $options) |
|
323 | |||
324 | /** |
||
325 | * Internal function for checking controller instance against class list. |
||
326 | * |
||
327 | * @param $controller |
||
328 | * @param array $classList |
||
329 | * @internal |
||
330 | * @return bool |
||
331 | */ |
||
332 | protected function _checkInList($controller, array $classList) |
||
341 | |||
342 | /** |
||
343 | * Checks controller instance against blacklist. |
||
344 | * |
||
345 | * @param $controller |
||
346 | * @param $options |
||
347 | * @throws DispatchException |
||
348 | */ |
||
349 | View Code Duplication | protected function _checkBlackList($controller, $options) |
|
357 | |||
358 | /** |
||
359 | * Create a new PSR-7 Response. |
||
360 | * |
||
361 | * @param $controllerResponse |
||
362 | * @return Response |
||
363 | * @throws \InvalidArgumentException |
||
364 | */ |
||
365 | public function createNewResponse($controllerResponse) |
||
373 | |||
374 | /** |
||
375 | * With Class Namespace. |
||
376 | * |
||
377 | * @param $namespace |
||
378 | * @return Dispatcher |
||
379 | */ |
||
380 | public function withNamespace($namespace) |
||
386 | |||
387 | /** |
||
388 | * With class Namespaces. |
||
389 | * |
||
390 | * @param array $namespaces |
||
391 | * @return Dispatcher |
||
392 | */ |
||
393 | public function withNamespaces(array $namespaces) |
||
401 | } |
||
402 |