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:
1 | <?php |
||
20 | class ClassAutoBind implements Dispatcher |
||
21 | { |
||
22 | /** |
||
23 | * Not found handler which will be called if nothing has been found. |
||
24 | * |
||
25 | * @var mixed |
||
26 | */ |
||
27 | private $notFoundHandler; |
||
28 | |||
29 | /** |
||
30 | * Base namespace for all dispatched classes. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private $baseNamespace = null; |
||
35 | |||
36 | /** |
||
37 | * Name of class that will be used for constructing a namespace for proper |
||
38 | * class resolve. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $controllerName = 'Controllers'; |
||
43 | |||
44 | /** |
||
45 | * Name of module that will be used for constructing a namespace for proper |
||
46 | * class resolve. |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | private $moduleName = 'Modules'; |
||
51 | |||
52 | /** |
||
53 | * Prefix for action method. |
||
54 | * Target method is allways called with this prefix. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | private $actionName = 'action'; |
||
59 | |||
60 | /** |
||
61 | * Dispatch found route with given parameters |
||
62 | * |
||
63 | * @param \Kambo\Router\Route\Route\Parsed $route Instance of found and parsed route. |
||
64 | * @param array $parameters Additional parameters. |
||
65 | * |
||
66 | * @return mixed |
||
67 | */ |
||
68 | 11 | public function dispatchRoute(Parsed $route, array $parameters = []) |
|
107 | |||
108 | /** |
||
109 | * Called if any of route did not match the request. |
||
110 | * |
||
111 | * @return mixed |
||
112 | */ |
||
113 | 3 | public function dispatchNotFound() |
|
138 | |||
139 | /** |
||
140 | * Set base namespace to allow proper resolve of class name |
||
141 | * |
||
142 | * @param string $baseNamespace base namespace |
||
143 | * |
||
144 | * @return self for fluent interface |
||
145 | */ |
||
146 | 25 | public function setBaseNamespace(string $baseNamespace) |
|
152 | |||
153 | /** |
||
154 | * Sets not found handler |
||
155 | * |
||
156 | * @param mixed $handler handler that will be excuted if nothing has been |
||
157 | * found |
||
158 | * |
||
159 | * @return self for fluent interface |
||
160 | */ |
||
161 | 2 | public function setNotFoundHandler($handler) |
|
167 | |||
168 | // ------------ PRIVATE METHODS |
||
169 | |||
170 | /** |
||
171 | * Resolve target name of class (controller) and method (action) |
||
172 | * |
||
173 | * @param mixed $matches found matched variables |
||
174 | * @param mixed $parameters route parameters |
||
175 | * @param mixed $handler handler that should be executed |
||
176 | * |
||
177 | * @return mixed |
||
178 | */ |
||
179 | 10 | private function resolveControlerAction($matches, $parameters, $handler) |
|
204 | |||
205 | /** |
||
206 | * Transform provided handler with variables and parameters |
||
207 | * |
||
208 | * @param mixed $matches found matched variables |
||
209 | * @param mixed $parameters route parameters |
||
210 | * @param mixed $handler handler that should be executed |
||
211 | * |
||
212 | * @return mixed |
||
213 | */ |
||
214 | 9 | private function transformHandler($matches, $parameters, $handler) |
|
231 | |||
232 | /** |
||
233 | * Resolve proper namespace according parameters, handler and matches |
||
234 | * |
||
235 | * @param mixed $parameters route parameters |
||
236 | * @param mixed $handler handler that should be executed |
||
237 | * @param mixed $matches found matched variables |
||
238 | |||
239 | * @return mixed |
||
240 | */ |
||
241 | 10 | private function resolveNamespace($parameters, $handler, $matches) |
|
270 | |||
271 | /** |
||
272 | * Check if the variable is placeholder |
||
273 | * |
||
274 | * @param string $value found route |
||
275 | * |
||
276 | * @return boolean true if value should be transfered |
||
277 | */ |
||
278 | 10 | private function isPlaceholder(string $value) : bool |
|
286 | |||
287 | /** |
||
288 | * Get function arguments for controler |
||
289 | * |
||
290 | * @param mixed $paramMap parameter map |
||
291 | * @param mixed $matches found matched variables |
||
292 | * @param mixed $parameters route parameters |
||
293 | * @param mixed $handlers handler that should be executed |
||
294 | |||
295 | * @return mixed |
||
296 | */ |
||
297 | 10 | private function getFunctionArgumentsControlers( |
|
332 | |||
333 | /** |
||
334 | * Get names of parameters for provided class and method |
||
335 | * |
||
336 | * @param string $class name of class |
||
337 | * @param string $methodName name of method |
||
338 | * |
||
339 | * @return array |
||
340 | */ |
||
341 | 10 | private function getMethodParameters(string $class, string $methodName) : array |
|
352 | } |
||
353 |