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; |
||
93 | |||
94 | /** |
||
95 | * The matcher that dispatched this route. |
||
96 | * |
||
97 | * @var Matcher $matcher |
||
98 | */ |
||
99 | |||
100 | protected $matcher; |
||
101 | |||
102 | /** |
||
103 | * @param Collector $collector |
||
104 | * @param string $method |
||
105 | * @param string $pattern |
||
106 | * @param string|array|\Closure $action |
||
107 | */ |
||
108 | |||
109 | public function __construct(Collector $collector, $method, $pattern, $action) |
||
122 | |||
123 | /** |
||
124 | * Clone this route and set it into the collector. |
||
125 | * |
||
126 | * @return Route |
||
127 | */ |
||
128 | |||
129 | public function reset() |
||
135 | |||
136 | /** |
||
137 | * Remove this route from the collector. |
||
138 | * |
||
139 | * @return self |
||
140 | */ |
||
141 | |||
142 | public function forget() |
||
147 | |||
148 | /** |
||
149 | * Blocking a route indicate that that route have been selected and |
||
150 | * parsed, now it will be given to the matcher. |
||
151 | * |
||
152 | * @return self |
||
153 | */ |
||
154 | |||
155 | public function block() |
||
160 | |||
161 | /** |
||
162 | * Verify if a Route have already been blocked. |
||
163 | * |
||
164 | * @return boolean |
||
165 | */ |
||
166 | |||
167 | public function blocked() |
||
171 | |||
172 | /** |
||
173 | * Execute the route action, if no strategy was provided the action |
||
174 | * will be executed by the call_user_func PHP function. |
||
175 | * |
||
176 | * @throws BadRouteException |
||
177 | * @return mixed |
||
178 | */ |
||
179 | |||
180 | public function call() |
||
203 | |||
204 | /** |
||
205 | * Seek for dynamic content on callables. eg. routes action controller#action |
||
206 | * syntax allow to use the variables to build the string like: {controller}@{action} |
||
207 | * |
||
208 | * @param string|array|\Closure $callable |
||
209 | * @return string|array|\Closure |
||
210 | */ |
||
211 | |||
212 | private function parseCallable($callable) |
||
227 | |||
228 | /** |
||
229 | * Parse and replace dynamic content on route action. |
||
230 | * |
||
231 | * @param string $fragment Part of callable |
||
232 | * @return string |
||
233 | */ |
||
234 | |||
235 | private function parseCallablePlaceholders($fragment) |
||
247 | |||
248 | /** |
||
249 | * @return Collector |
||
250 | */ |
||
251 | |||
252 | public function getCollector() |
||
256 | |||
257 | /** |
||
258 | * @return string |
||
259 | */ |
||
260 | |||
261 | public function getMethod() |
||
265 | |||
266 | /** |
||
267 | * @return string |
||
268 | */ |
||
269 | |||
270 | public function getPattern() |
||
274 | |||
275 | /** |
||
276 | * @return string[] |
||
277 | */ |
||
278 | |||
279 | public function getSegments() |
||
283 | |||
284 | /** |
||
285 | * @return string|array|\Closure |
||
286 | */ |
||
287 | |||
288 | public function getAction() |
||
292 | |||
293 | /** |
||
294 | * @return string |
||
295 | */ |
||
296 | |||
297 | public function getNamespace() |
||
301 | |||
302 | /** |
||
303 | * @return string[] |
||
304 | */ |
||
305 | |||
306 | public function getParams() |
||
310 | |||
311 | /** |
||
312 | * @param string $key |
||
313 | * @return string |
||
314 | */ |
||
315 | |||
316 | public function getParam($key) |
||
320 | |||
321 | /** |
||
322 | * @return array |
||
323 | */ |
||
324 | |||
325 | public function getDefaults() |
||
329 | |||
330 | /** |
||
331 | * @param string $key |
||
332 | * @return mixed |
||
333 | */ |
||
334 | |||
335 | public function getDefault($key) |
||
339 | |||
340 | /** |
||
341 | * @return array |
||
342 | */ |
||
343 | |||
344 | public function getMetadataArray() |
||
348 | |||
349 | /** |
||
350 | * @param string $key |
||
351 | * @return mixed |
||
352 | */ |
||
353 | |||
354 | public function getMetadata($key) |
||
358 | |||
359 | /** |
||
360 | * @return string|null |
||
361 | */ |
||
362 | |||
363 | public function getStrategy() |
||
371 | |||
372 | /** |
||
373 | * @inheritdoc |
||
374 | */ |
||
375 | |||
376 | public function getMatcher() |
||
380 | |||
381 | /** |
||
382 | * @param string $method |
||
383 | * @return Route |
||
384 | */ |
||
385 | |||
386 | public function setMethod($method) |
||
392 | |||
393 | /** |
||
394 | * @param string $pattern |
||
395 | * @return Route |
||
396 | */ |
||
397 | |||
398 | public function setPattern($pattern) |
||
404 | |||
405 | /** |
||
406 | * @param string $pattern |
||
407 | * @return self |
||
408 | */ |
||
409 | |||
410 | public function setPatternWithoutReset($pattern) |
||
415 | |||
416 | /** |
||
417 | * @param string $action |
||
418 | * @return self |
||
419 | */ |
||
420 | |||
421 | public function setAction($action) |
||
426 | |||
427 | /** |
||
428 | * @param string $namespace |
||
429 | * @return self |
||
430 | */ |
||
431 | |||
432 | public function setNamespace($namespace) |
||
437 | |||
438 | /** |
||
439 | * @param string[] $params |
||
440 | * @return self |
||
441 | */ |
||
442 | |||
443 | public function setParams(array $params) |
||
448 | |||
449 | /** |
||
450 | * @param string $key |
||
451 | * @param string $value |
||
452 | * |
||
453 | * @return self |
||
454 | */ |
||
455 | |||
456 | public function setParam($key, $value) |
||
461 | |||
462 | /** |
||
463 | * @param mixed[] $defaults |
||
464 | * @return self |
||
465 | */ |
||
466 | |||
467 | public function setDefaults(array $defaults) |
||
472 | |||
473 | /** |
||
474 | * @param string $key |
||
475 | * @param mixed $value |
||
476 | * |
||
477 | * @return self |
||
478 | */ |
||
479 | |||
480 | public function setDefault($key, $value) |
||
485 | |||
486 | /** |
||
487 | * @param mixed[] $metadata |
||
488 | * @return self |
||
489 | */ |
||
490 | |||
491 | public function setMetadataArray(array $metadata) |
||
496 | |||
497 | /** |
||
498 | * @param string $key |
||
499 | * @param mixed $value |
||
500 | * |
||
501 | * @return $this |
||
502 | */ |
||
503 | |||
504 | public function setMetadata($key, $value) |
||
509 | |||
510 | /** |
||
511 | * @param string|StrategyInterface $strategy |
||
512 | * @return self |
||
513 | */ |
||
514 | |||
515 | public function setStrategy($strategy) |
||
520 | |||
521 | /** |
||
522 | * @inheritdoc |
||
523 | */ |
||
524 | |||
525 | public function setMatcher(Matcher $matcher) |
||
529 | |||
530 | /** |
||
531 | * @param string $key |
||
532 | * @return bool |
||
533 | */ |
||
534 | |||
535 | public function hasParam($key) |
||
539 | |||
540 | /** |
||
541 | * @param string $key |
||
542 | * @return bool |
||
543 | */ |
||
544 | |||
545 | public function hasDefault($key) |
||
549 | |||
550 | /** |
||
551 | * @param string $key |
||
552 | * @return bool |
||
553 | */ |
||
554 | |||
555 | public function hasMetadata($key) |
||
559 | |||
560 | } |
||
561 |