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) |
|
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * Check if part of route is a argument and optionally match type |
||
| 57 | * as a requirement {argument:type}. |
||
| 58 | * |
||
| 59 | * @param string $rulePart the rule part to check. |
||
| 60 | * @param string $queryPart the query part to check. |
||
| 61 | * @param array &$args add argument to args array if matched |
||
| 62 | * |
||
| 63 | * @return boolean |
||
| 64 | */ |
||
| 65 | 7 | private function checkPartAsArgument($rulePart, $queryPart, &$args) |
|
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Check if value is matching a certain type of values. |
||
| 89 | * |
||
| 90 | * @param string $value the value to check. |
||
| 91 | * @param array $type the expected type to check against. |
||
| 92 | * |
||
| 93 | * @return boolean |
||
| 94 | */ |
||
| 95 | 2 | private function checkPartMatchingType($value, $type) |
|
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * Check if value is matching a certain type and do type |
||
| 123 | * conversion accordingly. |
||
| 124 | * |
||
| 125 | * @param string $value the value to check. |
||
| 126 | * @param array $type the expected type to check against. |
||
| 127 | * |
||
| 128 | * @return boolean |
||
| 129 | */ |
||
| 130 | 7 | private function typeConvertArgument($value, $type) |
|
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Match part of rule and query. |
||
| 146 | * |
||
| 147 | * @param string $rulePart the rule part to check. |
||
| 148 | * @param string $queryPart the query part to check. |
||
| 149 | * @param array &$args add argument to args array if matched |
||
| 150 | * |
||
| 151 | * @return boolean |
||
| 152 | */ |
||
| 153 | 30 | private function matchPart($rulePart, $queryPart, &$args) |
|
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * Check if the request method matches. |
||
| 177 | * |
||
| 178 | * @param string $method as request method |
||
| 179 | * |
||
| 180 | * @return boolean true if request method matches |
||
| 181 | */ |
||
| 182 | 36 | public function matchRequestMethod($method) |
|
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * Check if the route matches a query and request method. |
||
| 196 | * |
||
| 197 | * @param string $query to match against |
||
| 198 | * @param string $method as request method |
||
| 199 | * |
||
| 200 | * @return boolean true if query matches the route |
||
| 201 | */ |
||
| 202 | 36 | public function match($query, $method = null) |
|
| 237 | |||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * Handle the action for the route. |
||
| 242 | * |
||
| 243 | * @param string $di container with services |
||
| 244 | * |
||
| 245 | * @return mixed |
||
| 246 | */ |
||
| 247 | 26 | public function handle($di = null) |
|
| 270 | |||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * Set the name of the route. |
||
| 275 | * |
||
| 276 | * @param string $name set a name for the route |
||
| 277 | * |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function setName($name) |
||
| 285 | |||
| 286 | |||
| 287 | |||
| 288 | /** |
||
| 289 | * Get the name of the route. |
||
| 290 | * |
||
| 291 | 24 | * @return string as the route name. |
|
| 292 | */ |
||
| 293 | 24 | public function getName() |
|
| 297 | |||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * Get the rule for the route. |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public function getRule() |
||
| 309 | |||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * Get the request method for the route. |
||
| 314 | * |
||
| 315 | * @return string representing the request method supported |
||
| 316 | */ |
||
| 317 | public function getRequestMethod() |
||
| 325 | } |
||
| 326 |
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: