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 |
||
| 18 | class DispatcherClosure implements DispatcherInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Not found handler will be called if nothing has been found. |
||
| 22 | * |
||
| 23 | * @var \Closure |
||
| 24 | */ |
||
| 25 | private $notFoundHandler; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Dispatch found route with given parameters |
||
| 29 | * |
||
| 30 | * @param Route $route found route |
||
| 31 | * @param mixed $parameters parameters for route |
||
| 32 | * |
||
| 33 | * @return mixed |
||
| 34 | */ |
||
| 35 | public function dispatchRoute(Route $route, array $parameters) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Called if nothing was not found. |
||
| 54 | * Can call a a defined handler or simply do nothing if the handler is |
||
| 55 | * not specified. |
||
| 56 | * |
||
| 57 | * @return mixed|null |
||
| 58 | */ |
||
| 59 | public function dispatchNotFound() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Set not found handler |
||
| 70 | * |
||
| 71 | * @return self for fluent interface |
||
| 72 | * |
||
| 73 | * @throws InvalidArgumentException if the provided value is not closure |
||
| 74 | */ |
||
| 75 | public function setNotFoundHandler($handler) |
||
| 87 | |||
| 88 | // ------------ PRIVATE METHODS |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Check if variable is closure |
||
| 92 | * |
||
| 93 | * @param mixed $type variable to check |
||
| 94 | * |
||
| 95 | * @return boolean return true if is |
||
| 96 | */ |
||
| 97 | private function isClosure($type) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get arguments for closure function in proper order |
||
| 104 | * from provided parameters |
||
| 105 | * |
||
| 106 | * @param mixed $paramMap parameter map for getting proper order |
||
| 107 | * @param mixed $matches parameters from request |
||
| 108 | * @param mixed $parameters expected parameters from route |
||
| 109 | * |
||
| 110 | * @return array Parameters in right order, if there are not any |
||
| 111 | * parametrs an empty array is returned. |
||
| 112 | */ |
||
| 113 | private function getFunctionArguments($paramMap, $matches, $parameters) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get name of parameters for provided closure |
||
| 132 | * |
||
| 133 | * @param \Closure $closure |
||
| 134 | * |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | private function getFunctionArgumentsNames($closure) |
||
| 147 | } |
||
| 148 |