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 string|array $method as request method to support |
||
35 | * @param string $path for this route |
||
36 | * @param string|array|callable $handler for this path, callable or equal |
||
37 | * @param string $info description of the route |
||
38 | * |
||
39 | * @return $this |
||
40 | */ |
||
41 | 11 | public function set( |
|
62 | |||
63 | |||
64 | |||
65 | /** |
||
66 | * Check if part of route is a argument and optionally match type |
||
67 | * as a requirement {argument:type}. |
||
68 | * |
||
69 | * @param string $rulePart the rule part to check. |
||
70 | * @param string $queryPart the query part to check. |
||
71 | * @param array &$args add argument to args array if matched |
||
72 | * |
||
73 | * @return boolean |
||
74 | */ |
||
75 | private function checkPartAsArgument($rulePart, $queryPart, &$args) |
||
94 | |||
95 | |||
96 | |||
97 | /** |
||
98 | * Check if value is matching a certain type of values. |
||
99 | * |
||
100 | * @param string $value the value to check. |
||
101 | * @param array $type the expected type to check against. |
||
102 | * |
||
103 | * @return boolean |
||
104 | */ |
||
105 | private function checkPartMatchingType($value, $type) |
||
128 | |||
129 | |||
130 | |||
131 | /** |
||
132 | * Check if value is matching a certain type and do type |
||
133 | * conversion accordingly. |
||
134 | * |
||
135 | * @param string $value the value to check. |
||
136 | * @param array $type the expected type to check against. |
||
137 | * |
||
138 | * @return boolean |
||
139 | */ |
||
140 | private function typeConvertArgument($value, $type) |
||
151 | |||
152 | |||
153 | |||
154 | /** |
||
155 | * Match part of rule and query. |
||
156 | * |
||
157 | * @param string $rulePart the rule part to check. |
||
158 | * @param string $queryPart the query part to check. |
||
159 | * @param array &$args add argument to args array if matched |
||
160 | * |
||
161 | * @return boolean |
||
162 | */ |
||
163 | private function matchPart($rulePart, $queryPart, &$args) |
||
182 | |||
183 | |||
184 | |||
185 | /** |
||
186 | * Check if the request method matches. |
||
187 | * |
||
188 | * @param string $method as request method |
||
189 | * |
||
190 | * @return boolean true if request method matches |
||
191 | */ |
||
192 | 11 | public function matchRequestMethod($method) |
|
201 | |||
202 | |||
203 | |||
204 | /** |
||
205 | * Check if the route matches a query and request method. |
||
206 | * |
||
207 | * @param string $query to match against |
||
208 | * @param string $method as request method |
||
209 | * |
||
210 | * @return boolean true if query matches the route |
||
211 | */ |
||
212 | 11 | public function match($query, $method = null) |
|
247 | |||
248 | |||
249 | |||
250 | /** |
||
251 | * Handle the action for the route. |
||
252 | * |
||
253 | * @param string $di container with services |
||
254 | * |
||
255 | * @return mixed |
||
256 | */ |
||
257 | public function handle($di = null) |
||
291 | |||
292 | |||
293 | |||
294 | /** |
||
295 | * Set the name of the route. |
||
296 | * |
||
297 | * @param string $name set a name for the route |
||
298 | * |
||
299 | * @return $this |
||
300 | */ |
||
301 | public function setName($name) |
||
306 | |||
307 | |||
308 | |||
309 | /** |
||
310 | * Get information of the route. |
||
311 | * |
||
312 | * @return null|string as route information. |
||
313 | */ |
||
314 | public function getInfo() |
||
318 | |||
319 | |||
320 | |||
321 | /** |
||
322 | * Get the rule for the route. |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | public function getRule() |
||
330 | |||
331 | |||
332 | |||
333 | /** |
||
334 | * Get the request method for the route. |
||
335 | * |
||
336 | * @return string representing the request method supported |
||
337 | */ |
||
338 | public function getRequestMethod() |
||
346 | } |
||
347 |
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: