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 |
||
| 14 | class DispatcherClass implements DispatcherInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Not found handler which will be called if nothing has been found. |
||
| 18 | * |
||
| 19 | * @var mixed |
||
| 20 | */ |
||
| 21 | private $notFoundHandler; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Base namespace for all dispatched classes. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $baseNamespace = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Name of class that will be used for constructing a namespace for proper |
||
| 32 | * class resolve. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $controllerName = 'Controllers'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Name of module that will be used for constructing a namespace for proper |
||
| 40 | * class resolve. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $moduleName = 'Modules'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Prefix for action method. |
||
| 48 | * Target method is allways called with this prefix. |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $actionName = 'action'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Dispatch found route with given parameters |
||
| 56 | * |
||
| 57 | * @param Route $route found route |
||
| 58 | * @param mixed $parameters parameters for route |
||
| 59 | * |
||
| 60 | * @return mixed |
||
| 61 | */ |
||
| 62 | public function dispatchRoute(Route $route, array $parameters) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Called if nothing has been not found |
||
| 104 | * |
||
| 105 | * @return mixed |
||
| 106 | */ |
||
| 107 | public function dispatchNotFound() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Set base namespace to allow proper resolve of class name |
||
| 135 | * |
||
| 136 | * @param string $baseNamespace base namespace |
||
| 137 | * |
||
| 138 | * @return self for fluent interface |
||
| 139 | */ |
||
| 140 | public function setBaseNamespace($baseNamespace) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Set not found handler |
||
| 149 | * |
||
| 150 | * @param string $handler handler that will be excuted if nothing has been |
||
| 151 | * found |
||
| 152 | * |
||
| 153 | * @return self for fluent interface |
||
| 154 | */ |
||
| 155 | public function setNotFoundHandler($handler) |
||
| 161 | |||
| 162 | // ------------ PRIVATE METHODS |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Resolve target name of class (controller) and method (action) |
||
| 166 | * |
||
| 167 | * @param mixed $matches found matched variables |
||
| 168 | * @param mixed $parameters route parameters |
||
| 169 | * @param mixed $handler handler that should be executed |
||
| 170 | * |
||
| 171 | * @return mixed |
||
| 172 | */ |
||
| 173 | private function resolveControlerAction($matches, $parameters, $handler) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Transform provided handler with variables and parameters |
||
| 201 | * |
||
| 202 | * @param mixed $matches found matched variables |
||
| 203 | * @param mixed $parameters route parameters |
||
| 204 | * @param mixed $handler handler that should be executed |
||
| 205 | * |
||
| 206 | * @return mixed |
||
| 207 | */ |
||
| 208 | private function transformHandler($matches, $parameters, $handler) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Resolve proper namespace according parameters, handler and matches |
||
| 228 | * |
||
| 229 | * @param mixed $parameters route parameters |
||
| 230 | * @param mixed $handler handler that should be executed |
||
| 231 | * @param mixed $matches found matched variables |
||
| 232 | |||
| 233 | * @return mixed |
||
| 234 | */ |
||
| 235 | private function resolveNamespace($parameters, $handler, $matches) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Check if the variable is placeholder |
||
| 267 | * |
||
| 268 | * @param string $value found route |
||
| 269 | * |
||
| 270 | * @return boolean true if value should be transfered |
||
| 271 | */ |
||
| 272 | private function isPlaceholder($value) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get function arguments for controler |
||
| 283 | * |
||
| 284 | * @param mixed $paramMap parameter map |
||
| 285 | * @param mixed $matches found matched variables |
||
| 286 | * @param mixed $parameters route parameters |
||
| 287 | * @param mixed $handlers handler that should be executed |
||
| 288 | |||
| 289 | * @return mixed |
||
| 290 | */ |
||
| 291 | private function getFunctionArgumentsControlers($paramMap, $matches, $parameters, $handlers) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get names of parameters for provided class and method |
||
| 325 | * |
||
| 326 | * @param class $class name of class |
||
| 327 | * @param string $methodName name of method |
||
| 328 | * |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | private function getMethodParameters($class, $methodName) |
||
| 341 | } |
||
| 342 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: