Total Complexity | 48 |
Total Lines | 303 |
Duplicated Lines | 0 % |
Changes | 12 | ||
Bugs | 1 | Features | 0 |
Complex classes like RouterEngine 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 RouterEngine, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | final class RouterEngine |
||
20 | { |
||
21 | // ---------------------------------------------------------------// |
||
22 | |||
23 | /** |
||
24 | * Stores the URL broken logic wise. |
||
25 | * |
||
26 | * @var array<int,string> |
||
27 | */ |
||
28 | private array $pathInfo = []; |
||
29 | |||
30 | /** |
||
31 | * Stores the request method i.e get,post etc. |
||
32 | */ |
||
33 | private string $httpMethod; |
||
34 | |||
35 | /** |
||
36 | * Stores the request uri. |
||
37 | */ |
||
38 | private string $uri; |
||
39 | |||
40 | /** |
||
41 | * Stores dir mode. |
||
42 | */ |
||
43 | private bool $dirMode = false; |
||
44 | |||
45 | /** |
||
46 | * Store Dirctory during dir Mode. |
||
47 | */ |
||
48 | private string $dir = ''; |
||
49 | |||
50 | /** |
||
51 | * stores debug msg. |
||
52 | */ |
||
53 | private string $debugMsg = ''; |
||
54 | |||
55 | // ---------------------------------------------------------------// |
||
56 | |||
57 | /** |
||
58 | * constructor overloading for auto routing. |
||
59 | */ |
||
60 | public function __construct( |
||
61 | /** |
||
62 | * Stores the RouterCollection object. |
||
63 | */ |
||
64 | private readonly RouteCollection $collection, |
||
65 | ) { |
||
66 | } |
||
67 | |||
68 | // ---------------------------------------------------------------// |
||
69 | |||
70 | /** |
||
71 | * Detects the URL and call the corrosponding method |
||
72 | * of corrosponding controller. |
||
73 | * |
||
74 | * @return array<int, mixed> |
||
75 | */ |
||
76 | public function route(string $httpMethod, string $uri): array |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Set Arguments on the request object. |
||
101 | * |
||
102 | * @return array<int, mixed> |
||
103 | */ |
||
104 | private function routeAuto(): array |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Function to get namespace. |
||
127 | */ |
||
128 | private function getNamespace(): string |
||
129 | { |
||
130 | if ($this->dirMode) { |
||
131 | return $this->collection->getNamespace().'\\'.$this->dir; |
||
132 | } |
||
133 | |||
134 | return $this->collection->getNamespace(); |
||
135 | } |
||
136 | |||
137 | // ---------------------------------------------------------------// |
||
138 | |||
139 | /** |
||
140 | * Function to get controller. |
||
141 | */ |
||
142 | private function getController(): string |
||
143 | { |
||
144 | $controller = ucfirst($this->pathInfo[0]); |
||
145 | |||
146 | if (isset($this->pathInfo[0]) && $this->collection->isDir(ucfirst($this->pathInfo[0]))) { |
||
147 | $this->dir = ucfirst($this->pathInfo[0]); |
||
148 | $this->dirMode = true; |
||
149 | array_shift($this->pathInfo); |
||
150 | } |
||
151 | |||
152 | if ($this->dirMode && isset($this->pathInfo[0])) { |
||
153 | $controller = $this->dir.'/'.ucfirst($this->pathInfo[0]); |
||
154 | } |
||
155 | |||
156 | // Set corrosponding controller |
||
157 | if (isset($this->pathInfo[0]) && '' !== $this->pathInfo[0] && '0' !== $this->pathInfo[0]) { |
||
158 | $controller = $this->collection->getController($controller); |
||
159 | } else { |
||
160 | $controller = $this->getNamespace().'\Main'; |
||
161 | } |
||
162 | |||
163 | if (!$controller) { |
||
164 | $controller = ''; |
||
165 | } |
||
166 | |||
167 | if (class_exists($controller)) { |
||
168 | return $controller; |
||
169 | } |
||
170 | |||
171 | $controller = $this->getNamespace().'\Main'; |
||
172 | |||
173 | if (class_exists($controller)) { |
||
174 | array_unshift($this->pathInfo, ''); |
||
175 | |||
176 | return $controller; |
||
177 | } |
||
178 | |||
179 | $this->debug('No Controller could be resolved:'.$controller); |
||
180 | |||
181 | return ''; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Function to throw 404 error. |
||
186 | */ |
||
187 | private function debug(string $message): void |
||
190 | } |
||
191 | |||
192 | // ---------------------------------------------------------------// |
||
193 | |||
194 | /** |
||
195 | * Function to dispach the method if method exist. |
||
196 | * |
||
197 | * @return bool|array<mixed> |
||
198 | */ |
||
199 | private function getArguments(string $controller, string $method): bool|array |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Function to check for 405. |
||
234 | */ |
||
235 | private function checkMethodNotAllowed(string $controller): bool |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Returns the method to be called according to URL. |
||
246 | */ |
||
247 | private function getMethod(string $controller): string |
||
248 | { |
||
249 | // Set Method from second argument from URL |
||
250 | if (isset($this->pathInfo[1])) { |
||
251 | if (method_exists($controller, $function = $this->httpMethod.ucfirst($this->pathInfo[1]))) { |
||
252 | return $function; |
||
253 | } |
||
254 | if (method_exists($controller, $function = 'all'.ucfirst($this->pathInfo[1]))) { |
||
255 | return $function; |
||
256 | } |
||
257 | } |
||
258 | |||
259 | if (isset($function)) { |
||
260 | $last_function = $function; |
||
261 | } |
||
262 | if (method_exists($controller, $function = $this->httpMethod.'Index')) { |
||
263 | array_unshift($this->pathInfo, ''); |
||
264 | |||
265 | return $function; |
||
266 | } |
||
267 | // Last attempt to invoke allIndex |
||
268 | if (method_exists($controller, $function = 'allIndex')) { |
||
269 | array_unshift($this->pathInfo, ''); |
||
270 | |||
271 | return $function; |
||
272 | } |
||
273 | |||
274 | if (isset($last_function)) { |
||
275 | $this->debug('Neither '.$function.' method nor '.$last_function.' method you found in '.$controller.' controller'); |
||
276 | |||
277 | return ''; |
||
278 | } |
||
279 | |||
280 | $this->debug($function.' method not found in '.$controller.' controller'); |
||
281 | |||
282 | return ''; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Function to route manual routes. |
||
287 | * |
||
288 | * @return array<int, mixed> |
||
289 | */ |
||
290 | private function routeManual(): array |
||
322 | } |
||
323 | } |
||
324 |