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 | 2 | 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()) |
||
111 | |||
112 | /*** |
||
113 | * Get Controller/Method from handler string. |
||
114 | * |
||
115 | * @param $handler |
||
116 | * @return mixed |
||
117 | * @throws DispatchException |
||
118 | */ |
||
119 | protected function _processStringHandler($handler) |
||
135 | |||
136 | /** |
||
137 | * Find class from namespace. |
||
138 | * |
||
139 | * @param string $class |
||
140 | * @return string |
||
141 | * @throws DispatchException |
||
142 | */ |
||
143 | protected function _findClass($class) |
||
157 | |||
158 | /** |
||
159 | * Find class construct parameters from provided arguments. |
||
160 | * |
||
161 | * @param string $controllerName |
||
162 | * @param array $args |
||
163 | * @return array |
||
164 | * @throws DispatchException |
||
165 | */ |
||
166 | protected function _getParams(string $controllerName, array $args) |
||
189 | |||
190 | /** |
||
191 | * Reflect Controller construct and get parameters. |
||
192 | * |
||
193 | * @param $controllerName |
||
194 | * @internal |
||
195 | * |
||
196 | * @return \ReflectionParameter[] |
||
197 | */ |
||
198 | protected function _getConstructParams($controllerName) |
||
209 | |||
210 | /** |
||
211 | * Find controller constructor params based on $arg[index] |
||
212 | * |
||
213 | * @param $args |
||
214 | * @throws DispatchException |
||
215 | */ |
||
216 | protected function _getParamsFromVariableName($args) |
||
230 | |||
231 | /** |
||
232 | * Find missing parameters based on Class name or type hint. |
||
233 | * |
||
234 | * @param $args |
||
235 | */ |
||
236 | protected function _getParamsFromTypeHint($args) |
||
263 | |||
264 | /** |
||
265 | * Process provided arguments. |
||
266 | * |
||
267 | * @param array $args |
||
268 | * @return array |
||
269 | */ |
||
270 | protected function _processConstructArgs(array $args = []) |
||
286 | |||
287 | /** |
||
288 | * Call controller with provided request and return Response. |
||
289 | * |
||
290 | * @param ServerRequestInterface $request Server Request |
||
291 | * @param string $controller Controller class. |
||
292 | * @param string $method Controller method. |
||
293 | * |
||
294 | * @return ResponseInterface |
||
295 | * @throws DispatchException |
||
296 | */ |
||
297 | protected function _callController(ServerRequestInterface $request, string $controller, string $method) |
||
320 | |||
321 | /** |
||
322 | * Checks controller instance against whitelist. |
||
323 | * |
||
324 | * @param $controller |
||
325 | * @param $options |
||
326 | * @throws DispatchException |
||
327 | */ |
||
328 | View Code Duplication | protected function _checkWhiteList($controller, $options) |
|
336 | |||
337 | /** |
||
338 | * Internal function for checking controller instance against class list. |
||
339 | * |
||
340 | * @param $controller |
||
341 | * @param array $classList |
||
342 | * @internal |
||
343 | * @return bool |
||
344 | */ |
||
345 | protected function _checkInList($controller, array $classList) |
||
354 | |||
355 | /** |
||
356 | * Checks controller instance against blacklist. |
||
357 | * |
||
358 | * @param $controller |
||
359 | * @param $options |
||
360 | * @throws DispatchException |
||
361 | */ |
||
362 | View Code Duplication | protected function _checkBlackList($controller, $options) |
|
370 | |||
371 | /** |
||
372 | * Create a new PSR-7 Response. |
||
373 | * |
||
374 | * @param $controllerResponse |
||
375 | * @return Response |
||
376 | * @throws \InvalidArgumentException |
||
377 | */ |
||
378 | public function createNewResponse($controllerResponse) |
||
386 | |||
387 | /** |
||
388 | * With Class Namespace. |
||
389 | * |
||
390 | * @param $namespace |
||
391 | * @return Dispatcher |
||
392 | */ |
||
393 | 1 | public function withNamespace($namespace) |
|
399 | 1 | ||
400 | /** |
||
401 | * With class Namespaces. |
||
402 | * |
||
403 | * @param array $namespaces |
||
404 | * @return Dispatcher |
||
405 | */ |
||
406 | public function withNamespaces(array $namespaces) |
||
414 | } |
||
415 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.