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 if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
||
12 | class EEH_URL{ |
||
13 | |||
14 | /** |
||
15 | * _add_query_arg |
||
16 | * adds nonce to array of arguments then calls WP add_query_arg function |
||
17 | * |
||
18 | * @access public |
||
19 | * @param array $args |
||
20 | * @param string $url |
||
21 | * @param bool $exclude_nonce If true then the nonce will be excluded from the generated url. |
||
22 | * @return string |
||
23 | */ |
||
24 | public static function add_query_args_and_nonce( $args = array(), $url = '', $exclude_nonce = false ) { |
||
50 | |||
51 | |||
52 | |||
53 | /** |
||
54 | * Returns whether not the remote file exists. |
||
55 | * Checking via GET because HEAD requests are blocked on some server configurations. |
||
56 | * @param string $url |
||
57 | * @param boolean $sslverify whether we care if the SSL certificate for the requested site is setup properly |
||
|
|||
58 | * @return boolean |
||
59 | */ |
||
60 | public static function remote_file_exists( $url, $args = array() ){ |
||
74 | |||
75 | |||
76 | |||
77 | /** |
||
78 | * refactor_url |
||
79 | * primarily used for removing the query string from a URL |
||
80 | * |
||
81 | * @param string $url |
||
82 | * @param bool $remove_query - TRUE (default) will strip off any URL params, ie: ?this=1&that=2 |
||
83 | * @param bool $base_url_only - TRUE will only return the scheme and host with no other parameters |
||
84 | * @return string |
||
85 | */ |
||
86 | public static function refactor_url( $url = '', $remove_query = TRUE, $base_url_only = FALSE ) { |
||
110 | |||
111 | |||
112 | |||
113 | /** |
||
114 | * get_query_string |
||
115 | * returns just the query string from a URL, formatted by default into an array of key value pairs |
||
116 | * |
||
117 | * @param string $url |
||
118 | * @param bool $as_array TRUE (default) will return query params as an array of key value pairs, FALSE will simply return the query string |
||
119 | * @return string|array |
||
120 | */ |
||
121 | public static function get_query_string( $url = '', $as_array = TRUE ) { |
||
147 | |||
148 | |||
149 | |||
150 | /** |
||
151 | * prevent_prefetching |
||
152 | * @return void |
||
153 | */ |
||
154 | public static function prevent_prefetching(){ |
||
158 | |||
159 | |||
160 | |||
161 | |||
162 | /** |
||
163 | * This generates a unique site-specific string. |
||
164 | * An example usage for this string would be to save as a unique identifier for a record in the db for usage in urls. |
||
165 | * |
||
166 | * @param string $prefix Use this to prefix the string with something. |
||
167 | * @return string |
||
168 | */ |
||
169 | public static function generate_unique_token( $prefix = '' ) { |
||
173 | |||
174 | |||
175 | |||
176 | /** |
||
177 | * add_nocache_headers |
||
178 | * @return void |
||
179 | */ |
||
180 | public static function add_nocache_headers(){ |
||
186 | |||
187 | |||
188 | |||
189 | /** |
||
190 | * filter_input_server_url |
||
191 | * uses filter_input() to sanitize one of the INPUT_SERVER URL values |
||
192 | * but adds a backup in case filter_input() returns nothing, which can erringly happen on some servers |
||
193 | * |
||
194 | * @param string $server_variable |
||
195 | * @return string |
||
196 | */ |
||
197 | public static function filter_input_server_url( $server_variable = 'REQUEST_URI' ){ |
||
214 | |||
215 | |||
216 | |||
217 | /** |
||
218 | * Gets the current page's full URL |
||
219 | * @return string |
||
220 | */ |
||
221 | View Code Duplication | public static function current_url() { |
|
231 | |||
232 | |||
233 | |||
234 | } |
||
235 | // End of file EEH_URL.helper.php |
||
236 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.