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 |
||
| 44 | class Route |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * Controllers Array |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $controllers = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Current controller |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $controller; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Current action |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $action; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Current URI string |
||
| 69 | * |
||
| 70 | * @var mixed |
||
| 71 | */ |
||
| 72 | protected $uri = ''; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Parsed URI Array |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $uris = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Parsed params |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $params = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Class constructor |
||
| 90 | * |
||
| 91 | * Initialize route class. |
||
| 92 | * |
||
| 93 | * @return void |
||
|
|
|||
| 94 | */ |
||
| 95 | 1 | public function __construct() |
|
| 96 | { |
||
| 97 | 1 | if (Container::get('request')->isCli()) { |
|
| 98 | 1 | $this->uri = $this->parseArgv(); |
|
| 99 | } else { |
||
| 100 | if (isset($_GET['_i'])) { |
||
| 101 | $_SERVER['PATH_INFO'] = $_GET['_i']; |
||
| 102 | } |
||
| 103 | |||
| 104 | $_SERVER['PATH_INFO'] = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] |
||
| 105 | : (isset($_SERVER['ORIG_PATH_INFO']) ? $_SERVER['ORIG_PATH_INFO'] |
||
| 106 | : (isset($_SERVER['REDIRECT_PATH_INFO']) ? $_SERVER['REDIRECT_PATH_INFO'] : '')); |
||
| 107 | |||
| 108 | $this->uri = $_SERVER['PATH_INFO']; |
||
| 109 | } |
||
| 110 | |||
| 111 | 1 | if (substr($this->uri, 0, 1) == '/') { |
|
| 112 | $this->uri = ltrim($this->uri, '/'); |
||
| 113 | } |
||
| 114 | |||
| 115 | 1 | if (trim($this->uri, '/') == '') { |
|
| 116 | $this->uri = '/'; |
||
| 117 | } |
||
| 118 | |||
| 119 | 1 | Hook::listen(__CLASS__); |
|
| 120 | 1 | } |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Map URL to controller and action |
||
| 124 | * |
||
| 125 | * @return void |
||
| 126 | * |
||
| 127 | * @throws \Kotori\Exception\RouteNotFoundException |
||
| 128 | * @throws \Kotori\Exception\NotFoundException |
||
| 129 | */ |
||
| 130 | public function dispatch() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Returns the controller name |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | * |
||
| 238 | * @throws \Kotori\Exception\NotFoundException |
||
| 239 | */ |
||
| 240 | public function getController() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Returns the action name |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | * |
||
| 256 | * @throws \Kotori\Exception\NotFoundException |
||
| 257 | */ |
||
| 258 | public function getAction() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Returns the request params |
||
| 271 | * |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | public function getParams() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Returns the URI |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public function getUri() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Parse Routes |
||
| 293 | * |
||
| 294 | * Matches any routes that may exist in URL_ROUTE array |
||
| 295 | * against the URI to determine if the class/method need to be remapped. |
||
| 296 | * |
||
| 297 | * @param string $uri |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | protected function parseRoutes($uri) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Parse CLI arguments |
||
| 349 | * |
||
| 350 | * Take each command line argument and assume it is a URI segment. |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | 1 | protected function parseArgv() |
|
| 355 | { |
||
| 356 | 1 | $args = array_slice($_SERVER['argv'], 1); |
|
| 357 | 1 | return $args ? implode('/', $args) : ''; |
|
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Build Full URL |
||
| 362 | * |
||
| 363 | * @param string $uri |
||
| 364 | * @param string $module |
||
| 365 | * @return string |
||
| 366 | * |
||
| 367 | * @throws \Kotori\Exception\ConfigException |
||
| 368 | */ |
||
| 369 | public function url($uri = '', $module = null) |
||
| 399 | |||
| 400 | } |
||
| 401 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.