Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
13 | class Router implements |
||
14 | InjectionAwareInterface, |
||
15 | ConfigureInterface |
||
16 | { |
||
17 | use InjectionAwareTrait; |
||
18 | use ConfigureTrait { |
||
19 | configure as protected loadConfiguration; |
||
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 | * @throws Anax\Configure\Exception when template file is missing |
||
43 | * |
||
44 | * @return string as path to the template file |
||
45 | */ |
||
46 | public function configure($what) |
||
55 | |||
56 | |||
57 | |||
58 | /** |
||
59 | * Handle the routes and match them towards the request, dispatch them |
||
60 | * when a match is made. Each route handler may throw exceptions that |
||
61 | * may redirect to an internal route for error handling. |
||
62 | * Several routes can match and if the routehandler does not break |
||
63 | * execution flow, the route matching will carry on. |
||
64 | * Only the last routehandler will get its return value returned further. |
||
65 | * |
||
66 | * @param string $path the path to find a matching handler for. |
||
67 | * @param string $method the request method to match. |
||
68 | * |
||
69 | * @return mixed content returned from route. |
||
70 | */ |
||
71 | public function handle($path, $method = null) |
||
72 | { |
||
73 | try { |
||
74 | $match = false; |
||
75 | foreach ($this->routes as $route) { |
||
76 | View Code Duplication | if ($route->match($path, $method)) { |
|
77 | $this->lastRoute = $route->getRule(); |
||
78 | $match = true; |
||
79 | $results = $route->handle($this->di); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | if ($match) { |
||
84 | return $results; |
||
85 | } |
||
86 | |||
87 | $this->handleInternal("404"); |
||
88 | } catch (ForbiddenException $e) { |
||
89 | $this->handleInternal("403"); |
||
90 | } catch (NotFoundException $e) { |
||
91 | $this->handleInternal("404"); |
||
92 | } catch (InternalErrorException $e) { |
||
93 | $this->handleInternal("500"); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | |||
98 | |||
99 | /** |
||
100 | * Handle an internal route, the internal routes are not exposed to the |
||
101 | * end user. |
||
102 | * |
||
103 | * @param string $rule for this route. |
||
104 | * |
||
105 | * @return void |
||
106 | * |
||
107 | * @throws \Anax\Route\NotFoundException |
||
108 | */ |
||
109 | public function handleInternal($rule) |
||
118 | |||
119 | |||
120 | |||
121 | /** |
||
122 | * Load routes from a config file, the file should return an array with |
||
123 | * details of the routes. These details are used to create the routes. |
||
124 | * |
||
125 | * @param array $route details on the route. |
||
126 | * |
||
127 | * @return self |
||
128 | */ |
||
129 | public function load($route) |
||
154 | |||
155 | |||
156 | |||
157 | /** |
||
158 | * Add a route with a request method, a path rule to match and an action |
||
159 | * as the callback. Adding several path rules (array) results in several |
||
160 | * routes being created. |
||
161 | * |
||
162 | * @param null|string|array $method as a valid request method. |
||
163 | * @param null|string|array $rule path rule for this route. |
||
164 | * @param null|string|callable $action to implement a handler for the route. |
||
165 | * @param null|string $info about the route. |
||
166 | * |
||
167 | * @return class|array as new route(s), class if one added, else array. |
||
168 | */ |
||
169 | public function any($method, $rule, $action, $info = null) |
||
183 | |||
184 | |||
185 | |||
186 | /** |
||
187 | * Add a route to the router by rule(s) and a callback. |
||
188 | * |
||
189 | * @param null|string|array $rule for this route. |
||
190 | * @param null|string|callable $action a callback handler for the route. |
||
191 | * |
||
192 | * @return class|array as new route(s), class if one added, else array. |
||
193 | */ |
||
194 | public function add($rule, $action = null) |
||
198 | |||
199 | |||
200 | |||
201 | /** |
||
202 | * Add a default route which will be applied for any path. |
||
203 | * |
||
204 | * @param string|callable $action a callback handler for the route. |
||
205 | * |
||
206 | * @return class as new route. |
||
207 | */ |
||
208 | public function always($action) |
||
212 | |||
213 | |||
214 | |||
215 | /** |
||
216 | * Add a default route which will be applied for any path, if the choosen |
||
217 | * request method is matching. |
||
218 | * |
||
219 | * @param null|string|array $method as request methods |
||
220 | * @param null|string|callable $action a callback handler for the route. |
||
221 | * |
||
222 | * @return class|array as new route(s), class if one added, else array. |
||
223 | */ |
||
224 | public function all($method, $action) |
||
228 | |||
229 | |||
230 | |||
231 | /** |
||
232 | * Shortcut to add a GET route. |
||
233 | * |
||
234 | * @param null|string|array $method as request methods |
||
235 | * @param null|string|callable $action a callback handler for the route. |
||
236 | * |
||
237 | * @return class|array as new route(s), class if one added, else array. |
||
238 | */ |
||
239 | public function get($rule, $action) |
||
243 | |||
244 | |||
245 | |||
246 | /** |
||
247 | * Shortcut to add a POST route. |
||
248 | * |
||
249 | * @param null|string|array $method as request methods |
||
250 | * @param null|string|callable $action a callback handler for the route. |
||
251 | * |
||
252 | * @return class|array as new route(s), class if one added, else array. |
||
253 | */ |
||
254 | public function post($rule, $action) |
||
258 | |||
259 | |||
260 | |||
261 | /** |
||
262 | * Shortcut to add a PUT route. |
||
263 | * |
||
264 | * @param null|string|array $method as request methods |
||
265 | * @param null|string|callable $action a callback handler for the route. |
||
266 | * |
||
267 | * @return class|array as new route(s), class if one added, else array. |
||
268 | */ |
||
269 | public function put($rule, $action) |
||
273 | |||
274 | |||
275 | |||
276 | /** |
||
277 | * Shortcut to add a DELETE route. |
||
278 | * |
||
279 | * @param null|string|array $method as request methods |
||
280 | * @param null|string|callable $action a callback handler for the route. |
||
281 | * |
||
282 | * @return class|array as new route(s), class if one added, else array. |
||
283 | */ |
||
284 | public function delete($rule, $action) |
||
288 | |||
289 | |||
290 | |||
291 | /** |
||
292 | * Add an internal route to the router, this route is not exposed to the |
||
293 | * browser and the end user. |
||
294 | * |
||
295 | * @param string $rule for this route |
||
296 | * @param null|string|callable $action a callback handler for the route. |
||
297 | * |
||
298 | * @return class|array as new route(s), class if one added, else array. |
||
299 | */ |
||
300 | public function addInternal($rule, $action) |
||
307 | |||
308 | |||
309 | |||
310 | /** |
||
311 | * Get the route for the last route that was handled. |
||
312 | * |
||
313 | * @return mixed |
||
314 | */ |
||
315 | public function getLastRoute() |
||
319 | |||
320 | |||
321 | |||
322 | /** |
||
323 | * Get all routes. |
||
324 | * |
||
325 | * @return array with all routes. |
||
326 | */ |
||
327 | public function getAll() |
||
331 | |||
332 | |||
333 | |||
334 | /** |
||
335 | * Get all internal routes. |
||
336 | * |
||
337 | * @return array with internal routes. |
||
338 | */ |
||
339 | public function getInternal() |
||
343 | } |
||
344 |
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: