Total Complexity | 44 |
Total Lines | 341 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Complex classes like Route 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 Route, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Route |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * Current module name |
||
36 | * @var string |
||
37 | */ |
||
38 | private $moduleName; |
||
39 | |||
40 | /** |
||
41 | * Module options |
||
42 | * @var array |
||
43 | */ |
||
44 | private $moduleOptions = []; |
||
45 | |||
46 | /** |
||
47 | * Identifies the group middleware |
||
48 | * @var bool |
||
49 | */ |
||
50 | private $isGroupMiddlewares; |
||
51 | |||
52 | /** |
||
53 | * Identifies the group |
||
54 | * @var boolean |
||
55 | */ |
||
56 | private $isGroup = false; |
||
57 | |||
58 | /** |
||
59 | * Current group name |
||
60 | * @var string |
||
61 | */ |
||
62 | private $currentGroupName = null; |
||
63 | |||
64 | /** |
||
65 | * Current route |
||
66 | * @var array |
||
67 | */ |
||
68 | private $currentRoute = []; |
||
69 | |||
70 | /** |
||
71 | * Virtual routes |
||
72 | * @var array |
||
73 | */ |
||
74 | private $virtualRoutes = []; |
||
75 | |||
76 | /** |
||
77 | * @param array $module |
||
78 | * @throws ConfigException |
||
79 | * @throws DiException |
||
80 | * @throws ReflectionException |
||
81 | */ |
||
82 | public function __construct(array $module) |
||
83 | { |
||
84 | $this->virtualRoutes['*'] = []; |
||
85 | $this->moduleName = key($module); |
||
86 | $this->moduleOptions = $module[key($module)]; |
||
87 | |||
88 | if (config()->has('resource_cache') && !config()->has('view_cache')){ |
||
89 | config()->import(new Setup('config', 'view_cache')); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param string $route |
||
95 | * @param string $method |
||
96 | * @param ...$params |
||
97 | * @return $this |
||
98 | * @throws ConfigException |
||
99 | * @throws DatabaseException |
||
100 | * @throws DiException |
||
101 | * @throws LangException |
||
102 | * @throws ReflectionException |
||
103 | * @throws SessionException |
||
104 | */ |
||
105 | public function add(string $route, string $method, ...$params): Route |
||
106 | { |
||
107 | $this->currentRoute = [ |
||
108 | 'route' => !empty($this->moduleOptions['prefix']) ? $this->moduleOptions['prefix'] . '/' . $route : $route, |
||
109 | 'prefix' => $this->moduleOptions['prefix'], |
||
110 | 'method' => $method, |
||
111 | 'module' => $this->moduleName |
||
112 | ]; |
||
113 | |||
114 | if ($this->canSetCacheToCurrentRoute()){ |
||
115 | $this->currentRoute['cache_settings'] = [ |
||
116 | 'shouldCache' => true, |
||
117 | 'ttl' => config()->get('view_cache.ttl', 300), |
||
118 | ]; |
||
119 | } |
||
120 | |||
121 | if (is_callable($params[0])) { |
||
122 | $this->currentRoute['callback'] = $params[0]; |
||
123 | } else { |
||
124 | $this->currentRoute['controller'] = $params[0]; |
||
125 | $this->currentRoute['action'] = $params[1]; |
||
126 | } |
||
127 | |||
128 | if ($this->currentGroupName) { |
||
129 | $this->currentRoute['group'] = $this->currentGroupName; |
||
130 | $this->virtualRoutes[$this->currentGroupName][] = $this->currentRoute; |
||
131 | } else { |
||
132 | $this->isGroup = false; |
||
133 | $this->isGroupMiddlewares = false; |
||
134 | $this->virtualRoutes['*'][] = $this->currentRoute; |
||
135 | } |
||
136 | |||
137 | return $this; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param string $route |
||
142 | * @param ...$params |
||
143 | * @return $this |
||
144 | * @throws ConfigException |
||
145 | * @throws DatabaseException |
||
146 | * @throws DiException |
||
147 | * @throws LangException |
||
148 | * @throws ReflectionException |
||
149 | * @throws SessionException |
||
150 | */ |
||
151 | public function get(string $route, ...$params): Route |
||
152 | { |
||
153 | return $this->add($route, 'GET', ...$params); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param string $route |
||
158 | * @param ...$params |
||
159 | * @return $this |
||
160 | * @throws ConfigException |
||
161 | * @throws DatabaseException |
||
162 | * @throws DiException |
||
163 | * @throws LangException |
||
164 | * @throws ReflectionException |
||
165 | * @throws SessionException |
||
166 | */ |
||
167 | public function post(string $route, ...$params): Route |
||
168 | { |
||
169 | return $this->add($route, 'POST', ...$params); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Starts a named group of routes |
||
174 | * @param string $groupName |
||
175 | * @param Closure $callback |
||
176 | * @return Route |
||
177 | */ |
||
178 | public function group(string $groupName, Closure $callback): Route |
||
179 | { |
||
180 | $this->currentGroupName = $groupName; |
||
181 | |||
182 | $this->isGroup = true; |
||
183 | $this->isGroupMiddlewares = false; |
||
184 | $callback($this); |
||
185 | $this->isGroupMiddlewares = true; |
||
186 | $this->currentGroupName = null; |
||
187 | |||
188 | return $this; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Adds middlewares to routes and route groups |
||
193 | * @param array $middlewares |
||
194 | * @return Route |
||
195 | */ |
||
196 | public function middlewares(array $middlewares = []): Route |
||
197 | { |
||
198 | if (!$this->isGroup) { |
||
199 | end($this->virtualRoutes['*']); |
||
200 | $lastKey = key($this->virtualRoutes['*']); |
||
201 | $this->assignMiddlewaresToRoute($this->virtualRoutes['*'][$lastKey], $middlewares); |
||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | end($this->virtualRoutes); |
||
206 | $lastKeyOfFirstRound = key($this->virtualRoutes); |
||
207 | |||
208 | if (!$this->isGroupMiddlewares) { |
||
209 | end($this->virtualRoutes[$lastKeyOfFirstRound]); |
||
210 | $lastKeyOfSecondRound = key($this->virtualRoutes[$lastKeyOfFirstRound]); |
||
211 | $this->assignMiddlewaresToRoute($this->virtualRoutes[$lastKeyOfFirstRound][$lastKeyOfSecondRound], $middlewares); |
||
212 | return $this; |
||
213 | } |
||
214 | |||
215 | foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
||
216 | $this->assignMiddlewaresToRoute($route, $middlewares); |
||
217 | } |
||
218 | |||
219 | return $this; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @param bool $shouldCache |
||
224 | * @param int|null $ttl |
||
225 | * @return $this |
||
226 | * @throws ConfigException |
||
227 | * @throws DatabaseException |
||
228 | * @throws DiException |
||
229 | * @throws LangException |
||
230 | * @throws ReflectionException |
||
231 | * @throws SessionException |
||
232 | */ |
||
233 | public function cacheable(bool $shouldCache, int $ttl = null): Route |
||
234 | { |
||
235 | if (empty(session()->getId())){ |
||
236 | return $this; |
||
237 | } |
||
238 | |||
239 | if (!$ttl) { |
||
|
|||
240 | $ttl = config()->get('view_cache.ttl', 300); |
||
241 | } |
||
242 | |||
243 | if (!$this->isGroup){ |
||
244 | end($this->virtualRoutes['*']); |
||
245 | $lastKey = key($this->virtualRoutes['*']); |
||
246 | |||
247 | if (!$shouldCache && key_exists('cache_settings', $this->virtualRoutes['*'][$lastKey])) { |
||
248 | unset($this->virtualRoutes['*'][$lastKey]['cache_settings']); |
||
249 | }else{ |
||
250 | $this->assignCacheToCurrentRoute($this->virtualRoutes['*'][$lastKey], $shouldCache, $ttl); |
||
251 | } |
||
252 | |||
253 | return $this; |
||
254 | } |
||
255 | |||
256 | end($this->virtualRoutes); |
||
257 | $lastKeyOfFirstRound = key($this->virtualRoutes); |
||
258 | |||
259 | foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) { |
||
260 | if (!$shouldCache && key_exists('cache_settings', $route)) { |
||
261 | unset($route['cache_settings']); |
||
262 | }else{ |
||
263 | $this->assignCacheToCurrentRoute($route, $shouldCache, $ttl); |
||
264 | } |
||
265 | } |
||
266 | |||
267 | return $this; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Sets a unique name for a route |
||
272 | * @param string $name |
||
273 | * @return Route |
||
274 | * @throws RouteException |
||
275 | */ |
||
276 | public function name(string $name): Route |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Gets the run-time routes |
||
303 | * @return array |
||
304 | */ |
||
305 | public function getRuntimeRoutes(): array |
||
306 | { |
||
307 | $runtimeRoutes = []; |
||
308 | foreach ($this->virtualRoutes as $virtualRoute) { |
||
309 | foreach ($virtualRoute as $route) { |
||
310 | $runtimeRoutes[] = $route; |
||
311 | } |
||
312 | } |
||
313 | return $runtimeRoutes; |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * Gets the virtual routes |
||
318 | * @return array |
||
319 | */ |
||
320 | public function getVirtualRoutes(): array |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Assigns middlewares to the route |
||
327 | * @param array $route |
||
328 | * @param array $middlewares |
||
329 | */ |
||
330 | private function assignMiddlewaresToRoute(array &$route, array $middlewares) |
||
339 | } |
||
340 | } |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * @param array $route |
||
345 | * @param bool $shouldCache |
||
346 | * @param int $ttl |
||
347 | * @return void |
||
348 | */ |
||
349 | private function assignCacheToCurrentRoute(array &$route, bool $shouldCache, int $ttl) |
||
354 | ]; |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * @return bool |
||
359 | * @throws ConfigException |
||
360 | * @throws DatabaseException |
||
361 | * @throws DiException |
||
362 | * @throws LangException |
||
363 | * @throws SessionException |
||
364 | * @throws ReflectionException |
||
365 | */ |
||
366 | private function canSetCacheToCurrentRoute(): bool |
||
372 | } |
||
373 | } |
||
374 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: