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 ArrayMatcher 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 ArrayMatcher, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class ArrayMatcher implements MatcherInterface |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var |
||
| 19 | */ |
||
| 20 | private $router; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | private $request = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private $resolver = ['isClosureAndTemplate', 'isControllerAndTemplate', 'isTemplate']; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $dispatcher = [ |
||
| 36 | 'isClosure' => ClosureDispatcher::class, |
||
| 37 | 'isController' => ControllerDispatcher::class, |
||
| 38 | 'isTemplate' => TemplateDispatcher::class, |
||
| 39 | 'isControllerAndTemplate' => [ControllerDispatcher::class, TemplateDispatcher::class], |
||
| 40 | 'isClosureAndTemplate' => [ClosureDispatcher::class, TemplateDispatcher::class], |
||
| 41 | ]; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param Router $router |
||
| 45 | */ |
||
| 46 | public function __construct(Router $router) |
||
| 47 | { |
||
| 48 | $this->router = $router; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param array $resolver |
||
| 53 | */ |
||
| 54 | public function setResolver($resolver = []) |
||
| 55 | { |
||
| 56 | $this->resolver = $resolver; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string $resolver |
||
| 61 | */ |
||
| 62 | public function addResolver($resolver) |
||
| 63 | { |
||
| 64 | $this->resolver[] = $resolver; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | public function getResolver() |
||
| 71 | { |
||
| 72 | return $this->resolver; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param array $dispatcher |
||
| 77 | */ |
||
| 78 | public function setDispatcher($dispatcher = []) |
||
| 79 | { |
||
| 80 | $this->dispatcher = $dispatcher; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param $method |
||
| 85 | * @param $class |
||
| 86 | * @internal param array $dispatcher |
||
| 87 | */ |
||
| 88 | public function addDispatcher($method, $class) |
||
| 89 | { |
||
| 90 | $this->dispatcher[$method] = $class; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | public function match() |
||
| 97 | { |
||
| 98 | $this->request = []; |
||
| 99 | for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) { |
||
| 100 | $this->request['prefix'] = ($this->router->collection->getRoutes('prefix_' . $i) != '') ? $this->router->collection->getRoutes('prefix_' . $i) : ''; |
||
| 101 | $this->request['subdomain'] = ($this->router->collection->getRoutes('subdomain_' . $i) != '') ? $this->router->collection->getRoutes('subdomain_' . $i) : ''; |
||
| 102 | foreach ($this->router->collection->getRoutes('routes_' . $i) as $route => $params) { |
||
| 103 | $this->request['params'] = $params; |
||
| 104 | $this->request['collection_index'] = $i; |
||
| 105 | if ($this->checkSubdomain($route)) { |
||
| 106 | $route = strstr($route, '/'); |
||
| 107 | $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/' . trim(trim($this->request['prefix'], '/') . '/' . trim($route, '/'), '/')); |
||
| 108 | if ($this->routeMatch('#^' . $this->request['route'] . '$#')) { |
||
| 109 | $this->setCallback(); |
||
| 110 | return $this->generateTarget(); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | return false; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param $route |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | private function checkSubdomain($route) |
||
|
|
|||
| 123 | { |
||
| 124 | $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http') . '://' . ($host = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'])); |
||
| 125 | $host = explode(':', $host)[0]; |
||
| 126 | $domain = $this->router->collection->getDomain($url); |
||
| 127 | if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.') . '.' . $domain . $route; |
||
| 128 | if ($route[0] == '/') { |
||
| 129 | return ($host != $domain) ? false : true; |
||
| 130 | } elseif ($route[0] != '/' && $host != $domain) { |
||
| 131 | $route = substr($route, 0, strpos($route, "/")); |
||
| 132 | $route = str_replace('{host}', $domain, $route); |
||
| 133 | $route = preg_replace_callback('#{subdomain}#', [$this, 'subdomainMatch'], $route); |
||
| 134 | if (preg_match('#^' . $route . '$#', $host, $this->request['called_subdomain'])) { |
||
| 135 | $this->request['called_subdomain'] = array_shift($this->request['called_subdomain']); |
||
| 136 | $this->request['subdomain'] = str_replace('.' . $domain, '', $host); |
||
| 137 | return true; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | return false; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | private function subdomainMatch() |
||
| 147 | { |
||
| 148 | if (is_array($this->request['params']) && isset($this->request['params']['subdomain'])) { |
||
| 149 | return '(' . $this->request['params']['subdomain'] . ')'; |
||
| 150 | } |
||
| 151 | return '([^/]+)'; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param $match |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | private function paramMatch($match) |
||
| 159 | { |
||
| 160 | if (is_array($this->request['params']) && isset($this->request['params']['arguments'][$match[1]])) { |
||
| 161 | $this->request['params']['arguments'][$match[1]] = str_replace('(', '(?:', $this->request['params']['arguments'][$match[1]]); |
||
| 162 | return '(' . $this->request['params']['arguments'][$match[1]] . ')'; |
||
| 163 | } |
||
| 164 | if(isset($this->router->collection->getRoutes('params_' . $this->request['collection_index'])['arguments'][$match[1]])){ |
||
| 165 | $this->request['params']['arguments'][$match[1]] = str_replace('(', '(?:', $this->router->collection->getRoutes('params_' . $this->request['collection_index'])['arguments'][$match[1]]); |
||
| 166 | return '(' . $this->request['params']['arguments'][$match[1]] . ')'; |
||
| 167 | } |
||
| 168 | return '([^/]+)'; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param $regex |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | private function routeMatch($regex) |
||
| 176 | { |
||
| 177 | $regex = (substr($this->request['route'], -1) == '*') ? '#^' . $this->request['route'] . '#' : $regex; |
||
| 178 | if (preg_match($regex, $this->router->route->getUrl(), $this->request['parameters'])) { |
||
| 179 | array_shift($this->request['parameters']); |
||
| 180 | return true; |
||
| 181 | } |
||
| 182 | return false; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | private function generateTarget() |
||
| 189 | { |
||
| 190 | if ($this->validMethod()) { |
||
| 191 | foreach ($this->resolver as $resolver) { |
||
| 192 | if (is_array($target = call_user_func_array([$this, $resolver], [$this->router->route->getCallback()]))) { |
||
| 193 | $this->setTarget($target); |
||
| 194 | return true; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | $this->router->response->setStatusCode(405); |
||
| 199 | return false; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param array $target |
||
| 204 | */ |
||
| 205 | public function setTarget($target = []) |
||
| 206 | { |
||
| 207 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
||
| 208 | $this->checkRequest('subdomain'); |
||
| 209 | $this->checkRequest('prefix'); |
||
| 210 | $this->router->route->setDetail($this->request); |
||
| 211 | $this->router->route->setTarget($target); |
||
| 212 | $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_' . $index)); |
||
| 213 | $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_' . $index)); |
||
| 214 | $this->router->route->addTarget('params', $this->router->collection->getRoutes('params_' . $index)); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param $key |
||
| 219 | */ |
||
| 220 | private function checkRequest($key) |
||
| 221 | { |
||
| 222 | if (strpos($this->request[$key], ':') !== false && isset($this->request['parameters'][0])) { |
||
| 223 | $replacements = $this->request['parameters']; |
||
| 224 | $keys = []; |
||
| 225 | $this->request['@' . $key] = $this->request[$key]; |
||
| 226 | $this->request[$key] = preg_replace_callback('#:([\w?]+)#', function ($matches) use (&$replacements, &$keys) { |
||
| 227 | $route_key = preg_replace("/[^A-Za-z0-9\\-_:]/", '', $matches[0]); |
||
| 228 | $keys[$route_key] = isset($replacements[0]) ? $replacements[0] : null; |
||
| 229 | return is_null($keys[$route_key]) ? '' : array_shift($replacements); |
||
| 230 | }, $this->request[$key]); |
||
| 231 | $this->request['keys'] = $keys; |
||
| 232 | $this->request['parameters'] = $replacements; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * |
||
| 238 | */ |
||
| 239 | private function setCallback() |
||
| 240 | { |
||
| 241 | if (isset($this->request['params'])) { |
||
| 242 | if (is_callable($this->request['params'])) { |
||
| 243 | $this->router->route->setCallback($this->request['params']); |
||
| 244 | } else { |
||
| 245 | if (is_array($this->request['params']) && isset($this->request['params']['use'])) { |
||
| 246 | if (is_array($this->request['params']['use']) && isset($this->request['params']['use'][$this->router->route->getMethod()])) { |
||
| 247 | $this->router->route->setCallback($this->request['params']['use'][$this->router->route->getMethod()]); |
||
| 248 | } elseif (!is_array($this->request['params']['use'])) { |
||
| 249 | $this->router->route->setCallback($this->request['params']['use']); |
||
| 250 | } |
||
| 251 | } else { |
||
| 252 | $this->router->route->setCallback($this->request['params']); |
||
| 253 | } |
||
| 254 | if (isset($this->request['params']['name'])) { |
||
| 255 | $this->router->route->setName($this->request['params']['name']); |
||
| 256 | } |
||
| 257 | if (isset($this->request['params']['method'])) { |
||
| 258 | $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']]; |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | public function validMethod() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param $callback |
||
| 281 | * @return array|bool |
||
| 282 | * @throws \Exception |
||
| 283 | */ |
||
| 284 | View Code Duplication | public function isClosureAndTemplate($callback) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * @param $callback |
||
| 299 | * @return array|bool |
||
| 300 | * @throws \Exception |
||
| 301 | */ |
||
| 302 | View Code Duplication | public function isControllerAndTemplate($callback) |
|
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * @param $callback |
||
| 318 | * @return bool|array |
||
| 319 | */ |
||
| 320 | public function isClosure($callback) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param $callback |
||
| 333 | * @throws \Exception |
||
| 334 | * @return bool|array |
||
| 335 | */ |
||
| 336 | public function isController($callback) |
||
| 337 | { |
||
| 338 | if (is_string($callback) && strpos($callback, '@') !== false) { |
||
| 339 | $routes = explode('@', $callback); |
||
| 340 | if (!isset($routes[1])) $routes[1] = 'index'; |
||
| 341 | if ($routes[1] == '{method}') { |
||
| 342 | $params = explode('/', trim(preg_replace('#' . rtrim(str_replace('*', '', $this->request['route']), '/') . '#', '', $this->router->route->getUrl()), '/')); |
||
| 343 | $routes[1] = empty($params[0]) ? 'index' : $params[0]; |
||
| 344 | $this->request['@method'] = $routes[1]; |
||
| 345 | array_shift($params); |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param $callback |
||
| 371 | * @throws \Exception |
||
| 372 | * @return bool|array |
||
| 373 | */ |
||
| 374 | public function isTemplate($callback) |
||
| 408 | |||
| 409 | } |
||
| 410 |
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: