Total Complexity | 45 |
Total Lines | 367 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 1 | Features | 0 |
Complex classes like UrlParser 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 UrlParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | trait UrlParser |
||
5 | { |
||
6 | |||
7 | /** |
||
8 | * Supported types of URL parameters |
||
9 | * |
||
10 | * @var array |
||
11 | */ |
||
12 | private $types = []; |
||
13 | |||
14 | /** |
||
15 | * Parsed parameters of the calling router |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $parameters = []; |
||
20 | |||
21 | /** |
||
22 | * Method handles integer type |
||
23 | * |
||
24 | * @param string $value |
||
25 | * value to be parsed |
||
26 | * @return bool was the value parsed |
||
27 | */ |
||
28 | public static function intHandler(string &$value): bool |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Method handles command type |
||
40 | * |
||
41 | * @param string $value |
||
42 | * value to be parsed |
||
43 | * @return bool was the value parsed |
||
44 | */ |
||
45 | public static function commandHandler(string &$value): bool |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Method handles list of integers type |
||
56 | * |
||
57 | * @param string $value |
||
58 | * value to be parsed |
||
59 | * @return bool was the value parsed |
||
60 | */ |
||
61 | public static function intListHandler(string &$value): bool |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Method handles string type |
||
72 | * |
||
73 | * @param string $value |
||
74 | * value to be parsed |
||
75 | * @return bool was the value parsed |
||
76 | */ |
||
77 | public static function stringHandler(string &$value): bool |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Init types |
||
86 | */ |
||
87 | protected function initDefaultTypes() |
||
88 | { |
||
89 | $this->types['i'] = '\Mezon\Router\UrlParser::intHandler'; |
||
90 | $this->types['a'] = '\Mezon\Router\UrlParser::commandHandler'; |
||
91 | $this->types['il'] = '\Mezon\Router\UrlParser::intListHandler'; |
||
92 | $this->types['s'] = '\Mezon\Router\UrlParser::stringHandler'; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Matching parameter and component |
||
97 | * |
||
98 | * @param mixed $component |
||
99 | * Component of the URL |
||
100 | * @param string $parameter |
||
101 | * Parameter to be matched |
||
102 | * @return string Matched url parameter |
||
103 | */ |
||
104 | private function _matchParameterAndComponent(&$component, string $parameter) |
||
105 | { |
||
106 | $parameterData = explode(':', trim($parameter, '[]')); |
||
107 | |||
108 | if (isset($this->types[$parameterData[0]])) { |
||
109 | if ($this->types[$parameterData[0]]($component)) { |
||
110 | return $parameterData[1]; |
||
111 | } else { |
||
112 | return ''; |
||
113 | } |
||
114 | } else { |
||
115 | throw (new \Exception('Unknown parameter type : ' . $parameterData[0])); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Method matches route and pattern |
||
121 | * |
||
122 | * @param array $cleanRoute |
||
123 | * Cleaned route splitted in parts |
||
124 | * @param array $cleanPattern |
||
125 | * Route pattern |
||
126 | * @return array|bool Array of route's parameters |
||
127 | */ |
||
128 | private function _matchRouteAndPattern(array $cleanRoute, array $cleanPattern) |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Method searches dynamic route processor |
||
161 | * |
||
162 | * @param array $processors |
||
163 | * Callable router's processor |
||
164 | * @param string $route |
||
165 | * Route |
||
166 | * @return array|callable|bool route's handler or false in case the handler was not found |
||
167 | */ |
||
168 | protected function getDynamicRouteProcessor(array &$processors, string $route) |
||
169 | { |
||
170 | $cleanRoute = explode('/', trim($route, '/')); |
||
171 | |||
172 | foreach ($processors as $i => $processor) { |
||
173 | $cleanPattern = explode('/', trim($i, '/')); |
||
174 | |||
175 | if ($this->_matchRouteAndPattern($cleanRoute, $cleanPattern) !== false) { |
||
176 | return $processor; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | return false; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Method searches dynamic route processor |
||
185 | * |
||
186 | * @param array $processors |
||
187 | * Callable router's processor |
||
188 | * @param string $route |
||
189 | * Route |
||
190 | * @return string|bool Result of the router'scall or false if any error occured |
||
191 | */ |
||
192 | public function findDynamicRouteProcessor(array &$processors, string $route) |
||
193 | { |
||
194 | $cleanRoute = explode('/', trim($route, '/')); |
||
195 | |||
196 | foreach ($processors as $i => $processor) { |
||
197 | $cleanPattern = explode('/', trim($i, '/')); |
||
198 | |||
199 | if ($this->_matchRouteAndPattern($cleanRoute, $cleanPattern) !== false) { |
||
200 | return call_user_func($processor, $route, $this->parameters); // return result of the router |
||
201 | } |
||
202 | } |
||
203 | |||
204 | return false; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Checking that method exists |
||
209 | * |
||
210 | * @param mixed $processor |
||
211 | * callback object |
||
212 | * @param ?string $functionName |
||
213 | * callback method |
||
214 | * @return bool true if method does not exists |
||
215 | */ |
||
216 | private function methodDoesNotExists($processor, ?string $functionName): bool |
||
217 | { |
||
218 | return isset($processor[0]) && method_exists($processor[0], $functionName) === false; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Checking that handler can be called |
||
223 | * |
||
224 | * @param object|array|callable $processor |
||
225 | * callback object |
||
226 | * @param ?string $functionName |
||
227 | * callback method |
||
228 | * @return bool |
||
229 | */ |
||
230 | private function canBeCalled($processor, ?string $functionName): bool |
||
231 | { |
||
232 | return is_callable($processor) && |
||
233 | (method_exists($processor[0], $functionName) || isset($processor[0]->$functionName)); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Checking that processor can be called as function |
||
238 | * |
||
239 | * @param mixed $processor |
||
240 | * route processor |
||
241 | * @return bool true if the $processor can be called as function |
||
242 | */ |
||
243 | private function isFunction($processor): bool |
||
244 | { |
||
245 | return is_callable($processor) && is_array($processor) === false; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Method returns either universal hanler if it fits or normal handler |
||
250 | * |
||
251 | * @param array $processors |
||
252 | * list of routes and handlers |
||
253 | * @param string $route |
||
254 | * calling route |
||
255 | * @return mixed processor |
||
256 | */ |
||
257 | protected function getExactRouteHandlerOrUniversal(&$processors, string $route) |
||
258 | { |
||
259 | if ($this->universalRouteWasAdded) { |
||
260 | $allRoutes = array_keys($processors); |
||
261 | |||
262 | if (array_search('*', $allRoutes) <= array_search($route, $allRoutes)) { |
||
263 | $processor = $processors['*']; |
||
264 | } else { |
||
265 | $processor = $processors[$route]; |
||
266 | } |
||
267 | } else { |
||
268 | $processor = $processors[$route]; |
||
269 | } |
||
270 | |||
271 | return $processor; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Method executes route handler |
||
276 | * |
||
277 | * @param array|callable $processor |
||
278 | * @param string $route |
||
279 | * @return mixed route handler execution result |
||
280 | */ |
||
281 | protected function executeHandler($processor, string $route) |
||
299 | } |
||
300 | } |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Method returns route handler |
||
305 | * |
||
306 | * @param mixed $processors |
||
307 | * Callable router's processor |
||
308 | * @param string $route |
||
309 | * Route |
||
310 | * @return array|callable|bool route handler |
||
311 | */ |
||
312 | protected function getStaticRouteProcessor(&$processors, string $route) |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Method searches route processor |
||
327 | * |
||
328 | * @param mixed $processors |
||
329 | * Callable router's processor |
||
330 | * @param string $route |
||
331 | * Route |
||
332 | * @return mixed Result of the router processor |
||
333 | */ |
||
334 | public function findStaticRouteProcessor(&$processors, string $route) |
||
335 | { |
||
336 | $processor = $this->getStaticRouteProcessor($processors, $route); |
||
337 | |||
338 | if ($processor === false) { |
||
339 | return false; |
||
340 | } |
||
341 | |||
342 | return $this->executeHandler($processor, $route); |
||
|
|||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Method returns route parameter |
||
347 | * |
||
348 | * @param string $name |
||
349 | * Route parameter |
||
350 | * @return string Route parameter |
||
351 | */ |
||
352 | public function getParam(string $name): string |
||
353 | { |
||
354 | if (isset($this->parameters[$name]) === false) { |
||
355 | throw (new \Exception('Parameter ' . $name . ' was not found in route', - 1)); |
||
356 | } |
||
357 | |||
358 | return $this->parameters[$name]; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Does parameter exists |
||
363 | * |
||
364 | * @param string $name |
||
365 | * Param name |
||
366 | * @return bool True if the parameter exists |
||
367 | */ |
||
368 | public function hasParam(string $name): bool |
||
371 | } |
||
372 | } |