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 |
||
| 14 | class WPCOM_social_media_icons_widget extends WP_Widget { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Defaults |
||
| 18 | * |
||
| 19 | * @var mixed |
||
| 20 | * @access private |
||
| 21 | */ |
||
| 22 | private $defaults; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Services |
||
| 26 | * |
||
| 27 | * @var mixed |
||
| 28 | * @access private |
||
| 29 | */ |
||
| 30 | private $services; |
||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * __construct function. |
||
| 35 | * |
||
| 36 | * @access public |
||
| 37 | * @return void |
||
|
|
|||
| 38 | */ |
||
| 39 | public function __construct() { |
||
| 40 | parent::__construct( |
||
| 41 | 'wpcom_social_media_icons_widget', |
||
| 42 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
||
| 43 | apply_filters( 'jetpack_widget_name', esc_html__( 'Social Media Icons', 'jetpack' ) ), |
||
| 44 | array( |
||
| 45 | 'description' => __( 'A simple widget that displays social media icons.', 'jetpack' ), |
||
| 46 | 'customize_selective_refresh' => true, |
||
| 47 | ) |
||
| 48 | ); |
||
| 49 | $this->defaults = array( |
||
| 50 | 'title' => __( 'Social', 'jetpack' ), |
||
| 51 | 'facebook_username' => '', |
||
| 52 | 'twitter_username' => '', |
||
| 53 | 'instagram_username' => '', |
||
| 54 | 'pinterest_username' => '', |
||
| 55 | 'linkedin_username' => '', |
||
| 56 | 'github_username' => '', |
||
| 57 | 'youtube_username' => '', |
||
| 58 | 'vimeo_username' => '', |
||
| 59 | 'googleplus_username' => '', |
||
| 60 | 'flickr_username' => '', |
||
| 61 | 'wordpress_username' => '', |
||
| 62 | 'twitch_username' => '', |
||
| 63 | 'tumblr_username' => '', |
||
| 64 | ); |
||
| 65 | $this->services = array( |
||
| 66 | 'facebook' => array( 'Facebook', 'https://www.facebook.com/%s/' ), |
||
| 67 | 'twitter' => array( 'Twitter', 'https://twitter.com/%s/' ), |
||
| 68 | 'instagram' => array( 'Instagram', 'https://www.instagram.com/%s/' ), |
||
| 69 | 'pinterest' => array( 'Pinterest', 'https://www.pinterest.com/%s/' ), |
||
| 70 | 'linkedin' => array( 'LinkedIn', 'https://www.linkedin.com/in/%s/' ), |
||
| 71 | 'github' => array( 'GitHub', 'https://github.com/%s/' ), |
||
| 72 | 'youtube' => array( 'YouTube', 'https://www.youtube.com/%s/' ), |
||
| 73 | 'vimeo' => array( 'Vimeo', 'https://vimeo.com/%s/' ), |
||
| 74 | 'googleplus' => array( 'Google+', 'https://plus.google.com/u/0/%s/' ), |
||
| 75 | 'flickr' => array( 'Flickr', 'https://www.flickr.com/photos/%s/' ), |
||
| 76 | 'wordpress' => array( 'WordPress.org', 'https://profiles.wordpress.org/%s/' ), |
||
| 77 | 'twitch' => array( 'Twitch', 'https://www.twitch.tv/%s/' ), |
||
| 78 | 'tumblr' => array( 'Tumblr', 'https://%s.tumblr.com' ), |
||
| 79 | ); |
||
| 80 | if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { |
||
| 81 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Enqueue Style. |
||
| 87 | * |
||
| 88 | * @access public |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | public function enqueue_style() { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Check Genericons. |
||
| 98 | * |
||
| 99 | * @access private |
||
| 100 | * @return Bool. |
||
| 101 | */ |
||
| 102 | private function check_genericons() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Widget Front End. |
||
| 114 | * |
||
| 115 | * @access public |
||
| 116 | * @param mixed $args Arguments. |
||
| 117 | * @param mixed $instance Instance. |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | public function widget( $args, $instance ) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Widget Settings. |
||
| 233 | * |
||
| 234 | * @access public |
||
| 235 | * @param mixed $instance Instance. |
||
| 236 | * @return void |
||
| 237 | */ |
||
| 238 | public function form( $instance ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Update Widget Settings. |
||
| 276 | * |
||
| 277 | * @access public |
||
| 278 | * @param mixed $new_instance New Instance. |
||
| 279 | * @param mixed $old_instance Old Instance. |
||
| 280 | * @return Instance. |
||
| 281 | */ |
||
| 282 | public function update( $new_instance, $old_instance ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Remove username from value before to save stats. |
||
| 311 | * |
||
| 312 | * @access public |
||
| 313 | * @param mixed $val Value. |
||
| 314 | * @return Value. |
||
| 315 | */ |
||
| 316 | public function remove_username( $val ) { |
||
| 319 | } // End Class. |
||
| 320 | |||
| 331 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.