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 |
||
| 17 | class WPCOM_Widget_Goodreads extends WP_Widget { |
||
| 18 | |||
| 19 | private $goodreads_widget_id = 0; |
||
| 20 | |||
| 21 | function __construct() { |
||
| 43 | |||
| 44 | function enqueue_style() { |
||
| 51 | |||
| 52 | function widget( $args, $instance ) { |
||
| 53 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
| 54 | $title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '' ); |
||
| 55 | |||
| 56 | View Code Duplication | if ( empty( $instance['user_id'] ) || 'invalid' === $instance['user_id'] ) { |
|
| 57 | if ( current_user_can('edit_theme_options') ) { |
||
| 58 | echo $args['before_widget']; |
||
| 59 | echo '<p>' . sprintf( |
||
| 60 | __( 'You need to enter your numeric user ID for the <a href="%1$s">Goodreads Widget</a> to work correctly. <a href="%2$s" target="_blank">Full instructions</a>.', 'jetpack' ), |
||
| 61 | esc_url( admin_url( 'widgets.php' ) ), |
||
| 62 | 'http://support.wordpress.com/widgets/goodreads-widget/#goodreads-user-id' |
||
| 63 | ) . '</p>'; |
||
| 64 | echo $args['after_widget']; |
||
| 65 | } |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | |||
| 69 | if ( !array_key_exists( $instance['shelf'], $this->shelves ) ) |
||
| 70 | return; |
||
| 71 | |||
| 72 | $instance['user_id'] = absint( $instance['user_id'] ); |
||
| 73 | |||
| 74 | // Set widget ID based on shelf. |
||
| 75 | $this->goodreads_widget_id = $instance['user_id'] . '_' . $instance['shelf']; |
||
|
|
|||
| 76 | |||
| 77 | if ( empty( $title ) ) $title = esc_html__( 'Goodreads', 'jetpack' ); |
||
| 78 | |||
| 79 | echo $args['before_widget']; |
||
| 80 | echo $args['before_title'] . $title . $args['after_title']; |
||
| 81 | |||
| 82 | $goodreads_url = 'https://www.goodreads.com/review/custom_widget/' . urlencode( $instance['user_id'] ) . '.' . urlencode( $instance['title'] ) . ':%20' . urlencode( $instance['shelf'] ) . '?cover_position=&cover_size=small&num_books=5&order=d&shelf=' . urlencode( $instance['shelf'] ) . '&sort=date_added&widget_bg_transparent=&widget_id=' . esc_attr( $this->goodreads_widget_id ) ; |
||
| 83 | |||
| 84 | echo '<div class="gr_custom_widget" id="gr_custom_widget_' . esc_attr( $this->goodreads_widget_id ). '"></div>' . "\n"; |
||
| 85 | echo '<script src="' . esc_url( $goodreads_url ) . '"></script>' . "\n"; |
||
| 86 | |||
| 87 | echo $args['after_widget']; |
||
| 88 | |||
| 89 | /** This action is already documented in modules/widgets/gravatar-profile.php */ |
||
| 90 | do_action( 'jetpack_stats_extra', 'widget', 'goodreads' ); |
||
| 91 | } |
||
| 92 | |||
| 93 | function goodreads_user_id_exists( $user_id ) { |
||
| 101 | |||
| 102 | function update( $new_instance, $old_instance ) { |
||
| 118 | |||
| 119 | function form( $instance ) { |
||
| 143 | } |
||
| 144 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.