Total Complexity | 71 |
Total Lines | 384 |
Duplicated Lines | 0 % |
Changes | 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 |
||
32 | class Router extends KernelRouter |
||
33 | { |
||
34 | /** |
||
35 | * Router::parseRequest |
||
36 | * |
||
37 | * @param KernelMessageUri|null $uri |
||
38 | * |
||
39 | * @return bool |
||
40 | * @throws \ReflectionException |
||
41 | */ |
||
42 | public function parseRequest(KernelMessageUri $uri = null) |
||
43 | { |
||
44 | $this->uri = is_null($uri) ? new KernelMessageUri() : $uri; |
||
|
|||
45 | $uriSegments = $this->uri->getSegments()->getParts(); |
||
46 | $uriString = $this->uri->getSegments()->getString(); |
||
47 | |||
48 | if ($this->uri->getSegments()->getTotalParts()) { |
||
49 | if (strpos(end($uriSegments), '.json') !== false) { |
||
50 | output()->setContentType('application/json'); |
||
51 | $endSegment = str_replace('.json', '', end($uriSegments)); |
||
52 | array_pop($uriSegments); |
||
53 | array_push($uriSegments, $endSegment); |
||
54 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
55 | $uriString = $this->uri->getSegments()->getString(); |
||
56 | } elseif (strpos(end($uriSegments), '.xml') !== false) { |
||
57 | output()->setContentType('application/xml'); |
||
58 | $endSegment = str_replace('.xml', '', end($uriSegments)); |
||
59 | array_pop($uriSegments); |
||
60 | array_push($uriSegments, $endSegment); |
||
61 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
62 | $uriString = $this->uri->getSegments()->getString(); |
||
63 | } |
||
64 | } else { |
||
65 | $uriPath = urldecode( |
||
66 | parse_url($_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH) |
||
67 | ); |
||
68 | |||
69 | $uriPathParts = explode('public/', $uriPath); |
||
70 | $uriPath = end($uriPathParts); |
||
71 | |||
72 | if ($uriPath !== '/') { |
||
73 | $uriString = $uriPath; |
||
74 | $uriSegments = array_filter(explode('/', $uriString)); |
||
75 | |||
76 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
77 | $uriString = $this->uri->getSegments()->getString(); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | // Load app addresses config |
||
82 | $this->addresses = config()->loadFile('addresses', true); |
||
83 | |||
84 | if ($this->addresses instanceof KernelAddresses) { |
||
85 | // Domain routing |
||
86 | if (null !== ($domain = $this->addresses->getDomain())) { |
||
87 | if (is_array($domain)) { |
||
88 | $uriSegments = array_merge($domain, $uriSegments); |
||
89 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
90 | $uriString = $this->uri->getSegments()->getString(); |
||
91 | $domain = reset($uriSegments); |
||
92 | } |
||
93 | |||
94 | if (false !== ($app = modules()->getApp($domain))) { |
||
95 | $this->registerModule($app); |
||
96 | } elseif (false !== ($module = modules()->getModule($domain))) { |
||
97 | $this->registerModule($module); |
||
98 | } |
||
99 | } elseif (false !== ($subdomain = $this->uri->getSubdomain())) { |
||
100 | if (false !== ($app = modules()->getApp($subdomain))) { |
||
101 | $this->registerModule($app); |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | |||
106 | // Module routing |
||
107 | if ($uriTotalSegments = count($uriSegments)) { |
||
108 | for ($i = 0; $i <= $uriTotalSegments; $i++) { |
||
109 | $uriRoutedSegments = array_diff($uriSegments, |
||
110 | array_slice($uriSegments, ($uriTotalSegments - $i))); |
||
111 | |||
112 | if ( ! empty($app)) { |
||
113 | if (reset($uriSegments) !== $app->getParameter()) { |
||
114 | array_unshift($uriRoutedSegments, $app->getParameter()); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | if (false !== ($module = modules()->getModule($uriRoutedSegments))) { |
||
119 | $uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
||
120 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
121 | $uriString = $this->uri->getSegments()->getString(); |
||
122 | |||
123 | $this->registerModule($module); |
||
124 | |||
125 | break; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | |||
130 | // Try to translate from uri string |
||
131 | if (false !== ($action = $this->addresses->getTranslation($uriString))) { |
||
132 | if ( ! $action->isValidHttpMethod(input()->server('REQUEST_METHOD')) && ! $action->isAnyHttpMethod()) { |
||
133 | output()->sendError(405); |
||
134 | } else { |
||
135 | // Checks if action closure is an array |
||
136 | if (is_array($closureSegments = $action->getClosure())) { |
||
137 | // Closure App Routing |
||
138 | if (false !== ($app = modules()->getModule(reset($closureSegments)))) { |
||
139 | array_shift($closureSegments); |
||
140 | $this->registerModule($app); |
||
141 | } |
||
142 | |||
143 | // Closure Module routing |
||
144 | if ($numOfClosureSegments = count($closureSegments)) { |
||
145 | for ($i = 0; $i <= $numOfClosureSegments; $i++) { |
||
146 | $closureRoutedSegments = array_diff($closureSegments, |
||
147 | array_slice($closureSegments, ($numOfClosureSegments - $i))); |
||
148 | |||
149 | if ( ! empty($app)) { |
||
150 | if (reset($closureSegments) !== $app->getParameter()) { |
||
151 | array_unshift($closureRoutedSegments, $app->getParameter()); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | if (false !== ($module = modules()->getModule($closureRoutedSegments))) { |
||
156 | $uriSegments = array_diff($closureSegments, $closureRoutedSegments); |
||
157 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($closureSegments)); |
||
158 | $uriString = $this->uri->getSegments()->getString(); |
||
159 | |||
160 | $this->registerModule($module); |
||
161 | |||
162 | break; |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | } else { |
||
167 | if (false !== ($parseSegments = $action->getParseUriString($uriString))) { |
||
168 | $uriSegments = $parseSegments; |
||
169 | } else { |
||
170 | $uriSegments = []; |
||
171 | } |
||
172 | |||
173 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
174 | $uriString = $this->uri->getSegments()->getString(); |
||
175 | |||
176 | $this->parseAction($action, $uriSegments); |
||
177 | if ( ! empty(services()->has('controller'))) { |
||
178 | return true; |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | // Try to get route from controller & page |
||
185 | if ($uriTotalSegments = count($uriSegments)) { |
||
186 | for ($i = 0; $i <= $uriTotalSegments; $i++) { |
||
187 | $uriRoutedSegments = array_slice($uriSegments, 0, ($uriTotalSegments - $i)); |
||
188 | $modules = modules()->getArrayCopy(); |
||
189 | |||
190 | foreach ($modules as $module) { |
||
191 | $controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
||
192 | if ($module->getNamespace() === 'O2System\Framework\\') { |
||
193 | $controllerNamespace = 'O2System\Framework\Http\Controllers\\'; |
||
194 | } |
||
195 | |||
196 | if (false !== ($pagesDir = $module->getDir('pages', true))) { |
||
197 | $pageFilePath = $pagesDir . implode(DIRECTORY_SEPARATOR, |
||
198 | array_map('dash', $uriRoutedSegments)) . '.phtml'; |
||
199 | |||
200 | if ( ! class_exists($controllerClassName = $controllerNamespace . 'Pages')) { |
||
201 | $controllerClassName = '\O2System\Framework\Http\Controllers\Pages'; |
||
202 | } else { |
||
203 | $controller = new $controllerClassName(); |
||
204 | |||
205 | /** |
||
206 | * Try to find from database |
||
207 | */ |
||
208 | $modelClassName = str_replace('Controllers', 'Models', $controllerClassName); |
||
209 | |||
210 | if (class_exists($modelClassName)) { |
||
211 | models()->load($modelClassName, 'controller'); |
||
212 | |||
213 | if (false !== ($page = models('controller')->find($uriString, 'segments'))) { |
||
214 | if (isset($page->content)) { |
||
215 | presenter()->partials->offsetSet('content', $page->content); |
||
216 | |||
217 | $this->setController( |
||
218 | (new KernelControllerDataStructure($controller)) |
||
219 | ->setRequestMethod('index') |
||
220 | ); |
||
221 | |||
222 | return true; |
||
223 | break; |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Try to find from page file |
||
231 | */ |
||
232 | if (is_file($pageFilePath)) { |
||
233 | presenter()->page->setFile($pageFilePath); |
||
234 | } else { |
||
235 | $pageFilePath = str_replace('.phtml', DIRECTORY_SEPARATOR . 'index.phtml', $pageFilePath); |
||
236 | if(is_file($pageFilePath)) { |
||
237 | presenter()->page->setFile($pageFilePath); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | if(presenter()->page->file instanceof SplFileInfo) { |
||
242 | $this->setController( |
||
243 | (new KernelControllerDataStructure($controllerClassName)) |
||
244 | ->setRequestMethod('index') |
||
245 | ); |
||
246 | |||
247 | return true; |
||
248 | break; |
||
249 | } |
||
250 | } |
||
251 | |||
252 | if (class_exists($controllerClassName = $controllerNamespace . implode('\\', |
||
253 | array_map('studlycase', $uriRoutedSegments)))) { |
||
254 | $uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
||
255 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
256 | $uriSegments); |
||
257 | break; |
||
258 | } |
||
259 | } |
||
260 | |||
261 | // break the loop if the controller has been set |
||
262 | if (services()->has('controller')) { |
||
263 | return true; |
||
264 | break; |
||
265 | } |
||
266 | } |
||
267 | } |
||
268 | |||
269 | if (class_exists($controllerClassName = modules()->current()->getDefaultControllerClassName())) { |
||
270 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
271 | $uriSegments); |
||
272 | |||
273 | return true; |
||
274 | } |
||
275 | |||
276 | // Let's the framework do the rest when there is no controller found |
||
277 | // the framework will redirect to PAGE 404 |
||
278 | } |
||
279 | |||
280 | // ------------------------------------------------------------------------ |
||
281 | |||
282 | /** |
||
283 | * Router::registerModule |
||
284 | * |
||
285 | * @param FrameworkModuleDataStructure $module |
||
286 | */ |
||
287 | final public function registerModule(FrameworkModuleDataStructure $module) |
||
288 | { |
||
289 | // Push Subdomain App Module |
||
290 | modules()->push($module); |
||
291 | |||
292 | // Load modular addresses config |
||
293 | if (false !== ($configDir = $module->getDir('config', true))) { |
||
294 | unset($addresses); |
||
295 | |||
296 | $reconfig = false; |
||
297 | if (is_file( |
||
298 | $filePath = $configDir . ucfirst( |
||
299 | strtolower(ENVIRONMENT) |
||
300 | ) . DIRECTORY_SEPARATOR . 'Addresses.php' |
||
301 | )) { |
||
302 | require($filePath); |
||
303 | $reconfig = true; |
||
304 | } elseif (is_file( |
||
305 | $filePath = $configDir . 'Addresses.php' |
||
306 | )) { |
||
307 | require($filePath); |
||
308 | $reconfig = true; |
||
309 | } |
||
310 | |||
311 | if ( ! $reconfig) { |
||
312 | $controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
||
313 | $controllerClassName = $controllerNamespace . studlycase($module->getParameter()); |
||
314 | |||
315 | if (class_exists($controllerClassName)) { |
||
316 | $this->addresses->any( |
||
317 | '/', |
||
318 | function () use ($controllerClassName) { |
||
319 | return new $controllerClassName(); |
||
320 | } |
||
321 | ); |
||
322 | } |
||
323 | } elseif (isset($addresses)) { |
||
324 | $this->addresses = $addresses; |
||
325 | } |
||
326 | } else { |
||
327 | $controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
||
328 | $controllerClassName = $controllerNamespace . studlycase($module->getParameter()); |
||
329 | |||
330 | if (class_exists($controllerClassName)) { |
||
331 | $this->addresses->any( |
||
332 | '/', |
||
333 | function () use ($controllerClassName) { |
||
334 | return new $controllerClassName(); |
||
335 | } |
||
336 | ); |
||
337 | } |
||
338 | } |
||
339 | } |
||
340 | |||
341 | // ------------------------------------------------------------------------ |
||
342 | |||
343 | /** |
||
344 | * Router::parseAction |
||
345 | * |
||
346 | * @param KernelActionDataStructure $action |
||
347 | * @param array $uriSegments |
||
348 | * |
||
349 | * @throws \ReflectionException |
||
350 | */ |
||
351 | protected function parseAction(KernelActionDataStructure $action, array $uriSegments = []) |
||
416 | } |
||
417 | } |
||
418 | } |
||
419 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..