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 |
||
24 | class Jetpack_Blog_Stats_Widget extends WP_Widget { |
||
25 | |||
26 | /** |
||
27 | * Constructor |
||
28 | */ |
||
29 | View Code Duplication | function __construct() { |
|
43 | |||
44 | /** |
||
45 | * Return an associative array of default values |
||
46 | * |
||
47 | * These values are used in new widgets. |
||
48 | * |
||
49 | * @return array Array of default values for the Widget's options |
||
50 | */ |
||
51 | public function defaults() { |
||
58 | |||
59 | /** |
||
60 | * Return All Time Stats for that blog. |
||
61 | * |
||
62 | * We query the WordPress.com Stats REST API endpoint. |
||
63 | * |
||
64 | * @uses stats_get_from_restapi(). That function caches data locally for 5 minutes. |
||
65 | * |
||
66 | * @return string|false $views All Time Stats for that blog. |
||
67 | */ |
||
68 | public function get_stats() { |
||
78 | |||
79 | /** |
||
80 | * Back-end widget form. |
||
81 | * |
||
82 | * @see WP_Widget::form() |
||
83 | * |
||
84 | * @param array $instance Previously saved values from database. |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | function form( $instance ) { |
||
104 | |||
105 | /** |
||
106 | * Sanitize widget form values as they are saved. |
||
107 | * |
||
108 | * @see WP_Widget::update() |
||
109 | * |
||
110 | * @param array $new_instance Values just sent to be saved. |
||
111 | * @param array $old_instance Previously saved values from database. |
||
112 | * |
||
113 | * @return array Updated safe values to be saved. |
||
114 | */ |
||
115 | function update( $new_instance, $old_instance ) { |
||
122 | |||
123 | /** |
||
124 | * Front-end display of widget. |
||
125 | * |
||
126 | * @see WP_Widget::widget() |
||
127 | * |
||
128 | * @param array $args Widget arguments. |
||
129 | * @param array $instance Saved values from database. |
||
130 | */ |
||
131 | function widget( $args, $instance ) { |
||
161 | } |
||
162 | |||
174 |