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 |
||
10 | class WPCOM_social_media_icons_widget extends WP_Widget { |
||
11 | |||
12 | private $defaults; |
||
13 | |||
14 | private $services; |
||
15 | |||
16 | public function __construct() { |
||
56 | |||
57 | public function enqueue_style() { |
||
61 | |||
62 | private function check_social_icons() { |
||
63 | global $wp_styles; |
||
64 | |||
65 | foreach ( $wp_styles->queue as $handle ) { |
||
66 | if ( false !== stristr( $handle, 'social-logos' ) ) { |
||
67 | return $handle; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | return false; |
||
72 | } |
||
73 | |||
74 | // front end |
||
75 | public function widget( $args, $instance ) { |
||
76 | $instance = wp_parse_args( (array) $instance, $this->defaults ); |
||
77 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
78 | $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); |
||
79 | |||
80 | if ( ! $this->check_social_icons() ) { |
||
81 | wp_enqueue_style( 'social-logos' ); |
||
82 | } |
||
83 | |||
84 | $index = 10; |
||
85 | $html = array(); |
||
86 | |||
87 | $alt_text = esc_attr__( 'View %1$s’s profile on %2$s', 'jetpack' ); |
||
88 | |||
89 | foreach ( $this->services as $service => $data ) { |
||
90 | list( $service_name, $url ) = $data; |
||
91 | |||
92 | if ( ! isset( $instance[ $service . '_username' ] ) ) { |
||
93 | continue; |
||
94 | } |
||
95 | $username = $link_username = $instance[ $service . '_username' ]; |
||
96 | |||
97 | if ( empty( $username ) ) { |
||
98 | continue; |
||
99 | } |
||
100 | |||
101 | $index += 10; |
||
102 | |||
103 | if ( |
||
104 | $service === 'googleplus' |
||
105 | && ! is_numeric( $username ) |
||
106 | && substr( $username, 0, 1 ) !== "+" |
||
107 | ) { |
||
108 | $link_username = "+" . $username; |
||
109 | } |
||
110 | |||
111 | if ( $service === 'youtube' && substr( $username, 0, 2 ) == 'UC' ) { |
||
112 | $link_username = "channel/" . $username; |
||
113 | } else if ( $service === 'youtube' ) { |
||
114 | $link_username = "user/" . $username; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Fires for each profile link in the social icons widget. Can be used |
||
119 | * to change the links for certain social networks if needed. |
||
120 | * |
||
121 | * @module widgets |
||
122 | * |
||
123 | * @since 3.8.0 |
||
124 | * |
||
125 | * @param string $url the currently processed URL |
||
126 | * @param string $service the lowercase service slug, e.g. 'facebook', 'youtube', etc. |
||
127 | */ |
||
128 | $link = apply_filters( 'jetpack_social_media_icons_widget_profile_link', esc_url( sprintf( $url, $link_username ) ), $service ); |
||
129 | |||
130 | $html[ $index ] = |
||
131 | '<a title="' . sprintf( $alt_text, esc_attr( $username ), $service_name ) |
||
132 | . '" href="' . $link |
||
133 | . '" class="genericon genericon-' . $service . '" target="_blank"><span class="screen-reader-text">' |
||
134 | . sprintf( $alt_text, esc_html( $username ), $service_name ) |
||
135 | . '</span></a>'; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Fires at the end of the list of Social Media accounts. |
||
140 | * Can be used to add a new Social Media Site to the Social Media Icons Widget. |
||
141 | * The filter function passed the array of HTML entries that will be sorted |
||
142 | * by key, each wrapped in a list item element and output as an unsorted list. |
||
143 | * |
||
144 | * @module widgets |
||
145 | * |
||
146 | * @since 3.8.0 |
||
147 | * |
||
148 | * @param array $html Associative array of HTML snippets per each icon. |
||
149 | */ |
||
150 | $html = apply_filters( 'jetpack_social_media_icons_widget_array', $html ); |
||
151 | |||
152 | ksort( $html ); |
||
153 | $html = '<ul><li>' . join( '</li><li>', $html ) . '</li></ul>'; |
||
154 | |||
155 | View Code Duplication | if ( ! empty( $instance['title'] ) ) { |
|
156 | $html = $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'] . $html; |
||
157 | } |
||
158 | |||
159 | $html = $args['before_widget'] . $html . $args['after_widget']; |
||
160 | |||
161 | /** |
||
162 | * Filters the Social Media Icons widget output. |
||
163 | * |
||
164 | * @module widgets |
||
165 | * |
||
166 | * @since 3.6.0 |
||
167 | * |
||
168 | * @param string $html Social Media Icons widget html output. |
||
169 | */ |
||
170 | echo apply_filters( 'jetpack_social_media_icons_widget_output', $html ); |
||
171 | } |
||
172 | |||
173 | // back end |
||
174 | public function form( $instance ) { |
||
210 | |||
211 | // updating widget settings |
||
212 | public function update( $new_instance, $old_instance ) { |
||
241 | |||
242 | // Remove username from value before to save stats |
||
243 | public function remove_username( $val ) { |
||
246 | |||
247 | } // class ends here |
||
248 | |||
254 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.