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 $info description of route. |
||
14 | * @var string|array $method the method(s) to support |
||
15 | * @var string $rule the path rule for this route |
||
16 | * @var callable $action the callback to handle this route |
||
17 | * @var null|array $arguments arguments for the callback, extracted |
||
18 | * from path |
||
19 | */ |
||
20 | private $name; |
||
21 | private $info; |
||
22 | private $method; |
||
23 | private $rule; |
||
24 | private $action; |
||
25 | private $arguments = []; |
||
26 | |||
27 | |||
28 | |||
29 | /** |
||
30 | * Set values for route. |
||
31 | * |
||
32 | * @param null|string $rule for this route |
||
33 | * @param callable $action callable to implement a controller for |
||
34 | * the route |
||
35 | * @param null|string|array $method as request method to support |
||
36 | * @param null|string $info description of the route |
||
37 | * |
||
38 | * @return $this |
||
39 | */ |
||
40 | 37 | public function set($rule, $action = null, $method = null, $info = null) |
|
56 | |||
57 | |||
58 | |||
59 | /** |
||
60 | * Check if part of route is a argument and optionally match type |
||
61 | * as a requirement {argument:type}. |
||
62 | * |
||
63 | * @param string $rulePart the rule part to check. |
||
64 | * @param string $queryPart the query part to check. |
||
65 | * @param array &$args add argument to args array if matched |
||
66 | * |
||
67 | * @return boolean |
||
68 | */ |
||
69 | 7 | private function checkPartAsArgument($rulePart, $queryPart, &$args) |
|
88 | |||
89 | |||
90 | |||
91 | /** |
||
92 | * Check if value is matching a certain type of values. |
||
93 | * |
||
94 | * @param string $value the value to check. |
||
95 | * @param array $type the expected type to check against. |
||
96 | * |
||
97 | * @return boolean |
||
98 | */ |
||
99 | 2 | private function checkPartMatchingType($value, $type) |
|
122 | |||
123 | |||
124 | |||
125 | /** |
||
126 | * Check if value is matching a certain type and do type |
||
127 | * conversion accordingly. |
||
128 | * |
||
129 | * @param string $value the value to check. |
||
130 | * @param array $type the expected type to check against. |
||
131 | * |
||
132 | * @return boolean |
||
133 | */ |
||
134 | 7 | private function typeConvertArgument($value, $type) |
|
145 | |||
146 | |||
147 | |||
148 | /** |
||
149 | * Match part of rule and query. |
||
150 | * |
||
151 | * @param string $rulePart the rule part to check. |
||
152 | * @param string $queryPart the query part to check. |
||
153 | * @param array &$args add argument to args array if matched |
||
154 | * |
||
155 | * @return boolean |
||
156 | */ |
||
157 | 30 | private function matchPart($rulePart, $queryPart, &$args) |
|
176 | |||
177 | |||
178 | |||
179 | /** |
||
180 | * Check if the request method matches. |
||
181 | * |
||
182 | * @param string $method as request method |
||
183 | * |
||
184 | * @return boolean true if request method matches |
||
185 | */ |
||
186 | 36 | public function matchRequestMethod($method) |
|
195 | |||
196 | |||
197 | |||
198 | /** |
||
199 | * Check if the route matches a query and request method. |
||
200 | * |
||
201 | * @param string $query to match against |
||
202 | * @param string $method as request method |
||
203 | * |
||
204 | * @return boolean true if query matches the route |
||
205 | */ |
||
206 | 36 | public function match($query, $method = null) |
|
241 | |||
242 | |||
243 | |||
244 | /** |
||
245 | * Handle the action for the route. |
||
246 | * |
||
247 | * @param string $di container with services |
||
248 | * |
||
249 | * @return mixed |
||
250 | */ |
||
251 | 26 | public function handle($di = null) |
|
274 | |||
275 | |||
276 | |||
277 | /** |
||
278 | * Set the name of the route. |
||
279 | * |
||
280 | * @param string $name set a name for the route |
||
281 | * |
||
282 | * @return $this |
||
283 | */ |
||
284 | public function setName($name) |
||
289 | |||
290 | |||
291 | |||
292 | /** |
||
293 | * Get information of the route. |
||
294 | * |
||
295 | * @return null|string as route information. |
||
296 | */ |
||
297 | public function getInfo() |
||
301 | |||
302 | |||
303 | |||
304 | /** |
||
305 | * Get the rule for the route. |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | 24 | public function getRule() |
|
313 | |||
314 | |||
315 | |||
316 | /** |
||
317 | * Get the request method for the route. |
||
318 | * |
||
319 | * @return string representing the request method supported |
||
320 | */ |
||
321 | public function getRequestMethod() |
||
329 | } |
||
330 |
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: