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 |
||
| 22 | class ClosureAutoBind implements Dispatcher |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Not found handler will be called if nothing has been found. |
||
| 26 | * |
||
| 27 | * @var \Closure |
||
| 28 | */ |
||
| 29 | private $notFoundHandler; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Dispatch found route with given parameters |
||
| 33 | * |
||
| 34 | * @param \Kambo\Router\Route\Route\Parsed $route Instance of found and parsed route. |
||
| 35 | * @param array $parameters Additional parameters. |
||
| 36 | * |
||
| 37 | * @return mixed|null |
||
| 38 | */ |
||
| 39 | public function dispatchRoute(Parsed $route, array $parameters = []) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Called if any of route did not match the request. |
||
| 58 | * Call the defined handler or simply do nothing if the handler is not |
||
| 59 | * specified. |
||
| 60 | * |
||
| 61 | * @return mixed|null |
||
| 62 | */ |
||
| 63 | public function dispatchNotFound() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Sets not found handler |
||
| 74 | * |
||
| 75 | * @param Closure $handler handler that will be excuted if nothing has been |
||
| 76 | * found |
||
| 77 | * |
||
| 78 | * @return self for fluent interface |
||
| 79 | * |
||
| 80 | * @throws InvalidArgumentException if the provided value is not closure |
||
| 81 | */ |
||
| 82 | public function setNotFoundHandler($handler) : ClosureAutoBind |
||
| 94 | |||
| 95 | // ------------ PRIVATE METHODS |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Check if variable is closure |
||
| 99 | * |
||
| 100 | * @param mixed $type variable to check |
||
| 101 | * |
||
| 102 | * @return boolean return true if is |
||
| 103 | */ |
||
| 104 | private function isClosure($type) : bool |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get arguments for closure function in proper order |
||
| 111 | * from provided parameters |
||
| 112 | * |
||
| 113 | * @param array $paramMap parameter map for getting proper order |
||
| 114 | * @param array $matches parameters from request |
||
| 115 | * @param array $parameters expected parameters from route |
||
| 116 | * |
||
| 117 | * @return array Parameters in right order, if there are not any |
||
| 118 | * parametrs an empty array is returned. |
||
| 119 | */ |
||
| 120 | private function getFunctionArguments(array $paramMap, array $matches, array $parameters) : array |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get name of parameters for provided closure |
||
| 138 | * |
||
| 139 | * @param \Closure $closure |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | private function getFunctionArgumentsNames($closure) : array |
||
| 155 | } |
||
| 156 |