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:
Complex classes like EE_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 EE_Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class EE_Request implements InterminableInterface |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * $_GET parameters |
||
21 | * |
||
22 | * @var array $_get |
||
23 | */ |
||
24 | private $_get; |
||
25 | |||
26 | /** |
||
27 | * $_POST parameters |
||
28 | * |
||
29 | * @var array $_post |
||
30 | */ |
||
31 | private $_post; |
||
32 | |||
33 | /** |
||
34 | * $_COOKIE parameters |
||
35 | * |
||
36 | * @var array $_cookie |
||
37 | */ |
||
38 | private $_cookie; |
||
39 | |||
40 | /** |
||
41 | * $_REQUEST parameters |
||
42 | * |
||
43 | * @var array $_params |
||
44 | */ |
||
45 | private $_params; |
||
46 | |||
47 | /** |
||
48 | * whether current request is for the admin but NOT via AJAX |
||
49 | * |
||
50 | * @var boolean $admin |
||
51 | */ |
||
52 | public $admin = false; |
||
53 | |||
54 | /** |
||
55 | * whether current request is via AJAX |
||
56 | * |
||
57 | * @var boolean $ajax |
||
58 | */ |
||
59 | public $ajax = false; |
||
60 | |||
61 | /** |
||
62 | * whether current request is via AJAX from the frontend of the site |
||
63 | * |
||
64 | * @var boolean $front_ajax |
||
65 | */ |
||
66 | public $front_ajax = false; |
||
67 | |||
68 | /** |
||
69 | * IP address for request |
||
70 | * |
||
71 | * @var string $_ip_address |
||
72 | */ |
||
73 | private $_ip_address; |
||
74 | |||
75 | |||
76 | |||
77 | /** |
||
78 | * class constructor |
||
79 | * |
||
80 | * @access public |
||
81 | * @param array $get |
||
82 | * @param array $post |
||
83 | * @param array $cookie |
||
84 | */ |
||
85 | public function __construct(array $get, array $post, array $cookie) |
||
101 | |||
102 | |||
103 | |||
104 | /** |
||
105 | * @return array |
||
106 | */ |
||
107 | public function get_params() |
||
111 | |||
112 | |||
113 | |||
114 | /** |
||
115 | * @return array |
||
116 | */ |
||
117 | public function post_params() |
||
121 | |||
122 | |||
123 | |||
124 | /** |
||
125 | * @return array |
||
126 | */ |
||
127 | public function cookie_params() |
||
131 | |||
132 | |||
133 | |||
134 | /** |
||
135 | * returns contents of $_REQUEST |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | public function params() |
||
143 | |||
144 | |||
145 | |||
146 | /** |
||
147 | * @param $key |
||
148 | * @param $value |
||
149 | * @param bool $override_ee |
||
150 | * @return void |
||
151 | */ |
||
152 | public function set($key, $value, $override_ee = false) |
||
163 | |||
164 | |||
165 | |||
166 | /** |
||
167 | * returns the value for a request param if the given key exists |
||
168 | * |
||
169 | * @param $key |
||
170 | * @param null|mixed $default |
||
171 | * @return mixed |
||
172 | */ |
||
173 | public function get($key, $default = null) |
||
177 | |||
178 | |||
179 | |||
180 | /** |
||
181 | * check if param exists |
||
182 | * |
||
183 | * @param $key |
||
184 | * @return bool |
||
185 | */ |
||
186 | public function is_set($key) |
||
190 | |||
191 | |||
192 | /** |
||
193 | * check if a request parameter exists whose key that matches the supplied wildcard pattern |
||
194 | * and return the value for the first match found |
||
195 | * wildcards can be either of the following: |
||
196 | * ? to represent a single character of any type |
||
197 | * * to represent one or more characters of any type |
||
198 | * |
||
199 | * @param string $pattern |
||
200 | * @param null|mixed $default |
||
201 | * @return false|int |
||
202 | */ |
||
203 | public function getMatch($pattern, $default = null) |
||
207 | |||
208 | |||
209 | /** |
||
210 | * check if a request parameter exists whose key matches the supplied wildcard pattern |
||
211 | * wildcards can be either of the following: |
||
212 | * ? to represent a single character of any type |
||
213 | * * to represent one or more characters of any type |
||
214 | * returns true if a match is found or false if not |
||
215 | * |
||
216 | * @param string $pattern |
||
217 | * @return false|int |
||
218 | */ |
||
219 | public function matches($pattern) |
||
223 | |||
224 | |||
225 | /** |
||
226 | * @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard |
||
227 | * @param string $pattern A string including wildcards to be converted to a regex pattern |
||
228 | * and used to search through the current request's parameter keys |
||
229 | * @param array $request_params The array of request parameters to search through |
||
230 | * @param mixed $default [optional] The value to be returned if no match is found. |
||
231 | * Default is null |
||
232 | * @param string $return [optional] Controls what kind of value is returned. |
||
233 | * Options are: |
||
234 | * 'bool' will return true or false if match is found or not |
||
235 | * 'key' will return the first key found that matches the supplied pattern |
||
236 | * 'value' will return the value for the first request parameter |
||
237 | * whose key matches the supplied pattern |
||
238 | * Default is 'value' |
||
239 | * @return boolean|string |
||
240 | */ |
||
241 | private function match($pattern, array $request_params, $default = null, $return = 'value') |
||
265 | |||
266 | /** |
||
267 | * the supplied key can be a simple string to represent a "top-level" request parameter |
||
268 | * or represent a key for a request parameter that is nested deeper within the request parameter array, |
||
269 | * by using square brackets to surround keys for deeper array elements. |
||
270 | * For example : |
||
271 | * if the supplied $key was: "first[second][third]" |
||
272 | * then this will attempt to drill down into the request parameter array to find a value. |
||
273 | * Given the following request parameters: |
||
274 | * array( |
||
275 | * 'first' => array( |
||
276 | * 'second' => array( |
||
277 | * 'third' => 'has a value' |
||
278 | * ) |
||
279 | * ) |
||
280 | * ) |
||
281 | * would return true if default parameters were set |
||
282 | * |
||
283 | * @param string $callback |
||
284 | * @param $key |
||
285 | * @param null $default |
||
286 | * @param array $request_params |
||
287 | * @return bool|mixed|null |
||
288 | */ |
||
289 | private function request_parameter_drill_down( |
||
337 | |||
338 | |||
339 | |||
340 | /** |
||
341 | * remove param |
||
342 | * |
||
343 | * @param $key |
||
344 | * @param bool $unset_from_global_too |
||
345 | */ |
||
346 | public function un_set($key, $unset_from_global_too = false) |
||
353 | |||
354 | |||
355 | |||
356 | /** |
||
357 | * @return string |
||
358 | */ |
||
359 | public function ip_address() |
||
363 | |||
364 | |||
365 | /** |
||
366 | * @return bool |
||
367 | */ |
||
368 | public function isAdmin() |
||
372 | |||
373 | |||
374 | /** |
||
375 | * @return mixed |
||
376 | */ |
||
377 | public function isAjax() |
||
381 | |||
382 | |||
383 | /** |
||
384 | * @return mixed |
||
385 | */ |
||
386 | public function isFrontAjax() |
||
390 | |||
391 | |||
392 | |||
393 | /** |
||
394 | * _visitor_ip |
||
395 | * attempt to get IP address of current visitor from server |
||
396 | * plz see: http://stackoverflow.com/a/2031935/1475279 |
||
397 | * |
||
398 | * @access public |
||
399 | * @return string |
||
400 | */ |
||
401 | View Code Duplication | private function _visitor_ip() |
|
424 | |||
425 | |||
426 | |||
427 | } |
||
428 | // End of file EE_Request.core.php |
||
430 |