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:
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 ) { |
||
48 | |||
49 | |||
50 | |||
51 | /** |
||
52 | * Returns whether not the remote file exists. |
||
53 | * Checking via GET because HEAD requests are blocked on some server configurations. |
||
54 | * @param string $url |
||
55 | * @param boolean $sslverify whether we care if the SSL certificate for the requested site is setup properly |
||
56 | * @return boolean |
||
57 | */ |
||
58 | public static function remote_file_exists( $url, $args = array() ){ |
||
72 | |||
73 | |||
74 | |||
75 | /** |
||
76 | * refactor_url |
||
77 | * primarily used for removing the query string from a URL |
||
78 | * |
||
79 | * @param string $url |
||
80 | * @param bool $remove_query - TRUE (default) will strip off any URL params, ie: ?this=1&that=2 |
||
81 | * @param bool $base_url_only - TRUE will only return the scheme and host with no other parameters |
||
82 | * @return string |
||
83 | */ |
||
84 | public static function refactor_url( $url = '', $remove_query = TRUE, $base_url_only = FALSE ) { |
||
108 | |||
109 | |||
110 | |||
111 | /** |
||
112 | * get_query_string |
||
113 | * returns just the query string from a URL, formatted by default into an array of key value pairs |
||
114 | * |
||
115 | * @param string $url |
||
116 | * @param bool $as_array TRUE (default) will return query params as an array of key value pairs, FALSE will simply return the query string |
||
117 | * @return string|array |
||
118 | */ |
||
119 | public static function get_query_string( $url = '', $as_array = TRUE ) { |
||
145 | |||
146 | |||
147 | |||
148 | /** |
||
149 | * prevent_prefetching |
||
150 | * @return void |
||
151 | */ |
||
152 | public static function prevent_prefetching(){ |
||
156 | |||
157 | |||
158 | |||
159 | |||
160 | /** |
||
161 | * This generates a unique site-specific string. |
||
162 | * An example usage for this string would be to save as a unique identifier for a record in the db for usage in urls. |
||
163 | * |
||
164 | * @param string $prefix Use this to prefix the string with something. |
||
165 | * @return string |
||
166 | */ |
||
167 | public static function generate_unique_token( $prefix = '' ) { |
||
171 | |||
172 | |||
173 | |||
174 | /** |
||
175 | * add_nocache_headers |
||
176 | * @return void |
||
177 | */ |
||
178 | public static function add_nocache_headers(){ |
||
184 | |||
185 | |||
186 | |||
187 | /** |
||
188 | * filter_input_server_url |
||
189 | * uses filter_input() to sanitize one of the INPUT_SERVER URL values |
||
190 | * but adds a backup in case filter_input() returns nothing, which can erringly happen on some servers |
||
191 | * |
||
192 | * @param string $server_variable |
||
193 | * @return string |
||
194 | */ |
||
195 | public static function filter_input_server_url( $server_variable = 'REQUEST_URI' ){ |
||
212 | |||
213 | |||
214 | |||
215 | } |
||
216 | // End of file EEH_URL.helper.php |
||
217 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.