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 |
||
| 11 | class Route |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var string $name a name for this route. |
||
| 15 | * @var string $info description of route. |
||
| 16 | * @var string|array $method the method(s) to support |
||
| 17 | * @var string $rule the path rule for this route |
||
| 18 | * @var callable $action the callback to handle this route |
||
| 19 | * @var null|array $arguments arguments for the callback, extracted |
||
| 20 | * from path |
||
| 21 | */ |
||
| 22 | private $name; |
||
| 23 | private $info; |
||
| 24 | private $method; |
||
| 25 | private $rule; |
||
| 26 | private $action; |
||
| 27 | private $arguments = []; |
||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * Set values for route. |
||
| 33 | * |
||
| 34 | * @param null|string $rule for this route |
||
| 35 | * @param callable $action callable to implement a controller for |
||
| 36 | * the route |
||
| 37 | * @param null|string|array $method as request method to support |
||
| 38 | * @param null|string $info description of the route |
||
| 39 | * |
||
| 40 | * @return $this |
||
| 41 | */ |
||
| 42 | 43 | public function set($rule, $action = null, $method = null, $info = null) |
|
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * Check if part of route is a argument and optionally match type |
||
| 63 | * as a requirement {argument:type}. |
||
| 64 | * |
||
| 65 | * @param string $rulePart the rule part to check. |
||
| 66 | * @param string $queryPart the query part to check. |
||
| 67 | * @param array &$args add argument to args array if matched |
||
| 68 | * |
||
| 69 | * @return boolean |
||
| 70 | */ |
||
| 71 | 6 | private function checkPartAsArgument($rulePart, $queryPart, &$args) |
|
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * Check if value is matching a certain type of values. |
||
| 95 | * |
||
| 96 | * @param string $value the value to check. |
||
| 97 | * @param array $type the expected type to check against. |
||
| 98 | * |
||
| 99 | * @return boolean |
||
| 100 | */ |
||
| 101 | 2 | private function checkPartMatchingType($value, $type) |
|
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Check if value is matching a certain type and do type |
||
| 129 | * conversion accordingly. |
||
| 130 | * |
||
| 131 | * @param string $value the value to check. |
||
| 132 | * @param array $type the expected type to check against. |
||
| 133 | * |
||
| 134 | * @return boolean |
||
| 135 | */ |
||
| 136 | 6 | private function typeConvertArgument($value, $type) |
|
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * Match part of rule and query. |
||
| 152 | * |
||
| 153 | * @param string $rulePart the rule part to check. |
||
| 154 | * @param string $queryPart the query part to check. |
||
| 155 | * @param array &$args add argument to args array if matched |
||
| 156 | * |
||
| 157 | * @return boolean |
||
| 158 | */ |
||
| 159 | 35 | private function matchPart($rulePart, $queryPart, &$args) |
|
| 178 | |||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * Check if the request method matches. |
||
| 183 | * |
||
| 184 | * @param string $method as request method |
||
| 185 | * |
||
| 186 | * @return boolean true if request method matches |
||
| 187 | */ |
||
| 188 | 42 | public function matchRequestMethod($method) |
|
| 197 | |||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * Check if the route matches a query and request method. |
||
| 202 | * |
||
| 203 | * @param string $query to match against |
||
| 204 | * @param string $method as request method |
||
| 205 | * |
||
| 206 | * @return boolean true if query matches the route |
||
| 207 | */ |
||
| 208 | 42 | public function match($query, $method = null) |
|
| 243 | |||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * Handle the action for the route. |
||
| 248 | * |
||
| 249 | * @param string $di container with services |
||
| 250 | * |
||
| 251 | * @return mixed |
||
| 252 | */ |
||
| 253 | 32 | public function handle($di = null) |
|
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * Set the name of the route. |
||
| 292 | * |
||
| 293 | * @param string $name set a name for the route |
||
| 294 | * |
||
| 295 | * @return $this |
||
| 296 | */ |
||
| 297 | public function setName($name) |
||
| 302 | |||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * Get information of the route. |
||
| 307 | * |
||
| 308 | * @return null|string as route information. |
||
| 309 | */ |
||
| 310 | public function getInfo() |
||
| 314 | |||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Get the rule for the route. |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | 30 | public function getRule() |
|
| 326 | |||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * Get the request method for the route. |
||
| 331 | * |
||
| 332 | * @return string representing the request method supported |
||
| 333 | */ |
||
| 334 | public function getRequestMethod() |
||
| 342 | } |
||
| 343 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: