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_Google_Translate_Widget extends WP_Widget { |
||
16 | static $instance = null; |
||
|
|||
17 | |||
18 | /** |
||
19 | * Default widget title. |
||
20 | * |
||
21 | * @var string $default_title |
||
22 | */ |
||
23 | var $default_title; |
||
24 | |||
25 | /** |
||
26 | * Register widget with WordPress. |
||
27 | */ |
||
28 | View Code Duplication | function __construct() { |
|
42 | |||
43 | /** |
||
44 | * Enqueue frontend JS scripts. |
||
45 | */ |
||
46 | public function enqueue_scripts() { |
||
53 | |||
54 | /** |
||
55 | * Display the Widget. |
||
56 | * |
||
57 | * @see WP_Widget::widget() |
||
58 | * |
||
59 | * @param array $args Display arguments. |
||
60 | * @param array $instance The settings for the particular instance of the widget. |
||
61 | */ |
||
62 | public function widget( $args, $instance ) { |
||
93 | |||
94 | /** |
||
95 | * Widget form in the dashboard. |
||
96 | * |
||
97 | * @see WP_Widget::form() |
||
98 | * |
||
99 | * @param array $instance Previously saved values from database. |
||
100 | */ |
||
101 | public function form( $instance ) { |
||
113 | |||
114 | /** |
||
115 | * Sanitize widget form values as they are saved. |
||
116 | * |
||
117 | * @see WP_Widget::update() |
||
118 | * |
||
119 | * @param array $new_instance Values just sent to be saved. |
||
120 | * @param array $old_instance Previously saved values from database. |
||
121 | * |
||
122 | * @return array $instance Updated safe values to be saved. |
||
123 | */ |
||
124 | View Code Duplication | public function update( $new_instance, $old_instance ) { |
|
125 | $instance = array(); |
||
126 | $instance['title'] = wp_kses( $new_instance['title'], array() ); |
||
127 | if ( $instance['title'] === $this->default_title ) { |
||
128 | $instance['title'] = false; // Store as false in case of language change |
||
129 | } |
||
130 | return $instance; |
||
142 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.