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