Complex classes like Router 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 Router, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Router implements |
||
14 | InjectionAwareInterface, |
||
15 | ConfigureInterface |
||
16 | { |
||
17 | use InjectionAwareTrait; |
||
18 | use Configure2Trait { |
||
19 | configure as protected configure2; |
||
20 | } |
||
21 | |||
22 | |||
23 | |||
24 | /** |
||
25 | * @var array $routes all the routes. |
||
26 | * @var array $internalRoutes all internal routes. |
||
27 | * @var null|string $lastRoute last route that was matched and called. |
||
28 | */ |
||
29 | private $routes = []; |
||
30 | private $internalRoutes = []; |
||
31 | private $lastRoute = null; |
||
32 | |||
33 | |||
34 | |||
35 | /** |
||
36 | * Load and apply configurations. |
||
37 | * |
||
38 | * @param array|string $what is an array with key/value config options |
||
39 | * or a file to be included which returns such |
||
40 | * an array. |
||
41 | * |
||
42 | * @return self |
||
43 | */ |
||
44 | public function configure($what) |
||
72 | |||
73 | |||
74 | |||
75 | /** |
||
76 | * Handle the routes and match them towards the request, dispatch them |
||
77 | * when a match is made. Each route handler may throw exceptions that |
||
78 | * may redirect to an internal route for error handling. |
||
79 | * Several routes can match and if the routehandler does not break |
||
80 | * execution flow, the route matching will carry on. |
||
81 | * Only the last routehandler will get its return value returned further. |
||
82 | * |
||
83 | * @param string $path the path to find a matching handler for. |
||
84 | * @param string $method the request method to match. |
||
85 | * |
||
86 | * @return mixed content returned from route. |
||
87 | */ |
||
88 | public function handle($path, $method = null) |
||
113 | |||
114 | |||
115 | |||
116 | /** |
||
117 | * Handle an internal route, the internal routes are not exposed to the |
||
118 | * end user. |
||
119 | * |
||
120 | * @param string $rule for this route. |
||
121 | * |
||
122 | * @return void |
||
123 | * |
||
124 | * @throws \Anax\Route\NotFoundException |
||
125 | */ |
||
126 | public function handleInternal($rule) |
||
135 | |||
136 | |||
137 | |||
138 | /** |
||
139 | * Load routes from a config file, the file should return an array with |
||
140 | * details of the routes. These details are used to create the routes. |
||
141 | * |
||
142 | * @param array $route details on the route. |
||
143 | * |
||
144 | * @return self |
||
145 | */ |
||
146 | public function load($route) |
||
183 | |||
184 | |||
185 | |||
186 | /** |
||
187 | * Add a route with a request method, a path rule to match and an action |
||
188 | * as the callback. Adding several path rules (array) results in several |
||
189 | * routes being created. |
||
190 | * |
||
191 | * @param null|string|array $method as a valid request method. |
||
192 | * @param null|string|array $rule path rule for this route. |
||
193 | * @param null|string|callable $action to implement a handler for the route. |
||
194 | * @param null|string $info about the route. |
||
195 | * |
||
196 | * @return class|array as new route(s), class if one added, else array. |
||
197 | */ |
||
198 | public function any($method, $rule, $action, $info = null) |
||
212 | |||
213 | |||
214 | |||
215 | /** |
||
216 | * Add a route to the router by rule(s) and a callback. |
||
217 | * |
||
218 | * @param null|string|array $rule for this route. |
||
219 | * @param null|string|callable $action a callback handler for the route. |
||
220 | * |
||
221 | * @return class|array as new route(s), class if one added, else array. |
||
222 | */ |
||
223 | public function add($rule, $action = null) |
||
227 | |||
228 | |||
229 | |||
230 | /** |
||
231 | * Add a default route which will be applied for any path. |
||
232 | * |
||
233 | * @param string|callable $action a callback handler for the route. |
||
234 | * |
||
235 | * @return class as new route. |
||
236 | */ |
||
237 | public function always($action) |
||
241 | |||
242 | |||
243 | |||
244 | /** |
||
245 | * Add a default route which will be applied for any path, if the choosen |
||
246 | * request method is matching. |
||
247 | * |
||
248 | * @param null|string|array $method as request methods |
||
249 | * @param null|string|callable $action a callback handler for the route. |
||
250 | * |
||
251 | * @return class|array as new route(s), class if one added, else array. |
||
252 | */ |
||
253 | public function all($method, $action) |
||
257 | |||
258 | |||
259 | |||
260 | /** |
||
261 | * Shortcut to add a GET route. |
||
262 | * |
||
263 | * @param null|string|array $method as request methods |
||
264 | * @param null|string|callable $action a callback handler for the route. |
||
265 | * |
||
266 | * @return class|array as new route(s), class if one added, else array. |
||
267 | */ |
||
268 | public function get($rule, $action) |
||
272 | |||
273 | |||
274 | |||
275 | /** |
||
276 | * Shortcut to add a POST route. |
||
277 | * |
||
278 | * @param null|string|array $method as request methods |
||
279 | * @param null|string|callable $action a callback handler for the route. |
||
280 | * |
||
281 | * @return class|array as new route(s), class if one added, else array. |
||
282 | */ |
||
283 | public function post($rule, $action) |
||
287 | |||
288 | |||
289 | |||
290 | /** |
||
291 | * Shortcut to add a PUT route. |
||
292 | * |
||
293 | * @param null|string|array $method as request methods |
||
294 | * @param null|string|callable $action a callback handler for the route. |
||
295 | * |
||
296 | * @return class|array as new route(s), class if one added, else array. |
||
297 | */ |
||
298 | public function put($rule, $action) |
||
302 | |||
303 | |||
304 | |||
305 | /** |
||
306 | * Shortcut to add a DELETE route. |
||
307 | * |
||
308 | * @param null|string|array $method as request methods |
||
309 | * @param null|string|callable $action a callback handler for the route. |
||
310 | * |
||
311 | * @return class|array as new route(s), class if one added, else array. |
||
312 | */ |
||
313 | public function delete($rule, $action) |
||
317 | |||
318 | |||
319 | |||
320 | /** |
||
321 | * Add an internal route to the router, this route is not exposed to the |
||
322 | * browser and the end user. |
||
323 | * |
||
324 | * @param string $rule for this route |
||
325 | * @param null|string|callable $action a callback handler for the route. |
||
326 | * |
||
327 | * @return class|array as new route(s), class if one added, else array. |
||
328 | */ |
||
329 | public function addInternal($rule, $action) |
||
336 | |||
337 | |||
338 | |||
339 | /** |
||
340 | * Get the route for the last route that was handled. |
||
341 | * |
||
342 | * @return mixed |
||
343 | */ |
||
344 | public function getLastRoute() |
||
348 | |||
349 | |||
350 | |||
351 | /** |
||
352 | * Get all routes. |
||
353 | * |
||
354 | * @return array with all routes. |
||
355 | */ |
||
356 | public function getAll() |
||
360 | |||
361 | |||
362 | |||
363 | /** |
||
364 | * Get all internal routes. |
||
365 | * |
||
366 | * @return array with internal routes. |
||
367 | */ |
||
368 | public function getInternal() |
||
372 | } |
||
373 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: