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 |
||
| 8 | class WordAds_Sidebar_Widget extends WP_Widget { |
||
| 9 | |||
| 10 | private static $allowed_tags = array( 'mrec', 'wideskyscraper' ); |
||
| 11 | |||
| 12 | View Code Duplication | function __construct() { |
|
| 13 | parent::__construct( |
||
| 14 | 'wordads_sidebar_widget', |
||
| 15 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
||
| 16 | apply_filters( 'jetpack_widget_name', 'Ads' ), |
||
| 17 | array( |
||
| 18 | 'description' => __( 'Insert an ad unit wherever you can place a widget.', 'jetpack' ), |
||
| 19 | 'customize_selective_refresh' => true |
||
| 20 | ) |
||
| 21 | ); |
||
| 22 | } |
||
| 23 | |||
| 24 | public function widget( $args, $instance ) { |
||
| 25 | global $wordads; |
||
| 26 | if ( $wordads->should_bail() ) { |
||
| 27 | return false; |
||
| 28 | } |
||
| 29 | |||
| 30 | if ( ! isset( $instance['unit'] ) ) { |
||
| 31 | $instance['unit'] = 'mrec'; |
||
| 32 | } |
||
| 33 | |||
| 34 | $about = __( 'Advertisements', 'jetpack' ); |
||
| 35 | $width = WordAds::$ad_tag_ids[$instance['unit']]['width']; |
||
| 36 | $height = WordAds::$ad_tag_ids[$instance['unit']]['height']; |
||
| 37 | |||
| 38 | $snippet = ''; |
||
|
|
|||
| 39 | if ( $wordads->option( 'wordads_house', true ) ) { |
||
| 40 | $unit = 'mrec'; |
||
| 41 | if ( 'leaderboard' == $instance['unit'] && ! $this->params->mobile_device ) { |
||
| 42 | $unit = 'leaderboard'; |
||
| 43 | } else if ( 'wideskyscraper' == $instance['unit'] ) { |
||
| 44 | $unit = 'widesky'; |
||
| 45 | } |
||
| 46 | |||
| 47 | $snippet = $wordads->get_house_ad( $unit ); |
||
| 48 | } else { |
||
| 49 | $section_id = 0 === $wordads->params->blog_id ? WORDADS_API_TEST_ID : $wordads->params->blog_id . '3'; |
||
| 50 | $snippet = $wordads->get_ad_snippet( $section_id, $height, $width ); |
||
| 51 | } |
||
| 52 | |||
| 53 | echo <<< HTML |
||
| 54 | <div class="wpcnt"> |
||
| 55 | <div class="wpa"> |
||
| 56 | <span class="wpa-about">$about</span> |
||
| 57 | <div class="u {$instance['unit']}"> |
||
| 58 | $snippet |
||
| 59 | </div> |
||
| 60 | </div> |
||
| 61 | </div> |
||
| 62 | HTML; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function form( $instance ) { |
||
| 92 | |||
| 93 | public function update( $new_instance, $old_instance ) { |
||
| 104 | } |
||
| 105 | |||
| 106 | add_action( |
||
| 113 |
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.