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 |
||
11 | abstract class WC_Widget extends WP_Widget { |
||
12 | |||
13 | /** |
||
14 | * CSS class. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | public $widget_cssclass; |
||
19 | |||
20 | /** |
||
21 | * Widget description. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | public $widget_description; |
||
26 | |||
27 | /** |
||
28 | * Widget ID. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | public $widget_id; |
||
33 | |||
34 | /** |
||
35 | * Widget name. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | public $widget_name; |
||
40 | |||
41 | /** |
||
42 | * Settings. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | public $settings; |
||
47 | |||
48 | /** |
||
49 | * Constructor. |
||
50 | */ |
||
51 | public function __construct() { |
||
52 | $widget_ops = array( |
||
53 | 'classname' => $this->widget_cssclass, |
||
54 | 'description' => $this->widget_description, |
||
55 | 'customize_selective_refresh' => true |
||
56 | ); |
||
57 | |||
58 | parent::__construct( $this->widget_id, $this->widget_name, $widget_ops ); |
||
59 | |||
60 | add_action( 'save_post', array( $this, 'flush_widget_cache' ) ); |
||
61 | add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) ); |
||
62 | add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) ); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Get cached widget. |
||
67 | * |
||
68 | * @param array $args |
||
69 | * @return bool true if the widget is cached otherwise false |
||
70 | */ |
||
71 | public function get_cached_widget( $args ) { |
||
86 | |||
87 | /** |
||
88 | * Cache the widget. |
||
89 | * |
||
90 | * @param array $args |
||
91 | * @param string $content |
||
92 | * @return string the content that was cached |
||
93 | */ |
||
94 | public function cache_widget( $args, $content ) { |
||
99 | |||
100 | /** |
||
101 | * Flush the cache. |
||
102 | */ |
||
103 | public function flush_widget_cache() { |
||
106 | |||
107 | /** |
||
108 | * Output the html at the start of a widget. |
||
109 | * |
||
110 | * @param array $args |
||
111 | * @return string |
||
112 | */ |
||
113 | public function widget_start( $args, $instance ) { |
||
120 | |||
121 | /** |
||
122 | * Output the html at the end of a widget. |
||
123 | * |
||
124 | * @param array $args |
||
125 | * @return string |
||
126 | */ |
||
127 | public function widget_end( $args ) { |
||
130 | |||
131 | /** |
||
132 | * Updates a particular instance of a widget. |
||
133 | * |
||
134 | * @see WP_Widget->update |
||
135 | * @param array $new_instance |
||
136 | * @param array $old_instance |
||
137 | * @return array |
||
138 | */ |
||
139 | public function update( $new_instance, $old_instance ) { |
||
187 | |||
188 | /** |
||
189 | * Outputs the settings update form. |
||
190 | * |
||
191 | * @see WP_Widget->form |
||
192 | * @param array $instance |
||
193 | */ |
||
194 | public function form( $instance ) { |
||
266 | } |
||
267 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.