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 |
||
| 8 | class Route |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * head |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | const HEAD = 'HEAD'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * get |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | const GET = 'GET'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * post |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | const POST = 'POST'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * put |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | const PUT = 'PUT'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * patch |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | const PATCH = 'PATCH'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * delete |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | const DELETE = 'DELETE'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * purge |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | const PURGE = 'PURGE'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * options |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | const OPTIONS = 'OPTIONS'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * trace |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | const TRACE = 'TRACE'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * connect |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | const CONNECT = 'CONNECT'; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The route name |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $name; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Path |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $path; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Action |
||
| 84 | * @var mixed |
||
| 85 | */ |
||
| 86 | protected $action; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Schemes |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $schemes = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Host |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | protected $host; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Methods |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | protected $methods = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Requirements |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | protected $requirements = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Defaults |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | protected $defaults= []; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Parameters |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | protected $parameters = []; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * The computed parameters |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | protected $computedParameters = []; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * whether the route has been compiled |
||
| 132 | * @var bool |
||
| 133 | */ |
||
| 134 | protected $isCompiled = false; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * the host regex |
||
| 138 | * @var string |
||
| 139 | */ |
||
| 140 | protected $hostRegex; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * the path regex |
||
| 144 | * @var string |
||
| 145 | */ |
||
| 146 | protected $pathRegex; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Array of host variables |
||
| 150 | * @var array |
||
| 151 | */ |
||
| 152 | protected $hostVariables = []; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Array of path variables |
||
| 156 | * @var array |
||
| 157 | */ |
||
| 158 | protected $pathVariables = []; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Array of variables |
||
| 162 | * @var array |
||
| 163 | */ |
||
| 164 | protected $variables = []; |
||
| 165 | |||
| 166 | public function __construct($path, $action) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Sets route name |
||
| 173 | * @param string $name |
||
| 174 | * @return Route |
||
| 175 | */ |
||
| 176 | public function setName($name) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function getName() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Sets the route path |
||
| 192 | * @param string $path |
||
| 193 | * @return Route |
||
| 194 | */ |
||
| 195 | public function setPath($path) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Gets the route path |
||
| 203 | * @return string path |
||
| 204 | */ |
||
| 205 | public function getPath() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Gets the path regex |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getPathRegex() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Sets the action for the route |
||
| 221 | * @param mixed $action |
||
| 222 | * @return Route |
||
| 223 | */ |
||
| 224 | public function setAction($action) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Gets the action |
||
| 232 | * @return mixed |
||
| 233 | */ |
||
| 234 | public function getAction() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Sets the route schemes |
||
| 241 | * @param string|array $schemes |
||
| 242 | * @return Route |
||
| 243 | */ |
||
| 244 | public function setSchemes($schemes) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Checks whether the route has the scheme |
||
| 252 | * @param string $scheme |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | public function hasScheme($scheme) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Gets all schemes |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public function getSchemes() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Sets the route request methods |
||
| 271 | * @param string|array $methods |
||
| 272 | * @return Route |
||
| 273 | */ |
||
| 274 | public function setMethods($methods) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Checks whether the route has the request method |
||
| 282 | * @param string $method |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | public function hasMethod($method) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Gets all request methods that the route supports |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function getMethods() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Set the route host |
||
| 301 | * @param string $host |
||
| 302 | * @return Route |
||
| 303 | */ |
||
| 304 | public function setHost($host) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Gets the host |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function getHost() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Gets the route host regex |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | public function getHostRegex() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Sets the requirements of the route |
||
| 330 | * @param array $requirements |
||
| 331 | * @return Route |
||
| 332 | */ |
||
| 333 | public function setRequirements(array $requirements) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Sets a requirement by specified name and value |
||
| 341 | * @param string $name |
||
| 342 | * @param string $requirement |
||
| 343 | * @return Route |
||
| 344 | */ |
||
| 345 | public function setRequirement($name, $requirement) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Add the requirements of the route (not replace) |
||
| 353 | * @param array $requirements |
||
| 354 | * @return Route |
||
| 355 | */ |
||
| 356 | public function addRequirements(array $requirements) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Gets all requirements |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | public function getRequirements() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Checks whether the route has the requirement |
||
| 373 | * @param string $name |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | public function hasRequirement($name) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Gets a requirement by its name |
||
| 383 | * @param string $name |
||
| 384 | * @param string $default |
||
| 385 | * @return string|null |
||
| 386 | */ |
||
| 387 | public function getRequirement($name, $default = null) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Gets the defaults |
||
| 394 | */ |
||
| 395 | public function getDefaults() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Sets a default item |
||
| 402 | * @param string $name |
||
| 403 | * @param string $value |
||
| 404 | * @return Route |
||
| 405 | */ |
||
| 406 | public function setDefault($name, $value) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Sets the defaults |
||
| 414 | * @param array $defaults |
||
| 415 | * @return Route |
||
| 416 | */ |
||
| 417 | public function setDefaults(array $defaults) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Add the defaults of the route (not replace) |
||
| 425 | * @param array $defaults |
||
| 426 | * @return Route |
||
| 427 | */ |
||
| 428 | public function addDefaults(array $defaults) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Gets the default option value by its name |
||
| 436 | * @param string $name |
||
| 437 | * @return mixed|null |
||
| 438 | */ |
||
| 439 | public function getDefault($name) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Checks whether the route has the default option |
||
| 446 | * @param string $name |
||
| 447 | * @return bool |
||
| 448 | */ |
||
| 449 | public function hasDefault($name) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Sets the route parameters |
||
| 456 | * @param string $name |
||
| 457 | * @param mixed $parameter |
||
| 458 | * @return Route |
||
| 459 | */ |
||
| 460 | public function setParameter($name, $parameter) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Gets the route parameters |
||
| 468 | * @param string $name |
||
| 469 | * @return mixed |
||
| 470 | */ |
||
| 471 | public function getParameter($name) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Checks whether the parameter exists |
||
| 478 | * @param string $name |
||
| 479 | * @return mixed |
||
| 480 | */ |
||
| 481 | public function hasParameter($name) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Sets all parameters |
||
| 488 | * @param array $parameters |
||
| 489 | * @return Route |
||
| 490 | */ |
||
| 491 | public function setParameters(array $parameters) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Add the parameters of the route (not replace) |
||
| 499 | * @param array $parameters |
||
| 500 | * @return Route |
||
| 501 | */ |
||
| 502 | public function addParameters(array $parameters) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Gets all parameters |
||
| 510 | * @return array |
||
| 511 | */ |
||
| 512 | public function getParameters() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @param array $computedParameters |
||
| 519 | */ |
||
| 520 | public function setComputedParameters($computedParameters) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @return array |
||
| 527 | */ |
||
| 528 | public function getComputedParameters() |
||
| 532 | |||
| 533 | /** |
||
| 534 | * {@inheritdoc} |
||
| 535 | */ |
||
| 536 | public function isCompiled() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Complies the route |
||
| 543 | * @param boolean $reCompile |
||
| 544 | * @return Route |
||
| 545 | */ |
||
| 546 | public function compile($reCompile = false) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Gets all variables that been compiled |
||
| 561 | * @return array |
||
| 562 | */ |
||
| 563 | public function getVariables() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Parses the path to regex |
||
| 570 | * @param string $path |
||
| 571 | * @param boolean $isHost |
||
| 572 | * @return string |
||
| 573 | */ |
||
| 574 | protected function parsePattern($path, $isHost) |
||
| 595 | } |