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 |
||
15 | class ArrayMatcher implements MatcherInterface |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * @var |
||
20 | */ |
||
21 | private $router; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $request = []; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $resolver = ['isClosureAndTemplate', 'isControllerAndTemplate', 'isTemplate']; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | private $dispatcher = [ |
||
37 | 'isClosure' => ClosureDispatcher::class, |
||
38 | 'isController' => ControllerDispatcher::class, |
||
39 | 'isTemplate' => TemplateDispatcher::class, |
||
40 | 'isControllerAndTemplate' => [ControllerDispatcher::class, TemplateDispatcher::class], |
||
41 | 'isClosureAndTemplate' => [ClosureDispatcher::class, TemplateDispatcher::class], |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * @param Router $router |
||
46 | */ |
||
47 | public function __construct(Router $router) |
||
48 | { |
||
49 | $this->router = $router; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param array $resolver |
||
54 | */ |
||
55 | public function setResolver($resolver = []) |
||
56 | { |
||
57 | $this->resolver = $resolver; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param string $resolver |
||
62 | */ |
||
63 | public function addResolver($resolver) |
||
64 | { |
||
65 | $this->resolver[] = $resolver; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return array |
||
70 | */ |
||
71 | public function getResolver() |
||
72 | { |
||
73 | return $this->resolver; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param array $dispatcher |
||
78 | */ |
||
79 | public function setDispatcher($dispatcher = []) |
||
80 | { |
||
81 | $this->dispatcher = $dispatcher; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param $method |
||
86 | * @param $class |
||
87 | * @internal param array $dispatcher |
||
88 | */ |
||
89 | public function addDispatcher($method, $class) |
||
90 | { |
||
91 | $this->dispatcher[$method] = $class; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return bool |
||
96 | */ |
||
97 | public function match() |
||
98 | { |
||
99 | $this->request = []; |
||
100 | for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) { |
||
101 | $this->request['prefix'] = ($this->router->collection->getRoutes('prefix_' . $i) != '') ? $this->router->collection->getRoutes('prefix_' . $i) : ''; |
||
102 | $this->request['subdomain'] = ($this->router->collection->getRoutes('subdomain_' . $i) != '') ? $this->router->collection->getRoutes('subdomain_' . $i) : ''; |
||
103 | foreach ($this->router->collection->getRoutes('routes_' . $i) as $route => $params) { |
||
104 | $this->request['params'] = $params; |
||
105 | $this->request['collection_index'] = $i; |
||
106 | if ($this->checkSubdomain($route)) { |
||
107 | $route = strstr($route, '/'); |
||
108 | $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/' . trim(trim($this->request['prefix'], '/') . '/' . trim($route, '/'), '/')); |
||
109 | if ($this->routeMatch('#^' . $this->request['route'] . '$#')) { |
||
110 | $this->setCallback(); |
||
111 | return $this->generateTarget(); |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | return false; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param $route |
||
121 | * @return bool |
||
122 | */ |
||
123 | private function checkSubdomain($route) |
||
|
|||
124 | { |
||
125 | $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http') . '://' . ($host = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'])); |
||
126 | $host = explode(':', $host)[0]; |
||
127 | $domain = $this->router->collection->getDomain($url); |
||
128 | if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.') . '.' . $domain . $route; |
||
129 | if ($route[0] == '/') { |
||
130 | return ($host != $domain) ? false : true; |
||
131 | } elseif ($route[0] != '/' && $host != $domain) { |
||
132 | $route = substr($route, 0, strpos($route, "/")); |
||
133 | $route = str_replace('{host}', $domain, $route); |
||
134 | $route = preg_replace_callback('#{subdomain}#', [$this, 'subdomainMatch'], $route); |
||
135 | if (preg_match('#^' . $route . '$#', $host, $this->request['called_subdomain'])) { |
||
136 | $this->request['called_subdomain'] = array_shift($this->request['called_subdomain']); |
||
137 | $this->request['subdomain'] = str_replace('.' . $domain, '', $host); |
||
138 | return true; |
||
139 | } |
||
140 | } |
||
141 | return false; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return string |
||
146 | */ |
||
147 | private function subdomainMatch() |
||
148 | { |
||
149 | if (is_array($this->request['params']) && isset($this->request['params']['subdomain'])) { |
||
150 | return '(' . $this->request['params']['subdomain'] . ')'; |
||
151 | } |
||
152 | return '([^/]+)'; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param $match |
||
157 | * @return string |
||
158 | */ |
||
159 | private function paramMatch($match) |
||
160 | { |
||
161 | if (is_array($this->request['params']) && isset($this->request['params']['arguments'][$match[1]])) { |
||
162 | $this->request['params']['arguments'][$match[1]] = str_replace('(', '(?:', $this->request['params']['arguments'][$match[1]]); |
||
163 | return '(' . $this->request['params']['arguments'][$match[1]] . ')'; |
||
164 | } |
||
165 | return '([^/]+)'; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param $regex |
||
170 | * @return bool |
||
171 | */ |
||
172 | private function routeMatch($regex) |
||
173 | { |
||
174 | if (substr($this->request['route'], -1) == '*') { |
||
175 | $pos = strpos($this->request['route'], '*'); |
||
176 | View Code Duplication | if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params'])) { |
|
177 | return true; |
||
178 | } |
||
179 | } |
||
180 | View Code Duplication | if (preg_match($regex, $this->router->route->getUrl(), $this->request['parameters'])) { |
|
181 | array_shift($this->request['parameters']); |
||
182 | return true; |
||
183 | } |
||
184 | return false; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return bool |
||
189 | */ |
||
190 | private function generateTarget() |
||
191 | { |
||
192 | if ($this->validMethod()) { |
||
193 | foreach ($this->resolver as $resolver) { |
||
194 | if (is_array($target = call_user_func_array([$this, $resolver], [$this->router->route->getCallback()]))) { |
||
195 | $this->setTarget($target); |
||
196 | return true; |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | $this->router->response->setStatusCode(405); |
||
201 | return false; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param array $target |
||
206 | */ |
||
207 | public function setTarget($target = []) |
||
217 | |||
218 | /** |
||
219 | * @param $key |
||
220 | */ |
||
221 | private function checkRequest($key) |
||
229 | |||
230 | /** |
||
231 | * |
||
232 | */ |
||
233 | private function setCallback() |
||
234 | { |
||
235 | if (isset($this->request['params'])) { |
||
236 | if (is_callable($this->request['params'])) { |
||
237 | $this->router->route->setCallback($this->request['params']); |
||
238 | } else { |
||
239 | if (is_array($this->request['params']) && isset($this->request['params']['use'])) { |
||
240 | if(is_array($this->request['params']['use']) && isset($this->request['params']['use'][$this->router->route->getMethod()])){ |
||
241 | $this->router->route->setCallback($this->request['params']['use'][$this->router->route->getMethod()]); |
||
257 | |||
258 | /** |
||
259 | * @return bool |
||
260 | */ |
||
261 | public function validMethod() |
||
272 | |||
273 | /** |
||
274 | * @param $callback |
||
275 | * @return array|bool |
||
276 | * @throws \Exception |
||
277 | */ |
||
278 | View Code Duplication | public function isClosureAndTemplate($callback) |
|
290 | |||
291 | /** |
||
292 | * @param $callback |
||
293 | * @return array|bool |
||
294 | * @throws \Exception |
||
295 | */ |
||
296 | View Code Duplication | public function isControllerAndTemplate($callback) |
|
308 | |||
309 | |||
310 | /** |
||
311 | * @param $callback |
||
312 | * @return bool|array |
||
313 | */ |
||
314 | public function isClosure($callback) |
||
324 | |||
325 | /** |
||
326 | * @param $callback |
||
327 | * @throws \Exception |
||
328 | * @return bool|array |
||
329 | */ |
||
330 | public function isController($callback) |
||
363 | |||
364 | /** |
||
365 | * @param $callback |
||
366 | * @throws \Exception |
||
367 | * @return bool|array |
||
368 | */ |
||
369 | public function isTemplate($callback) |
||
400 | |||
401 | } |
||
402 |
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: