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 EEH_URL 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 EEH_URL, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class EEH_URL |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * _add_query_arg |
||
17 | * adds nonce to array of arguments then calls WP add_query_arg function |
||
18 | * |
||
19 | * @access public |
||
20 | * @param array $args |
||
21 | * @param string $url |
||
22 | * @param bool $exclude_nonce If true then the nonce will be excluded from the generated url. |
||
23 | * @return string |
||
24 | */ |
||
25 | public static function add_query_args_and_nonce($args = array(), $url = '', $exclude_nonce = false) |
||
68 | |||
69 | |||
70 | /** |
||
71 | * Returns whether not the remote file exists. |
||
72 | * Checking via GET because HEAD requests are blocked on some server configurations. |
||
73 | * |
||
74 | * @param string $url |
||
75 | * @param array $args the arguments that should be passed through to the wp_remote_request call. |
||
76 | * @return boolean |
||
77 | */ |
||
78 | public static function remote_file_exists($url, $args = array()) |
||
99 | |||
100 | |||
101 | /** |
||
102 | * refactor_url |
||
103 | * primarily used for removing the query string from a URL |
||
104 | * |
||
105 | * @param string $url |
||
106 | * @param bool $remove_query - TRUE (default) will strip off any URL params, ie: ?this=1&that=2 |
||
107 | * @param bool $base_url_only - TRUE will only return the scheme and host with no other parameters |
||
108 | * @return string |
||
109 | */ |
||
110 | public static function refactor_url($url = '', $remove_query = true, $base_url_only = false) |
||
135 | |||
136 | |||
137 | /** |
||
138 | * get_query_string |
||
139 | * returns just the query string from a URL, formatted by default into an array of key value pairs |
||
140 | * |
||
141 | * @param string $url |
||
142 | * @param bool $as_array TRUE (default) will return query params as an array of key value pairs, FALSE will |
||
143 | * simply return the query string |
||
144 | * @return string|array |
||
145 | */ |
||
146 | public static function get_query_string($url = '', $as_array = true) |
||
173 | |||
174 | |||
175 | /** |
||
176 | * prevent_prefetching |
||
177 | * |
||
178 | * @return void |
||
179 | */ |
||
180 | public static function prevent_prefetching() |
||
186 | |||
187 | |||
188 | /** |
||
189 | * This generates a unique site-specific string. |
||
190 | * An example usage for this string would be to save as a unique identifier for a record in the db for usage in |
||
191 | * urls. |
||
192 | * |
||
193 | * @param string $prefix Use this to prefix the string with something. |
||
194 | * @return string |
||
195 | */ |
||
196 | public static function generate_unique_token($prefix = '') |
||
201 | |||
202 | |||
203 | /** |
||
204 | * filter_input_server_url |
||
205 | * uses filter_input() to sanitize one of the INPUT_SERVER URL values |
||
206 | * but adds a backup in case filter_input() returns nothing, which can erringly happen on some servers |
||
207 | * |
||
208 | * @param string $server_variable |
||
209 | * @return string |
||
210 | */ |
||
211 | public static function filter_input_server_url($server_variable = 'REQUEST_URI') |
||
230 | |||
231 | |||
232 | /** |
||
233 | * Gets the current page's full URL. |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | View Code Duplication | public static function current_url() |
|
247 | |||
248 | |||
249 | /** |
||
250 | * Identical in functionality to EEH_current_url except it removes any provided query_parameters from it. |
||
251 | * |
||
252 | * @param array $query_parameters An array of query_parameters to remove from the current url. |
||
253 | * @since 4.9.46.rc.029 |
||
254 | * @return string |
||
255 | */ |
||
256 | public static function current_url_without_query_paramaters(array $query_parameters) |
||
260 | |||
261 | |||
262 | /** |
||
263 | * @param string $location |
||
264 | * @param int $status |
||
265 | * @param string $exit_notice |
||
266 | */ |
||
267 | public static function safeRedirectAndExit($location, $status = 302, $exit_notice = '') |
||
273 | |||
274 | /** |
||
275 | * Slugifies text for usage in a URL. |
||
276 | * |
||
277 | * Currently, this isn't just calling `sanitize_title()` on it, because that percent-encodes unicode characters, |
||
278 | * and WordPress chokes on them when used as CPT and custom taxonomy slugs. |
||
279 | * |
||
280 | * @since $VID:$ |
||
281 | * @param string $text |
||
282 | * @param string $fallback |
||
283 | * @return string which can be used in a URL |
||
284 | */ |
||
285 | public static function slugify($text, $fallback) |
||
296 | } |
||
297 |