Conditions | 31 |
Paths | > 20000 |
Total Lines | 203 |
Code Lines | 116 |
Lines | 5 |
Ratio | 2.46 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
166 | function widget( $args, $instance ) { |
||
167 | $instance = wp_parse_args( (array) $instance, $this->defaults() ); |
||
168 | |||
169 | $title = isset( $instance['title' ] ) ? $instance['title'] : false; |
||
170 | if ( false === $title ) { |
||
171 | $title = $this->default_title; |
||
172 | } |
||
173 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
174 | $title = apply_filters( 'widget_title', $title ); |
||
175 | |||
176 | $count = isset( $instance['count'] ) ? (int) $instance['count'] : false; |
||
177 | if ( $count < 1 || 10 < $count ) { |
||
178 | $count = 10; |
||
179 | } |
||
180 | /** |
||
181 | * Control the number of displayed posts. |
||
182 | * |
||
183 | * @module widgets |
||
184 | * |
||
185 | * @since 3.3.0 |
||
186 | * |
||
187 | * @param string $count Number of Posts displayed in the Top Posts widget. Default is 10. |
||
188 | */ |
||
189 | $count = apply_filters( 'jetpack_top_posts_widget_count', $count ); |
||
190 | |||
191 | $types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' ); |
||
192 | |||
193 | // 'likes' are not available in Jetpack |
||
194 | $ordering = isset( $instance['ordering'] ) && 'likes' == $instance['ordering'] ? 'likes' : 'views'; |
||
195 | |||
196 | View Code Duplication | if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) { |
|
197 | $display = $instance['display']; |
||
198 | } else { |
||
199 | $display = 'text'; |
||
200 | } |
||
201 | |||
202 | if ( 'text' != $display ) { |
||
203 | $get_image_options = array( |
||
204 | 'fallback_to_avatars' => true, |
||
205 | /** This filter is documented in modules/shortcodes/audio.php */ |
||
206 | 'gravatar_default' => apply_filters( 'jetpack_static_url', set_url_scheme( 'http://en.wordpress.com/i/logo/white-gray-80.png' ) ), |
||
207 | ); |
||
208 | if ( 'grid' == $display ) { |
||
209 | $get_image_options['avatar_size'] = 200; |
||
210 | } else { |
||
211 | $get_image_options['avatar_size'] = 40; |
||
212 | } |
||
213 | /** |
||
214 | * Top Posts Widget Image options. |
||
215 | * |
||
216 | * @module widgets |
||
217 | * |
||
218 | * @since 1.8.0 |
||
219 | * |
||
220 | * @param array $get_image_options { |
||
221 | * Array of Image options. |
||
222 | * @type bool true Should we default to Gravatars when no image is found? Default is true. |
||
223 | * @type string $gravatar_default Default Image URL if no Gravatar is found. |
||
224 | * @type int $avatar_size Default Image size. |
||
225 | * } |
||
226 | */ |
||
227 | $get_image_options = apply_filters( 'jetpack_top_posts_widget_image_options', $get_image_options ); |
||
228 | } |
||
229 | |||
230 | if ( function_exists( 'wpl_get_blogs_most_liked_posts' ) && 'likes' == $ordering ) { |
||
231 | $posts = $this->get_by_likes( $count ); |
||
232 | } else { |
||
233 | $posts = $this->get_by_views( $count ); |
||
234 | } |
||
235 | |||
236 | // Filter the returned posts. Remove all posts that do not match the chosen Post Types. |
||
237 | if ( isset( $types ) ) { |
||
238 | foreach ( $posts as $k => $post ) { |
||
239 | if ( ! in_array( $post['post_type'], $types ) ) { |
||
240 | unset( $posts[$k] ); |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | |||
245 | if ( ! $posts ) { |
||
246 | $posts = $this->get_fallback_posts(); |
||
247 | } |
||
248 | |||
249 | echo $args['before_widget']; |
||
250 | if ( ! empty( $title ) ) |
||
251 | echo $args['before_title'] . $title . $args['after_title']; |
||
252 | |||
253 | if ( ! $posts ) { |
||
254 | if ( current_user_can( 'edit_theme_options' ) ) { |
||
255 | echo '<p>' . sprintf( |
||
256 | __( 'There are no posts to display. <a href="%s">Want more traffic?</a>', 'jetpack' ), |
||
257 | 'http://en.support.wordpress.com/getting-more-site-traffic/' |
||
258 | ) . '</p>'; |
||
259 | } |
||
260 | |||
261 | echo $args['after_widget']; |
||
262 | return; |
||
263 | } |
||
264 | |||
265 | switch ( $display ) { |
||
266 | case 'list' : |
||
267 | case 'grid' : |
||
268 | wp_enqueue_style( 'widget-grid-and-list' ); |
||
269 | foreach ( $posts as &$post ) { |
||
270 | $image = Jetpack_PostImages::get_image( $post['post_id'], array( 'fallback_to_avatars' => true ) ); |
||
271 | $post['image'] = $image['src']; |
||
272 | if ( 'blavatar' != $image['from'] && 'gravatar' != $image['from'] ) { |
||
273 | $size = (int) $get_image_options['avatar_size']; |
||
|
|||
274 | $post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$size,$size" ) ); |
||
275 | } |
||
276 | } |
||
277 | |||
278 | unset( $post ); |
||
279 | |||
280 | if ( 'grid' == $display ) { |
||
281 | echo "<div class='widgets-grid-layout no-grav'>\n"; |
||
282 | foreach ( $posts as $post ) : |
||
283 | ?> |
||
284 | <div class="widget-grid-view-image"> |
||
285 | <?php |
||
286 | /** |
||
287 | * Fires before each Top Post result, inside <li>. |
||
288 | * |
||
289 | * @module widgets |
||
290 | * |
||
291 | * @since 3.2.0 |
||
292 | * |
||
293 | * @param string $post['post_id'] Post ID. |
||
294 | */ |
||
295 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
296 | ?> |
||
297 | <a href="<?php echo esc_url( $post['permalink'] ); ?>" title="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" class="bump-view" data-bump-view="tp"> |
||
298 | <?php $size = (int) $get_image_options['avatar_size']; ?> |
||
299 | <img width="<?php echo absint( $size ); ?>" height="<?php echo absint( $size ); ?>" src="<?php echo esc_url( $post['image'] ); ?>" alt="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" data-pin-nopin="true" /> |
||
300 | </a> |
||
301 | <?php |
||
302 | /** |
||
303 | * Fires after each Top Post result, inside <li>. |
||
304 | * |
||
305 | * @module widgets |
||
306 | * |
||
307 | * @since 3.2.0 |
||
308 | * |
||
309 | * @param string $post['post_id'] Post ID. |
||
310 | */ |
||
311 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
312 | ?> |
||
313 | </div> |
||
314 | <?php |
||
315 | endforeach; |
||
316 | echo "</div>\n"; |
||
317 | } else { |
||
318 | echo "<ul class='widgets-list-layout no-grav'>\n"; |
||
319 | foreach ( $posts as $post ) : |
||
320 | ?> |
||
321 | <li> |
||
322 | <?php |
||
323 | /** This action is documented in modules/widgets/top-posts.php */ |
||
324 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
325 | ?> |
||
326 | <a href="<?php echo esc_url( $post['permalink'] ); ?>" title="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" class="bump-view" data-bump-view="tp"> |
||
327 | <?php $size = (int) $get_image_options['avatar_size']; ?> |
||
328 | <img width="<?php echo absint( $size ); ?>" height="<?php echo absint( $size ); ?>" src="<?php echo esc_url( $post['image'] ); ?>" class='widgets-list-layout-blavatar' alt="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" data-pin-nopin="true" /> |
||
329 | </a> |
||
330 | <div class="widgets-list-layout-links"> |
||
331 | <a href="<?php echo esc_url( $post['permalink'] ); ?>" class="bump-view" data-bump-view="tp"> |
||
332 | <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?> |
||
333 | </a> |
||
334 | </div> |
||
335 | <?php |
||
336 | /** This action is documented in modules/widgets/top-posts.php */ |
||
337 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
338 | ?> |
||
339 | </li> |
||
340 | <?php |
||
341 | endforeach; |
||
342 | echo "</ul>\n"; |
||
343 | } |
||
344 | break; |
||
345 | default : |
||
346 | echo '<ul>'; |
||
347 | foreach ( $posts as $post ) : |
||
348 | ?> |
||
349 | <li> |
||
350 | <?php |
||
351 | /** This action is documented in modules/widgets/top-posts.php */ |
||
352 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
353 | ?> |
||
354 | <a href="<?php echo esc_url( $post['permalink'] ); ?>" class="bump-view" data-bump-view="tp"> |
||
355 | <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?> |
||
356 | </a> |
||
357 | <?php |
||
358 | /** This action is documented in modules/widgets/top-posts.php */ |
||
359 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
360 | ?> |
||
361 | </li> |
||
362 | <?php |
||
363 | endforeach; |
||
364 | echo '</ul>'; |
||
365 | } |
||
366 | |||
367 | echo $args['after_widget']; |
||
368 | } |
||
369 | |||
542 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: