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 | * Dispatcher default options. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected static $defaults = [ |
||
33 | 'stringHandlerPattern' => '::', |
||
34 | 'autoWire' => true |
||
35 | ]; |
||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $options; |
||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private $providedArguments; |
||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | private $constructParams; |
||
48 | |||
49 | /** |
||
50 | * Handler Class Namespaces. |
||
51 | * |
||
52 | * @var array. |
||
53 | */ |
||
54 | private $namespaces = []; |
||
55 | |||
56 | /** |
||
57 | * Dispatcher constructor. |
||
58 | * |
||
59 | * @param array $options |
||
60 | */ |
||
61 | 2 | public function __construct(array $options = array()) |
|
66 | |||
67 | /** |
||
68 | * Get response from handler string. |
||
69 | * |
||
70 | * @param $request |
||
71 | * @param string|\Closure $handler |
||
72 | * @param array $constructor |
||
73 | * @return ResponseInterface |
||
74 | * @throws \Exception |
||
75 | */ |
||
76 | public function dispatch(ServerRequestInterface $request, $handler, array $constructor = array()) |
||
110 | |||
111 | /*** |
||
112 | * Get Controller/Method from handler string. |
||
113 | * |
||
114 | * @param $handler |
||
115 | * @return mixed |
||
116 | * @throws DispatchException |
||
117 | */ |
||
118 | protected function _processStringHandler($handler) |
||
134 | |||
135 | /** |
||
136 | * Find class from namespace. |
||
137 | * |
||
138 | * @param string $class |
||
139 | * @return string |
||
140 | * @throws DispatchException |
||
141 | */ |
||
142 | protected function _findClass($class) |
||
156 | |||
157 | /** |
||
158 | * Find class construct parameters from provided arguments. |
||
159 | * |
||
160 | * @param string $controllerName |
||
161 | * @param array $args |
||
162 | * @return array |
||
163 | * @throws DispatchException |
||
164 | */ |
||
165 | protected function _getParams(string $controllerName, array $args) |
||
188 | |||
189 | /** |
||
190 | * Reflect Controller construct and get parameters. |
||
191 | * |
||
192 | * @param $controllerName |
||
193 | * @internal |
||
194 | * |
||
195 | * @return \ReflectionParameter[] |
||
196 | */ |
||
197 | protected function _getConstructParams($controllerName) |
||
208 | |||
209 | /** |
||
210 | * Find controller constructor params based on $arg[index] |
||
211 | * |
||
212 | * @param $args |
||
213 | * @throws DispatchException |
||
214 | */ |
||
215 | protected function _getParamsFromVariableName($args) |
||
229 | |||
230 | /** |
||
231 | * Find missing parameters based on Class name or type hint. |
||
232 | * |
||
233 | * @param $args |
||
234 | */ |
||
235 | protected function _getParamsFromTypeHint($args) |
||
262 | |||
263 | /** |
||
264 | * Process provided arguments. |
||
265 | * |
||
266 | * @param array $args |
||
267 | * @return array |
||
268 | */ |
||
269 | protected function _processConstructArgs(array $args = []) |
||
285 | |||
286 | /** |
||
287 | * Call controller with provided request and return Response. |
||
288 | * |
||
289 | * @param ServerRequestInterface $request Server Request |
||
290 | * @param string $controller Controller class. |
||
291 | * @param string $method Controller method. |
||
292 | * |
||
293 | * @return ResponseInterface |
||
294 | * @throws DispatchException |
||
295 | */ |
||
296 | protected function _callController(ServerRequestInterface $request, string $controller, string $method) |
||
319 | |||
320 | /** |
||
321 | * Checks controller instance against whitelist. |
||
322 | * |
||
323 | * @param $controller |
||
324 | * @param $options |
||
325 | * @throws DispatchException |
||
326 | */ |
||
327 | View Code Duplication | protected function _checkWhiteList($controller, $options) |
|
335 | |||
336 | /** |
||
337 | * Internal function for checking controller instance against class list. |
||
338 | * |
||
339 | * @param $controller |
||
340 | * @param array $classList |
||
341 | * @internal |
||
342 | * @return bool |
||
343 | */ |
||
344 | protected function _checkInList($controller, array $classList) |
||
353 | |||
354 | /** |
||
355 | * Checks controller instance against blacklist. |
||
356 | * |
||
357 | * @param $controller |
||
358 | * @param $options |
||
359 | * @throws DispatchException |
||
360 | */ |
||
361 | View Code Duplication | protected function _checkBlackList($controller, $options) |
|
369 | |||
370 | /** |
||
371 | * Create a new PSR-7 Response. |
||
372 | * |
||
373 | * @param $controllerResponse |
||
374 | * @return Response |
||
375 | * @throws \InvalidArgumentException |
||
376 | */ |
||
377 | public function createNewResponse($controllerResponse) |
||
385 | |||
386 | /** |
||
387 | * With Class Namespace. |
||
388 | * |
||
389 | * @param $namespace |
||
390 | * @return Dispatcher |
||
391 | */ |
||
392 | 1 | public function withNamespace($namespace) |
|
398 | |||
399 | /** |
||
400 | * With class Namespaces. |
||
401 | * |
||
402 | * @param array $namespaces |
||
403 | * @return Dispatcher |
||
404 | */ |
||
405 | 1 | public function withNamespaces(array $namespaces) |
|
413 | } |
||
414 |
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.