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 |
||
| 13 | class Router |
||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var Route |
||
| 18 | */ |
||
| 19 | public $route; |
||
| 20 | /** |
||
| 21 | * @var RouteCollection |
||
| 22 | */ |
||
| 23 | public $collection; |
||
| 24 | /** |
||
| 25 | * @var ResponseInterface |
||
| 26 | */ |
||
| 27 | public $response; |
||
| 28 | /** |
||
| 29 | * @var |
||
| 30 | */ |
||
| 31 | public $middleware; |
||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | public $matcher = []; |
||
| 36 | /** |
||
| 37 | * @var |
||
| 38 | */ |
||
| 39 | public $dispatcher; |
||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $config = [ |
||
| 44 | 'templateExtension' => ['.html', '.php', '.json', '.xml'], |
||
| 45 | 'templateCallback' => [], |
||
| 46 | 'di' => '', |
||
| 47 | 'generateRoutesPath' => false, |
||
| 48 | ]; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param RouteCollection $collection |
||
| 52 | * @param ResponseInterface $response |
||
| 53 | * @param Route $route |
||
| 54 | */ |
||
| 55 | public function __construct(RouteCollection $collection, ResponseInterface $response = null, Route $route = null) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param array $config |
||
| 69 | */ |
||
| 70 | public function setConfig($config) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | public function getConfig() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param object|array $matcher |
||
| 85 | */ |
||
| 86 | public function setMatcher($matcher) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $matcher |
||
| 95 | */ |
||
| 96 | public function addMatcher($matcher) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @description main function |
||
| 103 | */ |
||
| 104 | public function run() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param null $url |
||
| 114 | */ |
||
| 115 | public function setUrl($url = null) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | public function match() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * |
||
| 135 | */ |
||
| 136 | public function callTarget() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param array $responses |
||
| 149 | */ |
||
| 150 | public function setResponses($responses = []) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @description set response code |
||
| 157 | */ |
||
| 158 | public function callResponse() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param $controller |
||
| 189 | * @param $method |
||
| 190 | * @param array $methodArgs |
||
| 191 | * @param array $ctrlArgs |
||
| 192 | * @param array $classInstance |
||
| 193 | * @return mixed|null |
||
| 194 | */ |
||
| 195 | public function callMethod($controller, $method, $methodArgs = [], $ctrlArgs = [], $classInstance = []) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param $controller |
||
| 215 | * @param array $ctrlArgs |
||
| 216 | * @param array $classInstance |
||
| 217 | * @return object |
||
| 218 | * @throws \Exception |
||
| 219 | */ |
||
| 220 | public function callClass($controller, $ctrlArgs = [], $classInstance = []) |
||
| 241 | |||
| 242 | } |
||
| 243 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: