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 |
||
16 | class Utils { |
||
17 | |||
18 | const DEFAULT_JETPACK__API_VERSION = 1; |
||
19 | const DEFAULT_JETPACK__API_BASE = 'https://jetpack.wordpress.com/jetpack.'; |
||
20 | const DEFAULT_JETPACK__WPCOM_JSON_API_BASE = 'https://public-api.wordpress.com'; |
||
21 | |||
22 | const HTTPS_CHECK_OPTION_PREFIX = 'jetpack_sync_https_history_'; |
||
23 | const HTTPS_CHECK_HISTORY = 5; |
||
24 | |||
25 | /** |
||
26 | * This method used to set the URL scheme to HTTP when HTTPS requests can't be made. |
||
27 | * Now it returns the exact same URL you pass as an argument. |
||
28 | * |
||
29 | * @param string $url The url. |
||
30 | * @return string The exact same url. |
||
31 | * |
||
32 | * @deprecated 9.1.0 Jetpack can't function properly on servers that don't support outbound HTTPS requests. |
||
33 | */ |
||
34 | public static function fix_url_for_bad_hosts( $url ) { |
||
38 | |||
39 | /** |
||
40 | * Enters a user token into the user_tokens option |
||
41 | * |
||
42 | * @deprecated 9.5 Use Automattic\Jetpack\Connection\Tokens->update_user_token() instead. |
||
43 | * |
||
44 | * @param int $user_id The user id. |
||
45 | * @param string $token The user token. |
||
46 | * @param bool $is_master_user Whether the user is the master user. |
||
47 | * @return bool |
||
48 | */ |
||
49 | public static function update_user_token( $user_id, $token, $is_master_user ) { |
||
53 | |||
54 | /** |
||
55 | * Filters the value of the api constant. |
||
56 | * |
||
57 | * @param String $constant_value The constant value. |
||
58 | * @param String $constant_name The constant name. |
||
59 | * @return mixed | null |
||
60 | */ |
||
61 | public static function jetpack_api_constant_filter( $constant_value, $constant_name ) { |
||
73 | |||
74 | /** |
||
75 | * Add a filter to initialize default values of the constants. |
||
76 | */ |
||
77 | public static function init_default_constants() { |
||
85 | |||
86 | /** |
||
87 | * Filters the registration request body to include tracking properties. |
||
88 | * |
||
89 | * @param array $properties Already prepared tracking properties. |
||
90 | * @return array amended properties. |
||
91 | */ |
||
92 | View Code Duplication | public static function filter_register_request_body( $properties ) { |
|
104 | |||
105 | /** |
||
106 | * Return URL from option or PHP constant. |
||
107 | * |
||
108 | * @param string $option_name (e.g. 'home'). |
||
109 | * |
||
110 | * @return mixed|null URL. |
||
111 | */ |
||
112 | public static function get_raw_url( $option_name ) { |
||
130 | |||
131 | /** |
||
132 | * Normalize domains by removing www unless declared in the site's option. |
||
133 | * |
||
134 | * @param string $option Option value from the site. |
||
135 | * @param callable $url_function Function retrieving the URL to normalize. |
||
136 | * @return mixed|string URL. |
||
137 | */ |
||
138 | public static function normalize_www_in_url( $option, $url_function ) { |
||
166 | |||
167 | /** |
||
168 | * Return URL with a normalized protocol. |
||
169 | * |
||
170 | * @param callable $callable Function to retrieve URL option. |
||
171 | * @param string $new_value URL Protocol to set URLs to. |
||
172 | * @return string Normalized URL. |
||
173 | */ |
||
174 | public static function get_protocol_normalized_url( $callable, $new_value ) { |
||
198 | |||
199 | /** |
||
200 | * Helper function that is used when getting home or siteurl values. Decides |
||
201 | * whether to get the raw or filtered value. |
||
202 | * |
||
203 | * @param string $url_type URL to get, home or siteurl. |
||
204 | * @return string |
||
205 | */ |
||
206 | public static function get_raw_or_filtered_url( $url_type ) { |
||
224 | |||
225 | /** |
||
226 | * Return the escaped home_url. |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | public static function home_url() { |
||
242 | |||
243 | /** |
||
244 | * Return the escaped siteurl. |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | public static function site_url() { |
||
260 | |||
261 | /** |
||
262 | * Return main site URL with a normalized protocol. |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | public static function main_network_site_url() { |
||
269 | } |
||
270 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.