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 /** MicroRequest */ |
||
17 | class Request implements IRequest |
||
18 | { |
||
19 | /** @var bool $cli Is running as CLI */ |
||
20 | protected $cli; |
||
21 | |||
22 | /** @var array $get $_GET from request */ |
||
23 | protected $get; |
||
24 | /** @var array $data $_POST from request */ |
||
25 | protected $data; |
||
26 | /** @var array $files $_FILES from request */ |
||
27 | protected $files; |
||
28 | /** @var array $options $_SERVER from request */ |
||
29 | protected $options; |
||
30 | |||
31 | |||
32 | /** |
||
33 | * Constructor Request |
||
34 | * |
||
35 | * @access public |
||
36 | * |
||
37 | * @param array $get |
||
38 | * @param array $post |
||
39 | * @param array $files |
||
40 | * @param array $server |
||
41 | * |
||
42 | * @result void |
||
43 | */ |
||
44 | public function __construct(array $get = [], array $post = [], array $files = [], array $server = []) |
||
53 | |||
54 | /** |
||
55 | * Get flag of running as CLI |
||
56 | * |
||
57 | * @access public |
||
58 | * |
||
59 | * @return bool |
||
60 | */ |
||
61 | public function isCli() |
||
65 | |||
66 | /** |
||
67 | * Check request is AJAX ? |
||
68 | * |
||
69 | * @access public |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function isAjax() |
||
77 | |||
78 | /** |
||
79 | * Get request method |
||
80 | * |
||
81 | * @access public |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | public function getMethod() |
||
89 | |||
90 | /** |
||
91 | * Get user IP-address |
||
92 | * |
||
93 | * @access public |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | public function getUserIP() |
||
101 | |||
102 | /** |
||
103 | * Get browser data from user user agent string |
||
104 | * |
||
105 | * @access public |
||
106 | * |
||
107 | * @param null|string $agent User agent string |
||
108 | * |
||
109 | * @return mixed |
||
110 | */ |
||
111 | public function getBrowser($agent = null) |
||
115 | |||
116 | /** |
||
117 | * Get arguments from command line |
||
118 | * |
||
119 | * @access public |
||
120 | * |
||
121 | * @param string $char -a .. -z option char |
||
122 | * @param string $name --optionName_string |
||
123 | * @param bool|null $required Required value? |
||
124 | * |
||
125 | * @return mixed |
||
126 | */ |
||
127 | public function getOption($char = '', $name = '', $required = null) |
||
156 | |||
157 | /** |
||
158 | * Get files mapper |
||
159 | * |
||
160 | * @access public |
||
161 | * |
||
162 | * @param string $className Class name of mapper |
||
163 | * |
||
164 | * @return mixed |
||
165 | */ |
||
166 | public function getFiles($className = '\Micro\Web\Uploader') |
||
170 | |||
171 | /** |
||
172 | * Get value by key from query storage |
||
173 | * |
||
174 | * @access public |
||
175 | * |
||
176 | * @param string $name Key name |
||
177 | * @param integer $filter |
||
178 | * @param mixed $options |
||
179 | * |
||
180 | * @return bool |
||
181 | */ |
||
182 | public function query($name, $filter = FILTER_DEFAULT, $options = null) |
||
190 | |||
191 | /** |
||
192 | * Set query by key |
||
193 | * |
||
194 | * @access public |
||
195 | * |
||
196 | * @param string $name |
||
197 | * @param string|integer $value |
||
198 | * |
||
199 | * @return void |
||
200 | */ |
||
201 | public function setQuery($name, $value) |
||
205 | |||
206 | /** |
||
207 | * Get value by key from post storage |
||
208 | * |
||
209 | * @access public |
||
210 | * |
||
211 | * @param string $name Key name |
||
212 | * @param integer $filter |
||
213 | * @param mixed $options |
||
214 | * |
||
215 | * @return mixed |
||
216 | */ |
||
217 | public function post($name, $filter = FILTER_DEFAULT, $options = null) |
||
221 | |||
222 | /** |
||
223 | * Get value by key from cookie storage |
||
224 | * |
||
225 | * @access public |
||
226 | * |
||
227 | * @param string $name Key name |
||
228 | * @param integer $filter |
||
229 | * @param mixed $options |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | public function cookie($name, $filter = FILTER_DEFAULT, $options = null) |
||
237 | |||
238 | /** |
||
239 | * Get value by key from session storage |
||
240 | * |
||
241 | * @access public |
||
242 | * |
||
243 | * @param string $name Key name |
||
244 | * @param integer $filter |
||
245 | * @param mixed $options |
||
246 | * |
||
247 | * @return mixed |
||
248 | */ |
||
249 | public function session($name, $filter = FILTER_DEFAULT, $options = null) |
||
257 | |||
258 | /** |
||
259 | * Unset value by key from session storage |
||
260 | * |
||
261 | * @access public |
||
262 | * |
||
263 | * @param string $name Key name |
||
264 | * |
||
265 | * @return void |
||
266 | */ |
||
267 | public function unsetSession($name) |
||
273 | |||
274 | |||
275 | /** |
||
276 | * Get value by key from server storage |
||
277 | * |
||
278 | * @access public |
||
279 | * |
||
280 | * @param string $name Key name |
||
281 | * @param integer $filter |
||
282 | * @param mixed $options |
||
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | public function server($name, $filter = FILTER_DEFAULT, $options = null) |
||
290 | |||
291 | /** |
||
292 | * Get RequestPayload (RAW DATA) |
||
293 | * |
||
294 | * @return string|bool |
||
295 | */ |
||
296 | public function requestPayload() |
||
300 | |||
301 | /** |
||
302 | * Set value into session storage |
||
303 | * |
||
304 | * @access public |
||
305 | * |
||
306 | * @param string $name Key name |
||
307 | * @param string $value Key value |
||
308 | * |
||
309 | * @return void |
||
310 | */ |
||
311 | public function setSession($name, $value) |
||
315 | } |
||
316 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: