Total Complexity | 48 |
Total Lines | 316 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
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.
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 |
||
22 | class Router |
||
23 | { |
||
24 | /** |
||
25 | * Router::$uri |
||
26 | * |
||
27 | * @var Message\Uri |
||
28 | */ |
||
29 | protected $uri; |
||
30 | |||
31 | /** |
||
32 | * Router::$addresses |
||
33 | * |
||
34 | * @var Router\Addresses |
||
35 | */ |
||
36 | protected $addresses; |
||
37 | |||
38 | // ------------------------------------------------------------------------ |
||
39 | |||
40 | /** |
||
41 | * Router::getUri |
||
42 | * |
||
43 | * Gets routed Uri. |
||
44 | * |
||
45 | * @return Message\Uri |
||
46 | */ |
||
47 | public function getUri() |
||
48 | { |
||
49 | return $this->uri; |
||
50 | } |
||
51 | |||
52 | // ------------------------------------------------------------------------ |
||
53 | |||
54 | /** |
||
55 | * Router::getAddresses |
||
56 | * |
||
57 | * @return \O2System\Kernel\Http\Router\Addresses |
||
58 | */ |
||
59 | public function getAddresses() |
||
62 | } |
||
63 | |||
64 | // ------------------------------------------------------------------------ |
||
65 | |||
66 | /** |
||
67 | * Router::setAddresses |
||
68 | * |
||
69 | * Sets router addresses. |
||
70 | * |
||
71 | * @param \O2System\Kernel\Http\Router\Addresses $addresses |
||
72 | * |
||
73 | * @return static |
||
74 | */ |
||
75 | public function setAddresses(Router\Addresses $addresses) |
||
80 | } |
||
81 | |||
82 | // ------------------------------------------------------------------------ |
||
83 | |||
84 | /** |
||
85 | * Router::handle |
||
86 | * |
||
87 | * @param \O2System\Kernel\Http\Message\Uri|null $uri |
||
88 | * |
||
89 | * @return bool |
||
90 | * @throws \ReflectionException |
||
91 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
92 | */ |
||
93 | public function handle(Message\Uri $uri = null) |
||
94 | { |
||
95 | $this->uri = is_null($uri) ? server_request()->getUri() : $uri; |
||
96 | $uriSegments = $this->uri->segments->getArrayCopy(); |
||
97 | $uriString = $this->uri->segments->__toString(); |
||
98 | |||
99 | if (count($uriSegments)) { |
||
100 | if (strpos(end($uriSegments), '.json') !== false) { |
||
101 | output()->setContentType('application/json'); |
||
|
|||
102 | $endSegment = str_replace('.json', '', end($uriSegments)); |
||
103 | array_pop($uriSegments); |
||
104 | array_push($uriSegments, $endSegment); |
||
105 | $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
||
106 | $uriString = $this->uri->segments->__toString(); |
||
107 | } elseif (strpos(end($uriSegments), '.xml') !== false) { |
||
108 | output()->setContentType('application/xml'); |
||
109 | $endSegment = str_replace('.xml', '', end($uriSegments)); |
||
110 | array_pop($uriSegments); |
||
111 | array_push($uriSegments, $endSegment); |
||
112 | $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
||
113 | $uriString = $this->uri->segments->__toString(); |
||
114 | } |
||
115 | } else { |
||
116 | $uriPath = urldecode( |
||
117 | parse_url($_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH) |
||
118 | ); |
||
119 | |||
120 | $uriPathParts = explode('public/', $uriPath); |
||
121 | $uriPath = end($uriPathParts); |
||
122 | |||
123 | if ($uriPath !== '/') { |
||
124 | $uriString = $uriPath; |
||
125 | $uriSegments = array_filter(explode('/', $uriString)); |
||
126 | |||
127 | $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
||
128 | $uriString = $this->uri->segments->__toString(); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | // Try to translate from uri string |
||
133 | if (false !== ($action = $this->addresses->getTranslation($uriString))) { |
||
134 | if ( ! $action->isValidHttpMethod(server_request()->getMethod()) && ! $action->isAnyHttpMethod()) { |
||
135 | output()->sendError(405); |
||
136 | } else { |
||
137 | if (false !== ($parseSegments = $action->getParseUriString($uriString))) { |
||
138 | $uriSegments = $parseSegments; |
||
139 | } else { |
||
140 | $uriSegments = []; |
||
141 | } |
||
142 | |||
143 | $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
||
144 | |||
145 | $this->parseAction($action, $uriSegments); |
||
146 | |||
147 | if (services()->has('controller')) { |
||
148 | return true; |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | // Try to get route from controller & page |
||
154 | if ($uriTotalSegments = count($uriSegments)) { |
||
155 | $namespaces = [ |
||
156 | 'App\Controllers\\', |
||
157 | 'App\Http\Controllers\\', |
||
158 | 'O2System\Reactor\Http\Controllers\\', |
||
159 | ]; |
||
160 | |||
161 | for ($i = 0; $i <= $uriTotalSegments; $i++) { |
||
162 | $uriRoutedSegments = array_slice($uriSegments, 0, ($uriTotalSegments - $i)); |
||
163 | |||
164 | foreach ($namespaces as $namespace) { |
||
165 | |||
166 | $controllerClassName = $namespace . implode('\\', |
||
167 | array_map('studlycase', $uriRoutedSegments)); |
||
168 | |||
169 | if (class_exists($controllerClassName)) { |
||
170 | $uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
||
171 | $this->setController(new Router\DataStructures\Controller($controllerClassName), |
||
172 | $uriSegments); |
||
173 | break; |
||
174 | } |
||
175 | } |
||
176 | |||
177 | // break the loop if the controller has been set |
||
178 | if (services()->has('controller')) { |
||
179 | return true; |
||
180 | break; |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | |||
185 | // Let's the app do the rest when there is no controller found |
||
186 | // the app should redirect to PAGE 404 |
||
187 | } |
||
188 | |||
189 | // ------------------------------------------------------------------------ |
||
190 | |||
191 | /** |
||
192 | * Router::parseAction |
||
193 | * |
||
194 | * @param \O2System\Kernel\Http\Router\DataStructures\Action $action |
||
195 | * @param array $uriSegments |
||
196 | * |
||
197 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
198 | * @throws \ReflectionException |
||
199 | */ |
||
200 | protected function parseAction(Router\DataStructures\Action $action, array $uriSegments = []) |
||
201 | { |
||
202 | $closure = $action->getClosure(); |
||
203 | if (empty($closure)) { |
||
204 | output()->sendError(204); |
||
205 | } |
||
206 | |||
207 | if ($closure instanceof Controller) { |
||
208 | $uriSegments = empty($uriSegments) |
||
209 | ? $action->getClosureParameters() |
||
210 | : $uriSegments; |
||
211 | $this->setController( |
||
212 | (new Router\DataStructures\Controller($closure)) |
||
213 | ->setRequestMethod('index'), |
||
214 | $uriSegments |
||
215 | ); |
||
216 | } elseif ($closure instanceof Router\DataStructures\Controller) { |
||
217 | $this->setController($closure, $action->getClosureParameters()); |
||
218 | } elseif (is_array($closure)) { |
||
219 | $this->uri = (new Message\Uri()) |
||
220 | ->withSegments(new Message\Uri\Segments('')) |
||
221 | ->withQuery(''); |
||
222 | $this->handle($this->uri->addSegments($closure)); |
||
223 | } else { |
||
224 | if (class_exists($closure)) { |
||
225 | $this->setController( |
||
226 | (new Router\DataStructures\Controller($closure)) |
||
227 | ->setRequestMethod('index'), |
||
228 | $uriSegments |
||
229 | ); |
||
230 | } elseif (preg_match("/([a-zA-Z0-9\\\]+)(@)([a-zA-Z0-9\\\]+)/", $closure, $matches)) { |
||
231 | $this->setController( |
||
232 | (new Router\DataStructures\Controller($matches[ 1 ])) |
||
233 | ->setRequestMethod($matches[ 3 ]), |
||
234 | $uriSegments |
||
235 | ); |
||
236 | } elseif (is_string($closure) && $closure !== '') { |
||
237 | if (is_json($closure)) { |
||
238 | output()->setContentType('application/json'); |
||
239 | output()->send($closure); |
||
240 | } else { |
||
241 | output()->send($closure); |
||
242 | } |
||
243 | } elseif (is_array($closure) || is_object($closure)) { |
||
244 | output()->send($closure); |
||
245 | } elseif (is_numeric($closure)) { |
||
246 | output()->sendError($closure); |
||
247 | } else { |
||
248 | output()->sendError(204); |
||
249 | exit(EXIT_ERROR); |
||
250 | } |
||
251 | } |
||
252 | } |
||
253 | |||
254 | // ------------------------------------------------------------------------ |
||
255 | |||
256 | /** |
||
257 | * Router::setController |
||
258 | * |
||
259 | * @param \O2System\Kernel\Http\Router\DataStructures\Controller $controller |
||
260 | * @param array $uriSegments |
||
261 | * |
||
262 | * @throws \ReflectionException |
||
263 | */ |
||
264 | protected function setController( |
||
338 | } |
||
339 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.