Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Request |
||
21 | { |
||
22 | /** @var string The HTTP method (commonly GET, POST, PUT, DELETE, HEAD) */ |
||
23 | public $method = ''; |
||
24 | |||
25 | /** |
||
26 | * @var array A numeric array containing valid URL parameters. For a route |
||
27 | * path like /users/*, a Request for /users/alganet should have an array |
||
28 | * equivalent to ['alganet'] |
||
29 | */ |
||
30 | public $params = array(); |
||
31 | |||
32 | /** @var AbstractRoute A route matched for this request */ |
||
33 | public $route; |
||
34 | |||
35 | /** @var string The called URI */ |
||
36 | public $uri = ''; |
||
37 | |||
38 | /** |
||
39 | * @param string $method The HTTP method |
||
40 | * @param string $uri The called URI |
||
41 | */ |
||
42 | 130 | public function __construct($method = null, $uri = null) |
|
43 | { |
||
44 | //Tries to infer request variables only if null |
||
45 | 130 | if (is_null($method)) { |
|
46 | 5 | $method = isset($_SERVER['REQUEST_METHOD']) |
|
47 | 5 | ? $_SERVER['REQUEST_METHOD'] |
|
48 | 5 | : 'GET'; |
|
49 | } |
||
50 | |||
51 | 130 | if (is_null($uri)) { |
|
52 | 6 | $uri = isset($_SERVER['REQUEST_URI']) |
|
53 | 6 | ? $_SERVER['REQUEST_URI'] |
|
54 | 6 | : '/'; |
|
55 | } |
||
56 | |||
57 | 130 | $uri = parse_url($uri, PHP_URL_PATH); |
|
58 | 130 | $this->uri = rtrim($uri, ' /'); //We always ignore the last / |
|
59 | 130 | $this->method = strtoupper($method); //normalizing the HTTP method |
|
60 | 130 | } |
|
61 | |||
62 | /** |
||
63 | * Converting this request to string dispatch |
||
64 | */ |
||
65 | 7 | public function __toString() |
|
69 | |||
70 | /** |
||
71 | * Declares an error handler for a single Router::errorRoute instance on the |
||
72 | * fly before dispatching the request, so the application can capture the |
||
73 | * errors. These are cleaned after dispatching by forwardErrors() |
||
74 | * |
||
75 | * @see Respect\Rest\Request::forwardErrors |
||
76 | * @see http://php.net/set_error_handler |
||
77 | * |
||
78 | * @return mixed The previous error handler |
||
79 | */ |
||
80 | 111 | protected function prepareForErrorForwards() |
|
92 | |||
93 | /** |
||
94 | * Iterates over routines to find instances of |
||
95 | * Respect\Rest\Routines\ProxyableBy and call them, forwarding if |
||
96 | * necessary |
||
97 | * |
||
98 | * @see Respect\Rest\Routines\ProxyableBy |
||
99 | * @see Respect\Rest\Request::routineCall |
||
100 | * |
||
101 | * @return mixed A route forwarding or false |
||
102 | */ |
||
103 | 111 | protected function processPreRoutines() |
|
125 | |||
126 | /** |
||
127 | * Iterates over routines to find instances of |
||
128 | * Respect\Rest\Routines\ProxyableThrough and call them, forwarding if |
||
129 | * necessary |
||
130 | * |
||
131 | * @see Respect\Rest\Routines\ProxyableThrough |
||
132 | * @see Respect\Rest\Request::routineCall |
||
133 | * |
||
134 | * @return mixed A route forwarding or false |
||
135 | */ |
||
136 | 105 | protected function processPosRoutines($response) |
|
165 | |||
166 | /** |
||
167 | * Restores the previous error handler if present then check error routes |
||
168 | * for logged errors, forwarding them or returning null silently |
||
169 | * |
||
170 | * @param mixed $errorHandler Some error handler (internal or external to |
||
171 | * Respect) |
||
172 | * |
||
173 | * @return mixed A route forwarding or a silent null |
||
174 | */ |
||
175 | 105 | protected function forwardErrors($errorHandler) |
|
191 | |||
192 | /** |
||
193 | * Does a catch-like operation on an exception based on previously |
||
194 | * declared instances from Router::exceptionRoute |
||
195 | * |
||
196 | * @param Exception $e Any exception |
||
197 | * |
||
198 | * @return mixed A route forwarding or a silent null |
||
199 | */ |
||
200 | 2 | protected function catchExceptions($e) |
|
215 | |||
216 | /** |
||
217 | * Does a catch-like operation on an error based on previously |
||
218 | * declared instances from Router::exceptionRoute |
||
219 | * |
||
220 | * @param Error $e Any exception |
||
221 | * |
||
222 | * @return mixed A route forwarding or a silent null |
||
223 | */ |
||
224 | protected function catchErrors($e) |
||
239 | |||
240 | /** |
||
241 | * Generates and returns the response from the current route |
||
242 | * |
||
243 | * @return string A response! |
||
244 | */ |
||
245 | 115 | public function response() |
|
293 | |||
294 | /** |
||
295 | * Calls a routine on the current route and returns its result |
||
296 | * |
||
297 | * @param string $type The name of the routine (accept, when, etc) |
||
298 | * @param string $method The method name (GET, HEAD, POST, etc) |
||
299 | * @param Routinable $routine Some routine instance |
||
300 | * @param array $params Params from the routine |
||
301 | * |
||
302 | * @return mixed Whatever the routine returns |
||
303 | */ |
||
304 | 54 | public function routineCall($type, $method, Routinable $routine, &$params) |
|
327 | |||
328 | /** |
||
329 | * Extracts a parameter value from the current route |
||
330 | * |
||
331 | * @param ReflectionFunctionAbstract $callback Any function reflection |
||
332 | * @param ReflectionParameter $routeParam Any parameter reflection |
||
333 | * @param array $params Request URI params |
||
334 | * |
||
335 | * @return mixed a value from the reflected param |
||
336 | */ |
||
337 | 10 | protected function extractRouteParam( |
|
358 | |||
359 | /** |
||
360 | * Forwards a route |
||
361 | * |
||
362 | * @param AbstractRoute $route Any route |
||
363 | * |
||
364 | * @return Response from the forwarded route |
||
365 | */ |
||
366 | 5 | public function forward(AbstractRoute $route) |
|
372 | } |
||
373 |
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: