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 Router 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 Router, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Router implements |
||
| 18 | InjectionAwareInterface, |
||
| 19 | ConfigureInterface |
||
| 20 | { |
||
| 21 | use InjectionAwareTrait; |
||
| 22 | use Configure2Trait { |
||
| 23 | configure as protected configure2; |
||
| 24 | } |
||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array $routes all the routes. |
||
| 30 | * @var array $internalRoutes all internal routes. |
||
| 31 | * @var null|string $lastRoute last route that was matched and called. |
||
| 32 | */ |
||
| 33 | private $routes = []; |
||
| 34 | private $internalRoutes = []; |
||
| 35 | private $lastRoute = null; |
||
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * @const DEVELOPMENT Verbose with exceptions. |
||
| 41 | * @const PRODUCTION Exceptions turns into 500. |
||
| 42 | */ |
||
| 43 | const DEVELOPMENT = 0; |
||
| 44 | const PRODUCTION = 1; |
||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Load and apply configurations. |
||
| 50 | * |
||
| 51 | * @param array|string $what is an array with key/value config options |
||
| 52 | * or a file to be included which returns such |
||
| 53 | * an array. |
||
| 54 | * |
||
| 55 | * @return self |
||
| 56 | */ |
||
| 57 | public function configure($what) |
||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * Handle the routes and match them towards the request, dispatch them |
||
| 90 | * when a match is made. Each route handler may throw exceptions that |
||
| 91 | * may redirect to an internal route for error handling. |
||
| 92 | * Several routes can match and if the routehandler does not break |
||
| 93 | * execution flow, the route matching will carry on. |
||
| 94 | * Only the last routehandler will get its return value returned further. |
||
| 95 | * |
||
| 96 | * @param string $path the path to find a matching handler for. |
||
| 97 | * @param string $method the request method to match. |
||
| 98 | * |
||
| 99 | * @return mixed content returned from route. |
||
| 100 | */ |
||
| 101 | 6 | public function handle($path, $method = null) |
|
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | /** |
||
| 134 | * Handle an internal route, the internal routes are not exposed to the |
||
| 135 | * end user. |
||
| 136 | * |
||
| 137 | * @param string $rule for this route. |
||
| 138 | * |
||
| 139 | * @throws \Anax\Route\Exception\NotFoundException |
||
| 140 | * |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | 2 | View Code Duplication | public function handleInternal($rule) |
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * Load routes from a config file, the file should return an array with |
||
| 157 | * details of the routes. These details are used to create the routes. |
||
| 158 | * |
||
| 159 | * @throws \Anax\Route\Exception\ConfigurationException |
||
| 160 | * |
||
| 161 | * @param array $route details on the route. |
||
| 162 | * |
||
| 163 | * @return self |
||
| 164 | * |
||
| 165 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
| 166 | */ |
||
| 167 | public function load($route) |
||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Add a route with a request method, a path rule to match and an action |
||
| 211 | * as the callback. Adding several path rules (array) results in several |
||
| 212 | * routes being created. |
||
| 213 | * |
||
| 214 | * @param null|string|array $method as a valid request method. |
||
| 215 | * @param null|string|array $rule path rule for this route. |
||
| 216 | * @param null|string|callable $action to implement a handler for the route. |
||
| 217 | * @param null|string $info about the route. |
||
| 218 | * |
||
| 219 | * @return class|array as new route(s), class if one added, else array. |
||
| 220 | */ |
||
| 221 | 6 | View Code Duplication | public function any($method, $rule, $action, $info = null) |
| 235 | |||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * Add a route to the router by rule(s) and a callback. |
||
| 240 | * |
||
| 241 | * @param null|string|array $rule for this route. |
||
| 242 | * @param null|string|callable $action a callback handler for the route. |
||
| 243 | * |
||
| 244 | * @return class|array as new route(s), class if one added, else array. |
||
| 245 | */ |
||
| 246 | 5 | public function add($rule, $action = null) |
|
| 250 | |||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * Add a default route which will be applied for any path. |
||
| 255 | * |
||
| 256 | * @param string|callable $action a callback handler for the route. |
||
| 257 | * |
||
| 258 | * @return class as new route. |
||
| 259 | */ |
||
| 260 | public function always($action) |
||
| 264 | |||
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * Add a default route which will be applied for any path, if the choosen |
||
| 269 | * request method is matching. |
||
| 270 | * |
||
| 271 | * @param null|string|array $method as request methods |
||
| 272 | * @param null|string|callable $action a callback handler for the route. |
||
| 273 | * |
||
| 274 | * @return class|array as new route(s), class if one added, else array. |
||
| 275 | */ |
||
| 276 | 1 | public function all($method, $action) |
|
| 280 | |||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * Shortcut to add a GET route. |
||
| 285 | * |
||
| 286 | * @param null|string|array $method as request methods |
||
| 287 | * @param null|string|callable $action a callback handler for the route. |
||
| 288 | * |
||
| 289 | * @return class|array as new route(s), class if one added, else array. |
||
| 290 | */ |
||
| 291 | public function get($rule, $action) |
||
| 295 | |||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * Shortcut to add a POST route. |
||
| 300 | * |
||
| 301 | * @param null|string|array $method as request methods |
||
| 302 | * @param null|string|callable $action a callback handler for the route. |
||
| 303 | * |
||
| 304 | * @return class|array as new route(s), class if one added, else array. |
||
| 305 | */ |
||
| 306 | public function post($rule, $action) |
||
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * Shortcut to add a PUT route. |
||
| 315 | * |
||
| 316 | * @param null|string|array $method as request methods |
||
| 317 | * @param null|string|callable $action a callback handler for the route. |
||
| 318 | * |
||
| 319 | * @return class|array as new route(s), class if one added, else array. |
||
| 320 | */ |
||
| 321 | public function put($rule, $action) |
||
| 325 | |||
| 326 | |||
| 327 | |||
| 328 | /** |
||
| 329 | * Shortcut to add a DELETE route. |
||
| 330 | * |
||
| 331 | * @param null|string|array $method as request methods |
||
| 332 | * @param null|string|callable $action a callback handler for the route. |
||
| 333 | * |
||
| 334 | * @return class|array as new route(s), class if one added, else array. |
||
| 335 | */ |
||
| 336 | public function delete($rule, $action) |
||
| 340 | |||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * Add an internal route to the router, this route is not exposed to the |
||
| 345 | * browser and the end user. |
||
| 346 | * |
||
| 347 | * @param string $rule for this route |
||
| 348 | * @param null|string|callable $action a callback handler for the route. |
||
| 349 | * |
||
| 350 | * @return class|array as new route(s), class if one added, else array. |
||
| 351 | */ |
||
| 352 | 2 | View Code Duplication | public function addInternal($rule, $action) |
| 359 | |||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * Get the route for the last route that was handled. |
||
| 364 | * |
||
| 365 | * @return mixed |
||
| 366 | */ |
||
| 367 | public function getLastRoute() |
||
| 371 | |||
| 372 | |||
| 373 | |||
| 374 | /** |
||
| 375 | * Get all routes. |
||
| 376 | * |
||
| 377 | * @return array with all routes. |
||
| 378 | */ |
||
| 379 | public function getAll() |
||
| 383 | |||
| 384 | |||
| 385 | |||
| 386 | /** |
||
| 387 | * Get all internal routes. |
||
| 388 | * |
||
| 389 | * @return array with internal routes. |
||
| 390 | */ |
||
| 391 | public function getInternal() |
||
| 395 | } |
||
| 396 |
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: