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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
23 | class Route |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * @var Collector |
||
28 | */ |
||
29 | |||
30 | protected $collector; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | |||
36 | protected $method; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | |||
42 | protected $pattern; |
||
43 | |||
44 | /** |
||
45 | * @var callable |
||
46 | */ |
||
47 | |||
48 | protected $action; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | |||
54 | protected $namespace = ""; |
||
55 | |||
56 | /** |
||
57 | * @var string[] |
||
58 | */ |
||
59 | |||
60 | protected $params = []; |
||
61 | |||
62 | /** |
||
63 | * Defaults are parameters set by the user, and don't |
||
64 | * appear on the pattern. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | |||
69 | protected $defaults = []; |
||
70 | |||
71 | /** |
||
72 | * Metadata can be set to be used on filters, dispatch strategies |
||
73 | * or anywhere the route object is used. |
||
74 | * |
||
75 | * @var array |
||
76 | */ |
||
77 | |||
78 | protected $metadata = []; |
||
79 | |||
80 | /** |
||
81 | * @var string|StrategyInterface |
||
82 | */ |
||
83 | |||
84 | protected $strategy; |
||
85 | |||
86 | /** |
||
87 | * Blocked routes are dynamic routes selected to pass by the matcher. |
||
88 | * |
||
89 | * @var boolean |
||
90 | */ |
||
91 | |||
92 | protected $blocked = false; |
||
93 | |||
94 | /** |
||
95 | * The matcher that dispatched this route. |
||
96 | * |
||
97 | * @var Matcher $matcher |
||
98 | */ |
||
99 | |||
100 | protected $matcher; |
||
101 | |||
102 | /** |
||
103 | * The function used to create controllers from name. |
||
104 | * |
||
105 | * @var callable |
||
106 | */ |
||
107 | |||
108 | protected $controllerCreationFunction; |
||
109 | |||
110 | /** |
||
111 | * @param Collector $collector |
||
112 | * @param string $method |
||
113 | * @param string $pattern |
||
114 | * @param callable $action |
||
115 | */ |
||
116 | |||
117 | 47 | public function __construct(Collector $collector, $method, $pattern, $action) |
|
124 | |||
125 | /** |
||
126 | * Clone this route and set it into the collector. |
||
127 | * |
||
128 | * @return Route |
||
129 | */ |
||
130 | |||
131 | 4 | public function reset() |
|
137 | |||
138 | /** |
||
139 | * Remove this route from the collector. |
||
140 | * |
||
141 | * @return self |
||
142 | */ |
||
143 | |||
144 | 6 | public function forget() |
|
149 | |||
150 | /** |
||
151 | * Execute the route action, if no strategy was provided the action |
||
152 | * will be executed by the call_user_func PHP function. |
||
153 | * |
||
154 | * @throws BadRouteException |
||
155 | * @return mixed |
||
156 | */ |
||
157 | |||
158 | 15 | public function call() |
|
172 | |||
173 | /** |
||
174 | * Seek for dynamic content in one callable. This allow to use parameters defined on pattern on callable |
||
175 | * definition, eg. "get" "/{resource:string+}/{slug:slug+}" "{resource}::find". |
||
176 | * |
||
177 | * This will snakecase the resource parameter and deal with as a controller, then call the find method. |
||
178 | * A request for "/articles/my-first-article" will execute find method of Articles controller with only |
||
179 | * "my-first-article" as parameter. |
||
180 | * |
||
181 | * @param callable $callable |
||
182 | * @return callable |
||
183 | */ |
||
184 | |||
185 | 15 | private function parseCallable($callable) |
|
201 | |||
202 | /** |
||
203 | * Get the controller object. |
||
204 | * |
||
205 | * @param string $controller |
||
206 | * @return Object |
||
207 | */ |
||
208 | |||
209 | 9 | private function parseCallableController($controller) |
|
217 | |||
218 | /** |
||
219 | * Parse and replace dynamic content on route action. |
||
220 | * |
||
221 | * @param string $fragment Part of callable |
||
222 | * @return string |
||
223 | */ |
||
224 | |||
225 | 9 | private function parseCallablePlaceholders($fragment) |
|
237 | |||
238 | /** |
||
239 | * Execute the route action with the given strategy. |
||
240 | * |
||
241 | * @throws BadRouteException |
||
242 | * @return mixed |
||
243 | */ |
||
244 | |||
245 | 5 | private function callWithStrategy() |
|
257 | |||
258 | /** |
||
259 | * @return Collector |
||
260 | */ |
||
261 | |||
262 | public function getCollector() |
||
266 | |||
267 | /** |
||
268 | * @return string |
||
269 | */ |
||
270 | |||
271 | public function getMethod() |
||
275 | |||
276 | /** |
||
277 | * @return string |
||
278 | */ |
||
279 | |||
280 | 28 | public function getPattern() |
|
284 | |||
285 | /** |
||
286 | * @return string[] |
||
287 | */ |
||
288 | |||
289 | public function getSegments() |
||
293 | |||
294 | /** |
||
295 | * @return callable |
||
296 | */ |
||
297 | |||
298 | 9 | public function getAction() |
|
302 | |||
303 | /** |
||
304 | * @return string |
||
305 | */ |
||
306 | |||
307 | public function getNamespace() |
||
311 | |||
312 | /** |
||
313 | * @return string[] |
||
314 | */ |
||
315 | |||
316 | 28 | public function getParams() |
|
320 | |||
321 | /** |
||
322 | * @param string $key |
||
323 | * @return string |
||
324 | */ |
||
325 | |||
326 | public function getParam($key) |
||
330 | |||
331 | /** |
||
332 | * Return defaults and params merged in one array. |
||
333 | * |
||
334 | * @return array |
||
335 | */ |
||
336 | |||
337 | 3 | public function getMergedParams() |
|
341 | |||
342 | /** |
||
343 | * @return array |
||
344 | */ |
||
345 | |||
346 | 1 | public function getDefaults() |
|
350 | |||
351 | /** |
||
352 | * @param string $key |
||
353 | * @return mixed |
||
354 | */ |
||
355 | |||
356 | 1 | public function getDefault($key) |
|
360 | |||
361 | /** |
||
362 | * @return array |
||
363 | */ |
||
364 | |||
365 | 1 | public function getMetadataArray() |
|
369 | |||
370 | /** |
||
371 | * @param string $key |
||
372 | * @return mixed |
||
373 | */ |
||
374 | |||
375 | 1 | public function getMetadata($key) |
|
379 | |||
380 | /** |
||
381 | * @return string|null |
||
382 | */ |
||
383 | |||
384 | public function getStrategy() |
||
392 | |||
393 | /** |
||
394 | * @return Matcher |
||
395 | */ |
||
396 | |||
397 | public function getMatcher() |
||
401 | |||
402 | /** |
||
403 | * Verify if a Route have already been blocked. |
||
404 | * |
||
405 | * @return boolean |
||
406 | */ |
||
407 | |||
408 | 28 | public function getBlock() |
|
412 | |||
413 | /** |
||
414 | * Blocking a route indicate that that route have been selected and |
||
415 | * parsed, now it will be given to the matcher. |
||
416 | * |
||
417 | * @param bool $blocked |
||
418 | * @return self |
||
419 | */ |
||
420 | |||
421 | 28 | public function setBlock($blocked) |
|
426 | |||
427 | /** |
||
428 | * @param string $method |
||
429 | * @return Route |
||
430 | */ |
||
431 | |||
432 | 1 | public function setMethod($method) |
|
438 | |||
439 | /** |
||
440 | * @param string $pattern |
||
441 | * @return Route |
||
442 | */ |
||
443 | |||
444 | 3 | public function setPattern($pattern) |
|
450 | |||
451 | /** |
||
452 | * @param string $pattern |
||
453 | * @return self |
||
454 | */ |
||
455 | |||
456 | 28 | public function setPatternWithoutReset($pattern) |
|
461 | |||
462 | /** |
||
463 | * @param string $action |
||
464 | * @return self |
||
465 | */ |
||
466 | |||
467 | 3 | public function setAction($action) |
|
472 | |||
473 | /** |
||
474 | * @param string $namespace |
||
475 | * @return self |
||
476 | */ |
||
477 | |||
478 | 1 | public function setNamespace($namespace) |
|
483 | |||
484 | /** |
||
485 | * @param string[] $params |
||
486 | * @return self |
||
487 | */ |
||
488 | |||
489 | 29 | public function setParams(array $params) |
|
494 | |||
495 | /** |
||
496 | * @param string $key |
||
497 | * @param string $value |
||
498 | * |
||
499 | * @return self |
||
500 | */ |
||
501 | |||
502 | public function setParam($key, $value) |
||
507 | |||
508 | /** |
||
509 | * @param mixed[] $defaults |
||
510 | * @return self |
||
511 | */ |
||
512 | |||
513 | 5 | public function setDefaults(array $defaults) |
|
518 | |||
519 | /** |
||
520 | * @param string $key |
||
521 | * @param mixed $value |
||
522 | * |
||
523 | * @return self |
||
524 | */ |
||
525 | |||
526 | 1 | public function setDefault($key, $value) |
|
531 | |||
532 | /** |
||
533 | * @param mixed[] $metadata |
||
534 | * @return self |
||
535 | */ |
||
536 | |||
537 | 5 | public function setMetadataArray(array $metadata) |
|
542 | |||
543 | /** |
||
544 | * @param string $key |
||
545 | * @param mixed $value |
||
546 | * |
||
547 | * @return $this |
||
548 | */ |
||
549 | |||
550 | 1 | public function setMetadata($key, $value) |
|
555 | |||
556 | /** |
||
557 | * @param null|string|StrategyInterface $strategy |
||
558 | * @return self |
||
559 | */ |
||
560 | |||
561 | 16 | public function setStrategy($strategy) |
|
566 | |||
567 | /** |
||
568 | * @param Matcher $matcher |
||
569 | * @return self |
||
570 | */ |
||
571 | |||
572 | 26 | public function setMatcher(Matcher $matcher) |
|
577 | |||
578 | /** |
||
579 | * Set a constraint to a token in the route pattern. |
||
580 | * |
||
581 | * @param string $token |
||
582 | * @param string $regex |
||
583 | * |
||
584 | * @return self |
||
585 | */ |
||
586 | |||
587 | 1 | public function setConstraint($token, $regex) |
|
601 | |||
602 | /** |
||
603 | * Set a function to create controllers. |
||
604 | * |
||
605 | * @param callable $callable |
||
606 | * @throws BadRouteException |
||
607 | * @return self |
||
608 | */ |
||
609 | |||
610 | 1 | public function setControllerCreationFunction($callable) |
|
619 | |||
620 | /** |
||
621 | * @param string $key |
||
622 | * @return bool |
||
623 | */ |
||
624 | |||
625 | public function hasParam($key) |
||
629 | |||
630 | /** |
||
631 | * @param string $key |
||
632 | * @return bool |
||
633 | */ |
||
634 | |||
635 | public function hasDefault($key) |
||
639 | |||
640 | /** |
||
641 | * @param string $key |
||
642 | * @return bool |
||
643 | */ |
||
644 | |||
645 | public function hasMetadata($key) |
||
649 | |||
650 | } |
||
651 |