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 |
||
48 | class Route implements RouteInterface |
||
49 | { |
||
50 | /** |
||
51 | * GET request |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | const GET = 'GET'; |
||
56 | /** |
||
57 | * POST request |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | const POST = 'POST'; |
||
62 | /** |
||
63 | * PATCH request |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | const PATCH = 'PATCH'; |
||
68 | /** |
||
69 | * DELETE request |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | const DELETE = 'DELETE'; |
||
74 | /** |
||
75 | * OPTIONS request |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | const OPTIONS = 'OPTIONS'; |
||
80 | /** |
||
81 | * HEAD request |
||
82 | * |
||
83 | * @var string |
||
84 | */ |
||
85 | const HEAD = 'HEAD'; |
||
86 | |||
87 | /** |
||
88 | * Allowed HTTP verbs |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected static $validVerbs = [ |
||
93 | self::GET => true, |
||
94 | self::POST => true, |
||
95 | self::PATCH => true, |
||
96 | self::DELETE => true, |
||
97 | self::OPTIONS => true, |
||
98 | self::HEAD => true |
||
99 | ]; |
||
100 | /** |
||
101 | * Route name |
||
102 | * |
||
103 | * @var string |
||
104 | */ |
||
105 | protected $name; |
||
106 | /** |
||
107 | * Route path |
||
108 | * |
||
109 | * @var string |
||
110 | */ |
||
111 | protected $path; |
||
112 | /** |
||
113 | * Route action |
||
114 | * |
||
115 | * @var string|\Callable|\Closure|array |
||
116 | */ |
||
117 | protected $action; |
||
118 | /** |
||
119 | * Route tokens |
||
120 | * |
||
121 | * @var array |
||
122 | */ |
||
123 | protected $tokens = []; |
||
124 | /** |
||
125 | * Route default values |
||
126 | * |
||
127 | * @var array |
||
128 | */ |
||
129 | protected $defaults = []; |
||
130 | /** |
||
131 | * Route wildcard name |
||
132 | * |
||
133 | * @var string|null |
||
134 | */ |
||
135 | protected $wildcard = null; |
||
136 | /** |
||
137 | * Route host |
||
138 | * |
||
139 | * @var string|null |
||
140 | */ |
||
141 | protected $host = null; |
||
142 | /** |
||
143 | * Allowed accept headers |
||
144 | * |
||
145 | * @var array |
||
146 | */ |
||
147 | protected $accepts = []; |
||
148 | /** |
||
149 | * Allowed HTTP verbs |
||
150 | * |
||
151 | * @var array |
||
152 | */ |
||
153 | protected $verbs = []; |
||
154 | /** |
||
155 | * Secure protocol behaviour |
||
156 | * |
||
157 | * @var boolean|null |
||
158 | */ |
||
159 | protected $secure = false; |
||
160 | /** |
||
161 | * Authentication parameters |
||
162 | * |
||
163 | * @var mixed |
||
164 | */ |
||
165 | protected $auth = null; |
||
166 | /** |
||
167 | * Custom extra parameters |
||
168 | * |
||
169 | * @var array |
||
170 | */ |
||
171 | protected $extras = []; |
||
172 | /** |
||
173 | * Object route |
||
174 | * |
||
175 | * @var bool |
||
176 | */ |
||
177 | protected $object = false; |
||
178 | |||
179 | /** |
||
180 | * Route constructor |
||
181 | * |
||
182 | * @param string|array $verbs Allowed HTTP verbs |
||
183 | * @param string $name Route name |
||
184 | * @param string $path Route path |
||
185 | * @param string|\Callable|\Closure|array $action Route action |
||
186 | */ |
||
187 | 15 | public function __construct($verbs, $name, $path, $action) |
|
201 | |||
202 | /** |
||
203 | * Validate the allowed HTTP verbs |
||
204 | * |
||
205 | * @param string|array $verbs Allowed HTTP verbs |
||
206 | * @throws InvalidArgumentException If the HTTP verb list is empty |
||
207 | * @throws InvalidArgumentException If the HTTP verb is invalid |
||
208 | */ |
||
209 | 15 | protected function setAndValidateVerbs($verbs) |
|
232 | |||
233 | /** |
||
234 | * Set and validate the route name |
||
235 | * |
||
236 | * @param string $name Route name |
||
237 | * @throws InvalidArgumentException If the route name is empty |
||
238 | */ |
||
239 | 13 | protected function setAndValidateName($name) |
|
250 | |||
251 | /** |
||
252 | * Set and validate the route path |
||
253 | * |
||
254 | * @param string $path Route path |
||
255 | * @throws InvalidArgumentException If the route path is empty |
||
256 | */ |
||
257 | 12 | protected function setAndValidatePath($path) |
|
268 | |||
269 | /** |
||
270 | * Set and validate the route action |
||
271 | * |
||
272 | * @param string|\Callable|\Closure|array $actions Route actions |
||
273 | */ |
||
274 | 11 | protected function setAndValidateActionList($actions) |
|
279 | |||
280 | /** |
||
281 | * Get the route name |
||
282 | * |
||
283 | * @return string Route name |
||
284 | */ |
||
285 | 6 | public function getName() |
|
289 | |||
290 | /** |
||
291 | * Get the route path |
||
292 | * |
||
293 | * @return string Route path |
||
294 | */ |
||
295 | 6 | public function getPath() |
|
299 | |||
300 | /** |
||
301 | * Get the route action |
||
302 | * |
||
303 | * @return \Callable|\Closure|string|array Route action |
||
304 | */ |
||
305 | 6 | public function getAction() |
|
309 | |||
310 | /** |
||
311 | * Get the route tokens |
||
312 | * |
||
313 | * @return array Route tokens |
||
314 | */ |
||
315 | 6 | public function getTokens() |
|
319 | |||
320 | /** |
||
321 | * Set the route tokens |
||
322 | * |
||
323 | * @param array $tokens Route tokens |
||
324 | * @return Route Self reference |
||
325 | */ |
||
326 | 2 | public function setTokens(array $tokens) |
|
331 | |||
332 | /** |
||
333 | * Get the route defaults |
||
334 | * |
||
335 | * @return array Route defaults |
||
336 | */ |
||
337 | 6 | public function getDefaults() |
|
341 | |||
342 | /** |
||
343 | * Set the route defaults |
||
344 | * |
||
345 | * @param array $defaults Route defaults |
||
346 | * @return Route Self reference |
||
347 | */ |
||
348 | 1 | public function setDefaults(array $defaults) |
|
353 | |||
354 | /** |
||
355 | * Set the route wildcard name |
||
356 | * |
||
357 | * @return null|string |
||
358 | */ |
||
359 | 6 | public function getWildcard() |
|
363 | |||
364 | /** |
||
365 | * Get the route wildcard name |
||
366 | * |
||
367 | * @param null|string $wildcard Wildcard name |
||
368 | * @return Route Self reference |
||
369 | */ |
||
370 | 1 | public function setWildcard($wildcard) |
|
375 | |||
376 | /** |
||
377 | * Get the route host |
||
378 | * |
||
379 | * @return null|string Route host |
||
380 | */ |
||
381 | 6 | public function getHost() |
|
385 | |||
386 | /** |
||
387 | * Set the route host |
||
388 | * |
||
389 | * @param null|string $host Route host |
||
390 | * @return Route Self reference |
||
391 | */ |
||
392 | 1 | public function setHost($host) |
|
397 | |||
398 | /** |
||
399 | * Get the allowed Accept headers |
||
400 | * |
||
401 | * @return array Allowed Accept headers |
||
402 | */ |
||
403 | 6 | public function getAccepts() |
|
407 | |||
408 | /** |
||
409 | * Set the allowed Accept headers |
||
410 | * |
||
411 | * @param array $accepts Allowed Accept headers |
||
412 | * @return Route Self reference |
||
413 | */ |
||
414 | 2 | public function setAccepts(array $accepts) |
|
419 | |||
420 | /** |
||
421 | * Get the allowed HTTP verbs |
||
422 | * |
||
423 | * @return array Allowed HTTP verbs |
||
424 | */ |
||
425 | 6 | public function getVerbs() |
|
429 | |||
430 | /** |
||
431 | * Set the allowed HTTP verbs |
||
432 | * |
||
433 | * @param array $verbs Allowed HTTP verbs |
||
434 | * @return Route Self reference |
||
435 | */ |
||
436 | 1 | public function setVerbs(array $verbs) |
|
443 | |||
444 | /** |
||
445 | * Get the secure protocol mode |
||
446 | * |
||
447 | * @return boolean|null Secure protocol mode |
||
448 | */ |
||
449 | 6 | public function getSecure() |
|
453 | |||
454 | /** |
||
455 | * Set the secure protocol mode |
||
456 | * |
||
457 | * @param boolean|null $secure Secure protocol mode |
||
458 | * @return Route Self reference |
||
459 | */ |
||
460 | 1 | public function setSecure($secure) |
|
465 | |||
466 | /** |
||
467 | * Get the authentication information |
||
468 | * |
||
469 | * @return mixed Authentication information |
||
470 | */ |
||
471 | 6 | public function getAuth() |
|
475 | |||
476 | /** |
||
477 | * Set the authentication information |
||
478 | * |
||
479 | * @param mixed $auth Authentication information |
||
480 | * @return Route Self reference |
||
481 | */ |
||
482 | 1 | public function setAuth($auth) |
|
487 | |||
488 | /** |
||
489 | * Get the custom extra information |
||
490 | * |
||
491 | * @return array Custom extra information |
||
492 | */ |
||
493 | 6 | public function getExtras() |
|
497 | |||
498 | /** |
||
499 | * Set the custom extra information |
||
500 | * |
||
501 | * @param array $extras Custom extra information |
||
502 | * @return Route Self reference |
||
503 | */ |
||
504 | 1 | public function setExtras(array $extras) |
|
509 | |||
510 | /** |
||
511 | * Return whether this is an object route |
||
512 | * |
||
513 | * @return boolean Object route |
||
514 | */ |
||
515 | 6 | public function isObject() |
|
519 | |||
520 | /** |
||
521 | * Set whether this is an object route |
||
522 | * |
||
523 | * @param boolean $object Object route |
||
524 | * @return Route Self reference |
||
525 | */ |
||
526 | 2 | public function setObject($object) |
|
531 | |||
532 | /** |
||
533 | * Validate the route action |
||
534 | * |
||
535 | * @param string|\Callable|\Closure $action Route action |
||
536 | * @return string|\Callable|\Closure Validated route action |
||
537 | * @throws InvalidArgumentException If the route action is empty |
||
538 | * @throws InvalidArgumentException If the route action is not a class name |
||
539 | * @throws InvalidArgumentException If the route action is neither a callable nor an ActionInterface |
||
540 | */ |
||
541 | 11 | protected function validateAction($action) |
|
586 | } |
||
587 |