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 | function __construct() { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Enqueue frontend JS scripts. |
||
| 45 | */ |
||
| 46 | public function enqueue_scripts() { |
||
| 47 | wp_register_script( |
||
| 48 | 'google-translate-init', |
||
| 49 | Jetpack::get_file_url_for_environment( |
||
| 50 | '_inc/build/widgets/google-translate/google-translate.min.js', |
||
| 51 | 'modules/widgets/google-translate/google-translate.js' |
||
| 52 | ) |
||
| 53 | ); |
||
| 54 | wp_register_script( 'google-translate', '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', array( 'google-translate-init' ) ); |
||
| 55 | // Admin bar is also displayed on top of the site which causes google translate bar to hide beneath. |
||
| 56 | // This is a hack to show google translate bar a bit lower. |
||
| 57 | wp_add_inline_style( 'admin-bar', '.goog-te-banner-frame { top:32px !important }' ); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Display the Widget. |
||
| 62 | * |
||
| 63 | * @see WP_Widget::widget() |
||
| 64 | * |
||
| 65 | * @param array $args Display arguments. |
||
| 66 | * @param array $instance The settings for the particular instance of the widget. |
||
| 67 | */ |
||
| 68 | public function widget( $args, $instance ) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Widget form in the dashboard. |
||
| 135 | * |
||
| 136 | * @see WP_Widget::form() |
||
| 137 | * |
||
| 138 | * @param array $instance Previously saved values from database. |
||
| 139 | */ |
||
| 140 | public function form( $instance ) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Sanitize widget form values as they are saved. |
||
| 155 | * |
||
| 156 | * @see WP_Widget::update() |
||
| 157 | * |
||
| 158 | * @param array $new_instance Values just sent to be saved. |
||
| 159 | * @param array $old_instance Previously saved values from database. |
||
| 160 | * |
||
| 161 | * @return array $instance Updated safe values to be saved. |
||
| 162 | */ |
||
| 163 | View Code Duplication | public function update( $new_instance, $old_instance ) { |
|
| 171 | |||
| 172 | } |
||
| 173 | |||
| 181 |
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.