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 Jetpack_Flickr_Widget extends WP_Widget { |
||
| 16 | /** |
||
| 17 | * Constructor. |
||
| 18 | */ |
||
| 19 | View Code Duplication | function __construct() { |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Enqueue style. |
||
| 38 | */ |
||
| 39 | function enqueue_style() { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Return an associative array of default values. |
||
| 45 | * |
||
| 46 | * These values are used in new widgets. |
||
| 47 | * |
||
| 48 | * @return array Default values for the widget options. |
||
| 49 | */ |
||
| 50 | public function defaults() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Front-end display of the widget. |
||
| 61 | * |
||
| 62 | * @param array $args Widget arguments. |
||
| 63 | * @param array $instance Saved values from database. |
||
| 64 | */ |
||
| 65 | public function widget( $args, $instance ) { |
||
| 66 | $instance = wp_parse_args( $instance, $this->defaults() ); |
||
| 67 | |||
| 68 | $image_size_string = 'small' == $instance['flickr_image_size'] ? '_m.jpg' : '_t.jpg'; |
||
| 69 | |||
| 70 | $rss_url = ( ! isset( $instance['flickr_rss_url'] ) || empty( $instance['flickr_rss_url'] ) ) |
||
| 71 | ? 'https://api.flickr.com/services/feeds/photos_interesting.gne?format=rss_200' |
||
| 72 | : htmlspecialchars_decode( $instance['flickr_rss_url'] ); |
||
| 73 | |||
| 74 | // We want to use the HTTPS version so the URLs in the API response are also HTTPS and we avoid mixed-content warnings. |
||
| 75 | $rss_url = preg_replace( '!^http://api.flickr.com/!i', 'https://api.flickr.com/', $rss_url ); |
||
| 76 | |||
| 77 | $rss = fetch_feed( $rss_url ); |
||
| 78 | |||
| 79 | $photos = ''; |
||
| 80 | if ( ! is_wp_error( $rss ) ) { |
||
| 81 | foreach ( $rss->get_items( 0, $instance['items'] ) as $photo ) { |
||
| 82 | switch ( $instance['flickr_image_size'] ) { |
||
| 83 | case 'thumbnail': |
||
| 84 | $src = $photo->get_enclosure()->get_thumbnail(); |
||
| 85 | break; |
||
| 86 | case 'small': |
||
| 87 | $src = preg_match( '/src="(.*?)"/i', $photo->get_description(), $p ); |
||
|
|
|||
| 88 | $src = $p[1]; |
||
| 89 | break; |
||
| 90 | case 'large': |
||
| 91 | $src = $photo->get_enclosure()->get_link(); |
||
| 92 | break; |
||
| 93 | } |
||
| 94 | |||
| 95 | $photos .= '<a href="' . esc_url( $photo->get_permalink(), array( 'http', 'https' ) ) . '">'; |
||
| 96 | $photos .= '<img src="' . esc_url( $src, array( 'http', 'https' ) ) . '" '; |
||
| 97 | $photos .= 'alt="' . esc_attr( $photo->get_title() ) . '" '; |
||
| 98 | $photos .= 'border="0" '; |
||
| 99 | $photos .= 'title="' . esc_attr( $photo->get_title() ) . '" '; |
||
| 100 | $photos .= ' /></a><br /><br />'; |
||
| 101 | } |
||
| 102 | if ( ! empty( $photos ) && class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' ) ) { |
||
| 103 | $photos = Jetpack_Photon::filter_the_content( $photos ); |
||
| 104 | } |
||
| 105 | |||
| 106 | $flickr_home = $rss->get_link(); |
||
| 107 | } |
||
| 108 | |||
| 109 | echo $args['before_widget']; |
||
| 110 | echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title']; |
||
| 111 | require( dirname( __FILE__ ) . '/flickr/widget.php' ); |
||
| 112 | echo $args['after_widget']; |
||
| 113 | /** This action is already documented in modules/widgets/gravatar-profile.php */ |
||
| 114 | do_action( 'jetpack_stats_extra', 'widget_view', 'flickr' ); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Back-end widget form. |
||
| 119 | * |
||
| 120 | * @param array $instance Previously saved values from database. |
||
| 121 | */ |
||
| 122 | public function form( $instance ) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Sanitize widget form values as they are saved. |
||
| 129 | * |
||
| 130 | * @param array $new_instance Values just sent to be saved. |
||
| 131 | * @param array $old_instance Previously saved values from database. |
||
| 132 | * @return array Updated safe values to be saved. |
||
| 133 | */ |
||
| 134 | public function update( $new_instance, $old_instance ) { |
||
| 165 | } |
||
| 166 | |||
| 173 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.