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 |
||
15 | class Urls { |
||
16 | |||
17 | const HTTPS_CHECK_OPTION_PREFIX = 'jetpack_sync_https_history_'; |
||
18 | const HTTPS_CHECK_HISTORY = 5; |
||
19 | |||
20 | /** |
||
21 | * Return URL from option or PHP constant. |
||
22 | * |
||
23 | * @param string $option_name (e.g. 'home'). |
||
24 | * |
||
25 | * @return mixed|null URL. |
||
26 | */ |
||
27 | public static function get_raw_url( $option_name ) { |
||
45 | |||
46 | /** |
||
47 | * Normalize domains by removing www unless declared in the site's option. |
||
48 | * |
||
49 | * @param string $option Option value from the site. |
||
50 | * @param callable $url_function Function retrieving the URL to normalize. |
||
51 | * @return mixed|string URL. |
||
52 | */ |
||
53 | public static function normalize_www_in_url( $option, $url_function ) { |
||
81 | |||
82 | /** |
||
83 | * Return URL with a normalized protocol. |
||
84 | * |
||
85 | * @param callable $callable Function to retrieve URL option. |
||
86 | * @param string $new_value URL Protocol to set URLs to. |
||
87 | * @return string Normalized URL. |
||
88 | */ |
||
89 | public static function get_protocol_normalized_url( $callable, $new_value ) { |
||
113 | |||
114 | /** |
||
115 | * Helper function that is used when getting home or siteurl values. Decides |
||
116 | * whether to get the raw or filtered value. |
||
117 | * |
||
118 | * @param string $url_type URL to get, home or siteurl. |
||
119 | * @return string |
||
120 | */ |
||
121 | public static function get_raw_or_filtered_url( $url_type ) { |
||
139 | |||
140 | /** |
||
141 | * Return the escaped home_url. |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | public static function home_url() { |
||
157 | |||
158 | /** |
||
159 | * Return the escaped siteurl. |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | public static function site_url() { |
||
175 | |||
176 | /** |
||
177 | * Return main site URL with a normalized protocol. |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | public static function main_network_site_url() { |
||
184 | |||
185 | } |
||
186 |
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.