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 Jetpack_Contact_Info_Widget 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 Jetpack_Contact_Info_Widget, and based on these observations, apply Extract Interface, too.
1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
22 | class Jetpack_Contact_Info_Widget extends WP_Widget { |
||
23 | |||
24 | /** |
||
25 | * Constructor |
||
26 | */ |
||
27 | public function __construct() { |
||
51 | |||
52 | /** |
||
53 | * Enqueue scripts and styles. |
||
54 | */ |
||
55 | public function enqueue_scripts() { |
||
63 | |||
64 | |||
65 | /** |
||
66 | * Return an associative array of default values |
||
67 | * |
||
68 | * These values are used in new widgets. |
||
69 | * |
||
70 | * @return array Array of default values for the Widget's options |
||
71 | */ |
||
72 | public function defaults() { |
||
84 | |||
85 | /** |
||
86 | * Outputs the HTML for this widget. |
||
87 | * |
||
88 | * @param array $args An array of standard parameters for widgets in this theme. |
||
89 | * @param array $instance An array of settings for this widget instance. |
||
90 | * |
||
91 | * @return void Echoes it's output |
||
92 | **/ |
||
93 | public function widget( $args, $instance ) { |
||
182 | |||
183 | |||
184 | /** |
||
185 | * Deals with the settings when they are saved by the admin. Here is |
||
186 | * where any validation should be dealt with. |
||
187 | * |
||
188 | * @param array $new_instance New configuration values. |
||
189 | * @param array $old_instance Old configuration values. |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | public function update( $new_instance, $old_instance ) { |
||
213 | |||
214 | |||
215 | /** |
||
216 | * Displays the form for this widget on the Widgets page of the WP Admin area. |
||
217 | * |
||
218 | * @param array $instance Instance configuration. |
||
219 | * |
||
220 | * @return void |
||
221 | */ |
||
222 | public function form( $instance ) { |
||
223 | $instance = wp_parse_args( $instance, $this->defaults() ); |
||
224 | /** This filter is documented in modules/widgets/contact-info.php */ |
||
225 | $apikey = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] ); |
||
226 | |||
227 | wp_enqueue_script( |
||
228 | 'contact-info-admin', |
||
229 | Assets::get_file_url_for_environment( |
||
230 | '_inc/build/widgets/contact-info/contact-info-admin.min.js', |
||
231 | 'modules/widgets/contact-info/contact-info-admin.js' |
||
232 | ), |
||
233 | array( 'jquery' ), |
||
234 | 20160727, |
||
235 | false |
||
236 | ); |
||
237 | |||
238 | if ( is_customize_preview() ) { |
||
239 | $customize_contact_info_api_key_nonce = wp_create_nonce( 'customize_contact_info_api_key' ); |
||
240 | wp_localize_script( |
||
241 | 'contact-info-admin', |
||
242 | 'contact_info_api_key_ajax_obj', |
||
243 | array( 'nonce' => $customize_contact_info_api_key_nonce ) |
||
244 | ); |
||
245 | } |
||
246 | |||
247 | ?> |
||
248 | <p> |
||
249 | <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> |
||
250 | <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> |
||
251 | </p> |
||
252 | |||
253 | <p> |
||
254 | <label for="<?php echo esc_attr( $this->get_field_id( 'address' ) ); ?>"><?php esc_html_e( 'Address:', 'jetpack' ); ?></label> |
||
255 | <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'address' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'address' ) ); ?>"><?php echo esc_textarea( $instance['address'] ); ?></textarea> |
||
256 | </p> |
||
257 | |||
258 | <p> |
||
259 | <input class="jp-contact-info-showmap" id="<?php echo esc_attr( $this->get_field_id( 'showmap' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'showmap' ) ); ?>" value="1" type="checkbox" <?php checked( $instance['showmap'], 1 ); ?> /> |
||
260 | <label for="<?php echo esc_attr( $this->get_field_id( 'showmap' ) ); ?>"><?php esc_html_e( 'Show map', 'jetpack' ); ?></label> |
||
261 | </p> |
||
262 | |||
263 | <?php if ( ! has_filter( 'jetpack_google_maps_api_key' ) || false === apply_filters( 'jetpack_google_maps_api_key', false ) ) { ?> |
||
264 | |||
265 | <p class="jp-contact-info-admin-map" style="<?php echo $instance['showmap'] ? '' : 'display: none;'; ?>"> |
||
266 | <label for="<?php echo esc_attr( $this->get_field_id( 'apikey' ) ); ?>"> |
||
267 | <?php esc_html_e( 'Google Maps API Key', 'jetpack' ); ?> |
||
268 | <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'apikey' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'apikey' ) ); ?>" type="text" value="<?php echo esc_attr( $apikey ); ?>" /> |
||
269 | <br /> |
||
270 | <small> |
||
271 | <?php |
||
272 | printf( |
||
273 | wp_kses( |
||
274 | /* Translators: placeholder is a URL to support documentation. */ |
||
275 | __( 'Google now requires an API key to use their maps on your site. <a href="%s">See our documentation</a> for instructions on acquiring a key.', 'jetpack' ), |
||
276 | array( |
||
277 | 'a' => array( |
||
278 | 'href' => true, |
||
279 | ), |
||
280 | ) |
||
281 | ), |
||
282 | ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'https://wordpress.com/support/widgets/contact-info/' : esc_url( Redirect::get_url( 'jetpack-support-extra-sidebar-widgets-contact-info-widget' ) ) |
||
283 | ); |
||
284 | ?> |
||
285 | </small> |
||
286 | </label> |
||
287 | </p> |
||
288 | |||
289 | <?php } else { ?> |
||
290 | |||
291 | <input type="hidden" id="<?php echo esc_attr( $this->get_field_id( 'apikey' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'apikey' ) ); ?>" value="<?php echo esc_attr( $apikey ); ?>" /> |
||
292 | |||
293 | <?php } // end if jetpack_google_maps_api_key check. ?> |
||
294 | |||
295 | <p class="jp-contact-info-admin-map jp-contact-info-embed-map" style="<?php echo $instance['showmap'] ? '' : 'display: none;'; ?>"> |
||
296 | <?php |
||
297 | if ( ! is_customize_preview() && true === $instance['goodmap'] ) { |
||
298 | echo $this->build_map( $instance['address'], $apikey ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
||
299 | } elseif ( true !== $instance['goodmap'] && ! empty( $instance['goodmap'] ) ) { |
||
300 | printf( |
||
301 | '<span class="button-link-delete">%s</span>', |
||
302 | esc_html( $instance['goodmap'] ) |
||
303 | ); |
||
304 | } |
||
305 | ?> |
||
306 | </p> |
||
307 | |||
308 | <p> |
||
309 | <label for="<?php echo esc_attr( $this->get_field_id( 'phone' ) ); ?>"><?php esc_html_e( 'Phone:', 'jetpack' ); ?></label> |
||
310 | <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'phone' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'phone' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['phone'] ); ?>" /> |
||
311 | </p> |
||
312 | |||
313 | <p> |
||
314 | <label for="<?php echo esc_attr( $this->get_field_id( 'email' ) ); ?>"><?php esc_html_e( 'Email Address:', 'jetpack' ); ?></label> |
||
315 | <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'email' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'email' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['email'] ); ?>" /> |
||
316 | </p> |
||
317 | |||
318 | <p> |
||
319 | <label for="<?php echo esc_attr( $this->get_field_id( 'hours' ) ); ?>"><?php esc_html_e( 'Hours:', 'jetpack' ); ?></label> |
||
320 | <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'hours' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hours' ) ); ?>"><?php echo esc_textarea( $instance['hours'] ); ?></textarea> |
||
321 | </p> |
||
322 | |||
323 | <?php |
||
324 | } |
||
325 | |||
326 | |||
327 | /** |
||
328 | * Generate a Google Maps link for the supplied address. |
||
329 | * |
||
330 | * @param string $address Address to link to. |
||
331 | * |
||
332 | * @return string |
||
333 | */ |
||
334 | private function build_map_link( $address ) { |
||
338 | |||
339 | |||
340 | /** |
||
341 | * Builds map display HTML code from the supplied address. |
||
342 | * |
||
343 | * @param string $address Address. |
||
344 | * @param string $api_key API Key. |
||
345 | * |
||
346 | * @return string HTML of the map. |
||
347 | */ |
||
348 | private function build_map( $address, $api_key = null ) { |
||
383 | |||
384 | /** |
||
385 | * Encode an URL |
||
386 | * |
||
387 | * @param string $address The URL to encode. |
||
388 | * |
||
389 | * @return string The encoded URL |
||
390 | */ |
||
391 | private function urlencode_address( $address ) { |
||
400 | |||
401 | /** |
||
402 | * Returns the instance's updated 'goodmap' value. |
||
403 | * |
||
404 | * @param array $old_instance Old configuration values. |
||
405 | * @param array $instance Current configuration values. |
||
406 | * |
||
407 | * @return bool|string The instance's updated 'goodmap' value. The value is true if |
||
408 | * $instance can display a good map. If not, returns an error message. |
||
409 | */ |
||
410 | private function update_goodmap( $old_instance, $instance ) { |
||
436 | |||
437 | /** |
||
438 | * Check if the instance has a valid Map location. |
||
439 | * |
||
440 | * @param array $instance Widget instance configuration. |
||
441 | * |
||
442 | * @return bool|string Whether or not there is a valid map. If not, return an error message. |
||
443 | */ |
||
444 | private function has_good_map( $instance ) { |
||
469 | |||
470 | /** |
||
471 | * Check the Google Maps API key after an Ajax call from the widget's admin form in |
||
472 | * the Customizer preview. |
||
473 | */ |
||
474 | public function ajax_check_api_key() { |
||
486 | |||
487 | } |
||
488 | |||
490 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: