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 |
||
| 9 | class Route |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var string $name a name for this route. |
||
| 13 | * @var string|array $method the method(s) to support |
||
| 14 | * @var string $rule the path rule for this route |
||
| 15 | * @var callable $action the callback to handle this route |
||
| 16 | * @var null|array $arguments arguments for the callback, extracted |
||
| 17 | * from path |
||
| 18 | */ |
||
| 19 | private $name; |
||
| 20 | private $method; |
||
| 21 | private $rule; |
||
| 22 | private $action; |
||
| 23 | private $arguments = []; |
||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * Set values for route. |
||
| 29 | * |
||
| 30 | * @param null|string $rule for this route |
||
| 31 | * @param callable $action callable to implement a controller for |
||
| 32 | * the route |
||
| 33 | * @param null|string|array $method as request method to support |
||
| 34 | * |
||
| 35 | * @return $this |
||
| 36 | */ |
||
| 37 | 37 | public function set($rule, $action = null, $method = null) |
|
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | /** |
||
| 53 | * Check if part of route is a argument and optionally match type |
||
| 54 | * as a requirement {argument:type}. |
||
| 55 | * |
||
| 56 | * @param string $rulePart the rule part to check. |
||
| 57 | * @param string $queryPart the query part to check. |
||
| 58 | * @param array &$args add argument to args array if matched |
||
| 59 | * |
||
| 60 | * @return boolean |
||
| 61 | */ |
||
| 62 | 7 | private function checkPartAsArgument($rulePart, $queryPart, &$args) |
|
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * Check if value is matching a certain type of values. |
||
| 85 | * |
||
| 86 | * @param string $value the value to check. |
||
| 87 | * @param array $type the expected type to check against. |
||
| 88 | * |
||
| 89 | * @return boolean |
||
| 90 | */ |
||
| 91 | private function checkPartMatchingType($value, $type) |
||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * Check if value is matching a certain type and do type |
||
| 119 | * conversion accordingly. |
||
| 120 | * |
||
| 121 | * @param string $value the value to check. |
||
| 122 | * @param array $type the expected type to check against. |
||
| 123 | * |
||
| 124 | * @return boolean |
||
| 125 | */ |
||
| 126 | private function typeConvertArgument($value, $type) |
||
| 137 | 7 | ||
| 138 | 7 | ||
| 139 | |||
| 140 | 29 | /** |
|
| 141 | 29 | * Match part of rule and query. |
|
| 142 | 29 | * |
|
| 143 | 29 | * @param string $rulePart the rule part to check. |
|
| 144 | 30 | * @param string $queryPart the query part to check. |
|
| 145 | * @param array &$args add argument to args array if matched |
||
| 146 | * |
||
| 147 | * @return boolean |
||
| 148 | */ |
||
| 149 | private function matchPart($rulePart, $queryPart, &$args) |
||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * Check if the request method matches. |
||
| 173 | * |
||
| 174 | * @param string $method as request method |
||
| 175 | * |
||
| 176 | 36 | * @return boolean true if request method matches |
|
| 177 | */ |
||
| 178 | 36 | public function matchRequestMethod($method) |
|
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | 30 | /** |
|
| 191 | 30 | * Check if the route matches a query and request method. |
|
| 192 | 30 | * |
|
| 193 | 30 | * @param string $query to match against |
|
| 194 | * @param string $method as request method |
||
| 195 | 30 | * |
|
| 196 | 30 | * @return boolean true if query matches the route |
|
| 197 | 30 | */ |
|
| 198 | public function match($query, $method = null) |
||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * Handle the action for the route. |
||
| 238 | * |
||
| 239 | * @param string $app container with services |
||
| 240 | * |
||
| 241 | * @return void |
||
| 242 | */ |
||
| 243 | public function handle($app = null) |
||
| 264 | |||
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * Set the name of the route. |
||
| 269 | * |
||
| 270 | * @param string $name set a name for the route |
||
| 271 | * |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | public function setName($name) |
||
| 279 | |||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * Get the rule for the route. |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function getRule() |
||
| 291 | |||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * Get the request method for the route. |
||
| 296 | * |
||
| 297 | * @return string representing the request method supported |
||
| 298 | */ |
||
| 299 | public function getRequestMethod() |
||
| 307 | } |
||
| 308 |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.