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 string|array|\Closure |
||
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 string|array|\Closure |
||
106 | */ |
||
107 | |||
108 | protected $controllerCreationFunction; |
||
109 | |||
110 | /** |
||
111 | * @param Collector $collector |
||
112 | * @param string $method |
||
113 | * @param string $pattern |
||
114 | * @param string|array|\Closure $action |
||
115 | */ |
||
116 | |||
117 | 35 | 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 | 3 | public function reset() |
|
137 | |||
138 | /** |
||
139 | * Remove this route from the collector. |
||
140 | * |
||
141 | * @return self |
||
142 | */ |
||
143 | |||
144 | 5 | 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 | 7 | public function call() |
|
172 | |||
173 | /** |
||
174 | * Seek for dynamic content on callables. eg. routes action controller#action |
||
175 | * syntax allow to use the variables to build the string like: {controller}@{action} |
||
176 | * |
||
177 | * @param string|array|\Closure $callable |
||
178 | * @return string|array|\Closure |
||
179 | */ |
||
180 | |||
181 | 7 | private function parseCallable($callable) |
|
196 | |||
197 | /** |
||
198 | * Get the controller object. |
||
199 | * |
||
200 | * @param string $controller |
||
201 | * @return Object |
||
202 | */ |
||
203 | |||
204 | 4 | private function parseCallableController($controller) |
|
212 | |||
213 | /** |
||
214 | * Parse and replace dynamic content on route action. |
||
215 | * |
||
216 | * @param string $fragment Part of callable |
||
217 | * @return string |
||
218 | */ |
||
219 | |||
220 | 4 | private function parseCallablePlaceholders($fragment) |
|
232 | |||
233 | /** |
||
234 | * Execute the route action with the given strategy. |
||
235 | * |
||
236 | * @throws BadRouteException |
||
237 | * @return mixed |
||
238 | */ |
||
239 | |||
240 | 2 | private function callWithStrategy() |
|
254 | |||
255 | /** |
||
256 | * @return Collector |
||
257 | */ |
||
258 | |||
259 | public function getCollector() |
||
263 | |||
264 | /** |
||
265 | * @return string |
||
266 | */ |
||
267 | |||
268 | public function getMethod() |
||
272 | |||
273 | /** |
||
274 | * @return string |
||
275 | */ |
||
276 | |||
277 | 22 | public function getPattern() |
|
281 | |||
282 | /** |
||
283 | * @return string[] |
||
284 | */ |
||
285 | |||
286 | public function getSegments() |
||
290 | |||
291 | /** |
||
292 | * @return string|array|\Closure |
||
293 | */ |
||
294 | |||
295 | 6 | public function getAction() |
|
299 | |||
300 | /** |
||
301 | * @return string |
||
302 | */ |
||
303 | |||
304 | public function getNamespace() |
||
308 | |||
309 | /** |
||
310 | * @return string[] |
||
311 | */ |
||
312 | |||
313 | 22 | public function getParams() |
|
317 | |||
318 | /** |
||
319 | * @param string $key |
||
320 | * @return string |
||
321 | */ |
||
322 | |||
323 | public function getParam($key) |
||
327 | |||
328 | /** |
||
329 | * @return array |
||
330 | */ |
||
331 | |||
332 | public function getDefaults() |
||
336 | |||
337 | /** |
||
338 | * @param string $key |
||
339 | * @return mixed |
||
340 | */ |
||
341 | |||
342 | public function getDefault($key) |
||
346 | |||
347 | /** |
||
348 | * @return array |
||
349 | */ |
||
350 | |||
351 | public function getMetadataArray() |
||
355 | |||
356 | /** |
||
357 | * @param string $key |
||
358 | * @return mixed |
||
359 | */ |
||
360 | |||
361 | public function getMetadata($key) |
||
365 | |||
366 | /** |
||
367 | * @return string|null |
||
368 | */ |
||
369 | |||
370 | public function getStrategy() |
||
378 | |||
379 | /** |
||
380 | * @inheritdoc |
||
381 | */ |
||
382 | |||
383 | public function getMatcher() |
||
387 | |||
388 | /** |
||
389 | * Verify if a Route have already been blocked. |
||
390 | * |
||
391 | * @return boolean |
||
392 | */ |
||
393 | |||
394 | 22 | public function getBlock() |
|
398 | |||
399 | /** |
||
400 | * Blocking a route indicate that that route have been selected and |
||
401 | * parsed, now it will be given to the matcher. |
||
402 | * |
||
403 | * @param bool $blocked |
||
404 | * @return self |
||
405 | */ |
||
406 | |||
407 | 22 | public function setBlock($blocked) |
|
412 | |||
413 | /** |
||
414 | * @param string $method |
||
415 | * @return Route |
||
416 | */ |
||
417 | |||
418 | public function setMethod($method) |
||
424 | |||
425 | /** |
||
426 | * @param string $pattern |
||
427 | * @return Route |
||
428 | */ |
||
429 | |||
430 | 3 | public function setPattern($pattern) |
|
436 | |||
437 | /** |
||
438 | * @param string $pattern |
||
439 | * @return self |
||
440 | */ |
||
441 | |||
442 | 22 | public function setPatternWithoutReset($pattern) |
|
447 | |||
448 | /** |
||
449 | * @param string $action |
||
450 | * @return self |
||
451 | */ |
||
452 | |||
453 | public function setAction($action) |
||
458 | |||
459 | /** |
||
460 | * @param string $namespace |
||
461 | * @return self |
||
462 | */ |
||
463 | |||
464 | public function setNamespace($namespace) |
||
469 | |||
470 | /** |
||
471 | * @param string[] $params |
||
472 | * @return self |
||
473 | */ |
||
474 | |||
475 | 22 | public function setParams(array $params) |
|
480 | |||
481 | /** |
||
482 | * @param string $key |
||
483 | * @param string $value |
||
484 | * |
||
485 | * @return self |
||
486 | */ |
||
487 | |||
488 | public function setParam($key, $value) |
||
493 | |||
494 | /** |
||
495 | * @param mixed[] $defaults |
||
496 | * @return self |
||
497 | */ |
||
498 | |||
499 | 3 | public function setDefaults(array $defaults) |
|
504 | |||
505 | /** |
||
506 | * @param string $key |
||
507 | * @param mixed $value |
||
508 | * |
||
509 | * @return self |
||
510 | */ |
||
511 | |||
512 | public function setDefault($key, $value) |
||
517 | |||
518 | /** |
||
519 | * @param mixed[] $metadata |
||
520 | * @return self |
||
521 | */ |
||
522 | |||
523 | 3 | public function setMetadataArray(array $metadata) |
|
528 | |||
529 | /** |
||
530 | * @param string $key |
||
531 | * @param mixed $value |
||
532 | * |
||
533 | * @return $this |
||
534 | */ |
||
535 | |||
536 | public function setMetadata($key, $value) |
||
541 | |||
542 | /** |
||
543 | * @param null|string|StrategyInterface $strategy |
||
544 | * @return self |
||
545 | */ |
||
546 | |||
547 | 12 | public function setStrategy($strategy) |
|
552 | |||
553 | /** |
||
554 | * @inheritdoc |
||
555 | */ |
||
556 | |||
557 | 20 | public function setMatcher(Matcher $matcher) |
|
562 | |||
563 | /** |
||
564 | * Set a constraint to a token in the route pattern. |
||
565 | * |
||
566 | * @param string $token |
||
567 | * @param string $regex |
||
568 | * |
||
569 | * @return self |
||
570 | */ |
||
571 | |||
572 | 1 | public function setConstraint($token, $regex) |
|
586 | |||
587 | /** |
||
588 | * Set a function to create controllers. |
||
589 | * |
||
590 | * @param string|array|\Closure $callable |
||
591 | * @throws BadRouteException |
||
592 | * @return self |
||
593 | */ |
||
594 | |||
595 | 1 | public function setControllerCreationFunction($callable) |
|
604 | |||
605 | /** |
||
606 | * @param string $key |
||
607 | * @return bool |
||
608 | */ |
||
609 | |||
610 | public function hasParam($key) |
||
614 | |||
615 | /** |
||
616 | * @param string $key |
||
617 | * @return bool |
||
618 | */ |
||
619 | |||
620 | public function hasDefault($key) |
||
624 | |||
625 | /** |
||
626 | * @param string $key |
||
627 | * @return bool |
||
628 | */ |
||
629 | |||
630 | public function hasMetadata($key) |
||
634 | |||
635 | } |
||
636 |