Complex classes like Router 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 Router, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Router |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Holds the controller string. |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $controller = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Holds the current method. |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $method = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Holds the current action. |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $action = null; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Holds the current action. |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $name = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Holds all the actions (HTTP methods to CRUD verbs). |
||
| 50 | * TODO: refactor this!! |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | public static $actions = array( |
||
| 54 | 'POST' => 'onCreate', |
||
| 55 | 'GET' => 'onRead', |
||
| 56 | 'PUT' => 'onUpdate', |
||
| 57 | 'DELETE' => 'onDelete', |
||
| 58 | 'PATCH' => 'onModify', |
||
| 59 | 'OPTIONS' => 'onHelp', |
||
| 60 | 'HEAD' => 'onTest', |
||
| 61 | 'TRACE' => 'onTrace' |
||
| 62 | ); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Holds an array of router params |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $params = array(); |
||
| 69 | |||
| 70 | private $_rules = array(); |
||
| 71 | private $_defaults = array(); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The constructor, set the default routing rules. |
||
| 75 | * |
||
| 76 | * @param array $rules |
||
| 77 | * @param array $defaults |
||
| 78 | * @throws \InvalidArgumentException |
||
| 79 | */ |
||
| 80 | public function __construct(array $rules=array(), array $defaults=array()) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Sets the public properties e.g. controller, action, method and params |
||
| 102 | * in order as per the following: |
||
| 103 | * - router rules, |
||
| 104 | * - router params, |
||
| 105 | * - then router defauls. |
||
| 106 | * |
||
| 107 | * @param string $route |
||
|
|
|||
| 108 | * @param array $rules |
||
| 109 | * @param array $params |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | public function setAsProperties(array $rules, array $params) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Maps an URI against the routing table. |
||
| 132 | * |
||
| 133 | * @param string $uri |
||
| 134 | * @param array $params Additional params to merge with the current set (optional) |
||
| 135 | * @return void |
||
| 136 | */ |
||
| 137 | public function map($uri, array $params=null) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Tries to match a route to an URI params (version with REGEX). |
||
| 158 | * |
||
| 159 | * @param string $route |
||
| 160 | * @param string $uri |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | public function routeToParamsMatcher($route, $uri) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Sets the current action from a specified method |
||
| 204 | * or use the method in the current scope. |
||
| 205 | * |
||
| 206 | * @param string $method The method to set the action (optional) |
||
| 207 | * @return void |
||
| 208 | */ |
||
| 209 | public function setAction($method=null) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Returns the current action, or as specified. |
||
| 221 | * |
||
| 222 | * @param string $method A method key (optional) |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function getAction($method=null) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Returns all the actions. |
||
| 241 | * |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | public function getActions() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Sets the router's params. |
||
| 251 | * |
||
| 252 | * @param array $params |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | public function setParams(array $params) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Returns all the router's params. |
||
| 262 | * |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | public function getParams() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Sets the specified router param. |
||
| 272 | * |
||
| 273 | * @param string $key |
||
| 274 | * @param string $value |
||
| 275 | * @return void |
||
| 276 | */ |
||
| 277 | public function setParam($key, $value) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Returns the specified router param. |
||
| 284 | * |
||
| 285 | * @param string $key |
||
| 286 | * @return array |
||
| 287 | * @throws \InvalidArgumentException |
||
| 288 | */ |
||
| 289 | public function getParam($key) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Checks a specified router param exists. |
||
| 302 | * |
||
| 303 | * @param string $key A key to check |
||
| 304 | * @return bolean |
||
| 305 | */ |
||
| 306 | public function hasParam($key) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Sets the controller. |
||
| 313 | * |
||
| 314 | * @param string $controller |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | public function setController($controller) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns the current controller. |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function getController() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Sets the method. |
||
| 334 | * |
||
| 335 | * @param string $method |
||
| 336 | * @return void |
||
| 337 | */ |
||
| 338 | public function setMethod($method) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Returns the current method. |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | public function getMethod() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Returns, if set, the current route name. |
||
| 355 | * Alternatively, return the current path in scope. |
||
| 356 | * |
||
| 357 | * @return string|null |
||
| 358 | */ |
||
| 359 | public function getName() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Returns, if set, the current route path. |
||
| 366 | * |
||
| 367 | * @return string|null |
||
| 368 | */ |
||
| 369 | public function getPath() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Sets the route name. |
||
| 376 | * |
||
| 377 | * @param $name |
||
| 378 | * @return string|null |
||
| 379 | */ |
||
| 380 | public function setName($name) |
||
| 384 | |||
| 385 | } |
||
| 386 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.