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 null|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 null|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 | 28 | 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 | * Blocking a route indicate that that route have been selected and |
||
152 | * parsed, now it will be given to the matcher. |
||
153 | * |
||
154 | * @return self |
||
155 | */ |
||
156 | |||
157 | 17 | public function block() |
|
162 | |||
163 | /** |
||
164 | * Verify if a Route have already been blocked. |
||
165 | * |
||
166 | * @return boolean |
||
167 | */ |
||
168 | |||
169 | 17 | public function blocked() |
|
173 | |||
174 | /** |
||
175 | * Execute the route action, if no strategy was provided the action |
||
176 | * will be executed by the call_user_func PHP function. |
||
177 | * |
||
178 | * @throws BadRouteException |
||
179 | * @return mixed |
||
180 | */ |
||
181 | |||
182 | 5 | public function call() |
|
196 | |||
197 | /** |
||
198 | * Seek for dynamic content on callables. eg. routes action controller#action |
||
199 | * syntax allow to use the variables to build the string like: {controller}@{action} |
||
200 | * |
||
201 | * @param string|array|\Closure $callable |
||
202 | * @return string|array|\Closure |
||
203 | */ |
||
204 | |||
205 | 5 | private function parseCallable($callable) |
|
220 | |||
221 | /** |
||
222 | * Get the controller object. |
||
223 | * |
||
224 | * @param string $controller |
||
225 | * @return Object |
||
226 | */ |
||
227 | |||
228 | 2 | private function parseCallableController($controller) |
|
236 | |||
237 | /** |
||
238 | * Parse and replace dynamic content on route action. |
||
239 | * |
||
240 | * @param string $fragment Part of callable |
||
241 | * @return string |
||
242 | */ |
||
243 | |||
244 | 2 | private function parseCallablePlaceholders($fragment) |
|
256 | |||
257 | /** |
||
258 | * Execute the route action with the given strategy. |
||
259 | * |
||
260 | * @throws BadRouteException |
||
261 | * @return mixed |
||
262 | */ |
||
263 | |||
264 | private function callWithStrategy() |
||
277 | |||
278 | /** |
||
279 | * @return Collector |
||
280 | */ |
||
281 | |||
282 | public function getCollector() |
||
286 | |||
287 | /** |
||
288 | * @return string |
||
289 | */ |
||
290 | |||
291 | public function getMethod() |
||
295 | |||
296 | /** |
||
297 | * @return string |
||
298 | */ |
||
299 | |||
300 | 17 | public function getPattern() |
|
304 | |||
305 | /** |
||
306 | * @return string[] |
||
307 | */ |
||
308 | |||
309 | public function getSegments() |
||
313 | |||
314 | /** |
||
315 | * @return string|array|\Closure |
||
316 | */ |
||
317 | |||
318 | 5 | public function getAction() |
|
322 | |||
323 | /** |
||
324 | * @return string |
||
325 | */ |
||
326 | |||
327 | public function getNamespace() |
||
331 | |||
332 | /** |
||
333 | * @return string[] |
||
334 | */ |
||
335 | |||
336 | 17 | public function getParams() |
|
340 | |||
341 | /** |
||
342 | * @param string $key |
||
343 | * @return string |
||
344 | */ |
||
345 | |||
346 | public function getParam($key) |
||
350 | |||
351 | /** |
||
352 | * @return array |
||
353 | */ |
||
354 | |||
355 | public function getDefaults() |
||
359 | |||
360 | /** |
||
361 | * @param string $key |
||
362 | * @return mixed |
||
363 | */ |
||
364 | |||
365 | public function getDefault($key) |
||
369 | |||
370 | /** |
||
371 | * @return array |
||
372 | */ |
||
373 | |||
374 | public function getMetadataArray() |
||
378 | |||
379 | /** |
||
380 | * @param string $key |
||
381 | * @return mixed |
||
382 | */ |
||
383 | |||
384 | public function getMetadata($key) |
||
388 | |||
389 | /** |
||
390 | * @return string|null |
||
391 | */ |
||
392 | |||
393 | public function getStrategy() |
||
401 | |||
402 | /** |
||
403 | * @inheritdoc |
||
404 | */ |
||
405 | |||
406 | public function getMatcher() |
||
410 | |||
411 | /** |
||
412 | * @param string $method |
||
413 | * @return Route |
||
414 | */ |
||
415 | |||
416 | public function setMethod($method) |
||
422 | |||
423 | /** |
||
424 | * @param string $pattern |
||
425 | * @return Route |
||
426 | */ |
||
427 | |||
428 | 3 | public function setPattern($pattern) |
|
434 | |||
435 | /** |
||
436 | * @param string $pattern |
||
437 | * @return self |
||
438 | */ |
||
439 | |||
440 | 17 | public function setPatternWithoutReset($pattern) |
|
445 | |||
446 | /** |
||
447 | * @param string $action |
||
448 | * @return self |
||
449 | */ |
||
450 | |||
451 | public function setAction($action) |
||
456 | |||
457 | /** |
||
458 | * @param string $namespace |
||
459 | * @return self |
||
460 | */ |
||
461 | |||
462 | public function setNamespace($namespace) |
||
467 | |||
468 | /** |
||
469 | * @param string[] $params |
||
470 | * @return self |
||
471 | */ |
||
472 | |||
473 | 17 | public function setParams(array $params) |
|
478 | |||
479 | /** |
||
480 | * @param string $key |
||
481 | * @param string $value |
||
482 | * |
||
483 | * @return self |
||
484 | */ |
||
485 | |||
486 | public function setParam($key, $value) |
||
491 | |||
492 | /** |
||
493 | * @param mixed[] $defaults |
||
494 | * @return self |
||
495 | */ |
||
496 | |||
497 | 3 | public function setDefaults(array $defaults) |
|
502 | |||
503 | /** |
||
504 | * @param string $key |
||
505 | * @param mixed $value |
||
506 | * |
||
507 | * @return self |
||
508 | */ |
||
509 | |||
510 | public function setDefault($key, $value) |
||
515 | |||
516 | /** |
||
517 | * @param mixed[] $metadata |
||
518 | * @return self |
||
519 | */ |
||
520 | |||
521 | 3 | public function setMetadataArray(array $metadata) |
|
526 | |||
527 | /** |
||
528 | * @param string $key |
||
529 | * @param mixed $value |
||
530 | * |
||
531 | * @return $this |
||
532 | */ |
||
533 | |||
534 | public function setMetadata($key, $value) |
||
539 | |||
540 | /** |
||
541 | * @param string|StrategyInterface $strategy |
||
542 | * @return self |
||
543 | */ |
||
544 | |||
545 | 10 | public function setStrategy($strategy) |
|
550 | |||
551 | /** |
||
552 | * @inheritdoc |
||
553 | */ |
||
554 | |||
555 | 15 | public function setMatcher(Matcher $matcher) |
|
559 | |||
560 | /** |
||
561 | * Set a function to create controllers. |
||
562 | * |
||
563 | * @param string|array|\Closure $callable |
||
564 | * @throws BadRouteException |
||
565 | * @return self |
||
566 | */ |
||
567 | |||
568 | 1 | public function setControllerCreationFunction($callable) |
|
577 | |||
578 | /** |
||
579 | * @param string $key |
||
580 | * @return bool |
||
581 | */ |
||
582 | |||
583 | public function hasParam($key) |
||
587 | |||
588 | /** |
||
589 | * @param string $key |
||
590 | * @return bool |
||
591 | */ |
||
592 | |||
593 | public function hasDefault($key) |
||
597 | |||
598 | /** |
||
599 | * @param string $key |
||
600 | * @return bool |
||
601 | */ |
||
602 | |||
603 | public function hasMetadata($key) |
||
607 | |||
608 | } |
||
609 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.