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 | /** @var array $data Data from request */ |
||
22 | protected $data; |
||
23 | |||
24 | |||
25 | /** |
||
26 | * Constructor Request |
||
27 | * |
||
28 | * @access public |
||
29 | * |
||
30 | * @result void |
||
31 | */ |
||
32 | public function __construct() |
||
36 | |||
37 | /** |
||
38 | * @inheritdoc |
||
39 | */ |
||
40 | public function isCli() |
||
44 | |||
45 | /** |
||
46 | * @inheritdoc |
||
47 | */ |
||
48 | public function isAjax() |
||
52 | |||
53 | /** |
||
54 | * @inheritdoc |
||
55 | */ |
||
56 | public function getMethod() |
||
60 | |||
61 | /** |
||
62 | * @inheritdoc |
||
63 | */ |
||
64 | public function getUserIP() |
||
68 | |||
69 | /** |
||
70 | * @inheritdoc |
||
71 | */ |
||
72 | public function getBrowser($agent = null) |
||
76 | |||
77 | /** |
||
78 | * Get arguments from command line |
||
79 | * |
||
80 | * @access public |
||
81 | * |
||
82 | * @param string $char -a .. -z option char |
||
83 | * @param string $name --optionName_string |
||
84 | * @param bool|null $required Required value? |
||
85 | * |
||
86 | * @return mixed |
||
87 | */ |
||
88 | public function getOption($char = '', $name = '', $required = null) |
||
117 | |||
118 | /** |
||
119 | * @inheritdoc |
||
120 | */ |
||
121 | public function getFiles($className = '\Micro\Web\Uploader') |
||
129 | |||
130 | /** |
||
131 | * @inheritdoc |
||
132 | */ |
||
133 | public function getStorage($name) |
||
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | */ |
||
141 | public function setStorage($name, array $data = []) |
||
145 | |||
146 | /** |
||
147 | * @inheritdoc |
||
148 | */ |
||
149 | public function query($name) |
||
153 | |||
154 | /** |
||
155 | * @inheritdoc |
||
156 | */ |
||
157 | public function getVar($name, $storage) |
||
161 | |||
162 | /** |
||
163 | * @inheritdoc |
||
164 | */ |
||
165 | public function post($name) |
||
169 | |||
170 | /** |
||
171 | * @inheritdoc |
||
172 | */ |
||
173 | public function cookie($name) |
||
177 | |||
178 | /** |
||
179 | * @inheritdoc |
||
180 | */ |
||
181 | public function session($name) |
||
185 | |||
186 | /** |
||
187 | * Get value by key from server storage |
||
188 | * |
||
189 | * @access public |
||
190 | * |
||
191 | * @param string $name Key name |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function server($name) |
||
199 | |||
200 | /** |
||
201 | * @inheritdoc |
||
202 | */ |
||
203 | public function setQuery($name, $value) |
||
207 | |||
208 | /** |
||
209 | * @inheritdoc |
||
210 | */ |
||
211 | public function setVar($name, $value, $storage) |
||
215 | |||
216 | /** |
||
217 | * @inheritdoc |
||
218 | */ |
||
219 | public function setPost($name, $value) |
||
223 | |||
224 | /** |
||
225 | * @inheritdoc |
||
226 | */ |
||
227 | public function setCookie($name, $value) |
||
231 | |||
232 | /** |
||
233 | * @inheritdoc |
||
234 | */ |
||
235 | public function setSession($name, $value) |
||
239 | |||
240 | /** |
||
241 | * @inheritdoc |
||
242 | */ |
||
243 | public function unsetQuery($name) |
||
247 | |||
248 | /** |
||
249 | * @inheritdoc |
||
250 | */ |
||
251 | public function unsetVar($name, $storage) |
||
255 | |||
256 | /** |
||
257 | * @inheritdoc |
||
258 | */ |
||
259 | public function unsetPost($name) |
||
263 | |||
264 | /** |
||
265 | * @inheritdoc |
||
266 | */ |
||
267 | public function unsetSession($name) |
||
271 | |||
272 | /** |
||
273 | * @inheritdoc |
||
274 | */ |
||
275 | public function getRequestPayload() |
||
279 | } |
||
280 |
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: