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 |
||
208 | function widget( $args, $instance ) { |
||
209 | $instance = wp_parse_args( (array) $instance, $this->defaults() ); |
||
210 | |||
211 | $title = isset( $instance['title' ] ) ? $instance['title'] : false; |
||
212 | if ( false === $title ) { |
||
213 | $title = $this->default_title; |
||
214 | } |
||
215 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
216 | $title = apply_filters( 'widget_title', $title ); |
||
217 | |||
218 | $count = isset( $instance['count'] ) ? (int) $instance['count'] : false; |
||
219 | if ( $count < 1 || 10 < $count ) { |
||
220 | $count = 10; |
||
221 | } |
||
222 | /** |
||
223 | * Control the number of displayed posts. |
||
224 | * |
||
225 | * @module widgets |
||
226 | * |
||
227 | * @since 3.3.0 |
||
228 | * |
||
229 | * @param string $count Number of Posts displayed in the Top Posts widget. Default is 10. |
||
230 | */ |
||
231 | $count = apply_filters( 'jetpack_top_posts_widget_count', $count ); |
||
232 | |||
233 | $types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' ); |
||
234 | |||
235 | // 'likes' are not available in Jetpack |
||
236 | $ordering = isset( $instance['ordering'] ) && 'likes' == $instance['ordering'] ? 'likes' : 'views'; |
||
237 | |||
238 | View Code Duplication | if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) { |
|
239 | $display = $instance['display']; |
||
240 | } else { |
||
241 | $display = 'text'; |
||
242 | } |
||
243 | |||
244 | if ( 'text' != $display ) { |
||
245 | $get_image_options = array( |
||
246 | 'fallback_to_avatars' => true, |
||
247 | /** This filter is documented in modules/shortcodes/audio.php */ |
||
248 | 'gravatar_default' => apply_filters( 'jetpack_static_url', set_url_scheme( 'http://en.wordpress.com/i/logo/white-gray-80.png' ) ), |
||
249 | ); |
||
250 | if ( 'grid' == $display ) { |
||
251 | $get_image_options['avatar_size'] = 200; |
||
252 | } else { |
||
253 | $get_image_options['avatar_size'] = 40; |
||
254 | } |
||
255 | /** |
||
256 | * Top Posts Widget Image options. |
||
257 | * |
||
258 | * @module widgets |
||
259 | * |
||
260 | * @since 1.8.0 |
||
261 | * |
||
262 | * @param array $get_image_options { |
||
263 | * Array of Image options. |
||
264 | * @type bool true Should we default to Gravatars when no image is found? Default is true. |
||
265 | * @type string $gravatar_default Default Image URL if no Gravatar is found. |
||
266 | * @type int $avatar_size Default Image size. |
||
267 | * } |
||
268 | */ |
||
269 | $get_image_options = apply_filters( 'jetpack_top_posts_widget_image_options', $get_image_options ); |
||
270 | } |
||
271 | |||
272 | if ( function_exists( 'wpl_get_blogs_most_liked_posts' ) && 'likes' == $ordering ) { |
||
273 | $posts = $this->get_by_likes( $count ); |
||
274 | } else { |
||
275 | $posts = $this->get_by_views( $count, $args ); |
||
276 | } |
||
277 | |||
278 | // Filter the returned posts. Remove all posts that do not match the chosen Post Types. |
||
279 | if ( isset( $types ) ) { |
||
280 | foreach ( $posts as $k => $post ) { |
||
281 | if ( ! in_array( $post['post_type'], $types ) ) { |
||
282 | unset( $posts[$k] ); |
||
283 | } |
||
284 | } |
||
285 | } |
||
286 | |||
287 | if ( ! $posts ) { |
||
288 | $posts = $this->get_fallback_posts(); |
||
289 | } |
||
290 | |||
291 | echo $args['before_widget']; |
||
292 | if ( ! empty( $title ) ) |
||
293 | echo $args['before_title'] . $title . $args['after_title']; |
||
294 | |||
295 | if ( ! $posts ) { |
||
296 | if ( current_user_can( 'edit_theme_options' ) ) { |
||
297 | echo '<p>' . sprintf( |
||
298 | __( 'There are no posts to display. <a href="%s">Want more traffic?</a>', 'jetpack' ), |
||
299 | 'http://en.support.wordpress.com/getting-more-site-traffic/' |
||
300 | ) . '</p>'; |
||
301 | } |
||
302 | |||
303 | echo $args['after_widget']; |
||
304 | return; |
||
305 | } |
||
306 | |||
307 | switch ( $display ) { |
||
308 | case 'list' : |
||
309 | case 'grid' : |
||
310 | wp_enqueue_style( 'widget-grid-and-list' ); |
||
311 | foreach ( $posts as &$post ) { |
||
312 | $image = Jetpack_PostImages::get_image( $post['post_id'], array( 'fallback_to_avatars' => true ) ); |
||
313 | $post['image'] = $image['src']; |
||
314 | if ( 'blavatar' != $image['from'] && 'gravatar' != $image['from'] ) { |
||
315 | $size = (int) $get_image_options['avatar_size']; |
||
|
|||
316 | $post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$size,$size" ) ); |
||
317 | } |
||
318 | } |
||
319 | |||
320 | unset( $post ); |
||
321 | |||
322 | if ( 'grid' == $display ) { |
||
323 | echo "<div class='widgets-grid-layout no-grav'>\n"; |
||
324 | foreach ( $posts as $post ) : |
||
325 | ?> |
||
326 | <div class="widget-grid-view-image"> |
||
327 | <?php |
||
328 | /** |
||
329 | * Fires before each Top Post result, inside <li>. |
||
330 | * |
||
331 | * @module widgets |
||
332 | * |
||
333 | * @since 3.2.0 |
||
334 | * |
||
335 | * @param string $post['post_id'] Post ID. |
||
336 | */ |
||
337 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
338 | ?> |
||
339 | <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"> |
||
340 | <?php $size = (int) $get_image_options['avatar_size']; ?> |
||
341 | <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" /> |
||
342 | </a> |
||
343 | <?php |
||
344 | /** |
||
345 | * Fires after each Top Post result, inside <li>. |
||
346 | * |
||
347 | * @module widgets |
||
348 | * |
||
349 | * @since 3.2.0 |
||
350 | * |
||
351 | * @param string $post['post_id'] Post ID. |
||
352 | */ |
||
353 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
354 | ?> |
||
355 | </div> |
||
356 | <?php |
||
357 | endforeach; |
||
358 | echo "</div>\n"; |
||
359 | } else { |
||
360 | echo "<ul class='widgets-list-layout no-grav'>\n"; |
||
361 | foreach ( $posts as $post ) : |
||
362 | ?> |
||
363 | <li> |
||
364 | <?php |
||
365 | /** This action is documented in modules/widgets/top-posts.php */ |
||
366 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
367 | ?> |
||
368 | <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"> |
||
369 | <?php $size = (int) $get_image_options['avatar_size']; ?> |
||
370 | <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" /> |
||
371 | </a> |
||
372 | <div class="widgets-list-layout-links"> |
||
373 | <a href="<?php echo esc_url( $post['permalink'] ); ?>" class="bump-view" data-bump-view="tp"> |
||
374 | <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?> |
||
375 | </a> |
||
376 | </div> |
||
377 | <?php |
||
378 | /** This action is documented in modules/widgets/top-posts.php */ |
||
379 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
380 | ?> |
||
381 | </li> |
||
382 | <?php |
||
383 | endforeach; |
||
384 | echo "</ul>\n"; |
||
385 | } |
||
386 | break; |
||
387 | default : |
||
388 | echo '<ul>'; |
||
389 | foreach ( $posts as $post ) : |
||
390 | ?> |
||
391 | <li> |
||
392 | <?php |
||
393 | /** This action is documented in modules/widgets/top-posts.php */ |
||
394 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
395 | ?> |
||
396 | <a href="<?php echo esc_url( $post['permalink'] ); ?>" class="bump-view" data-bump-view="tp"> |
||
397 | <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?> |
||
398 | </a> |
||
399 | <?php |
||
400 | /** This action is documented in modules/widgets/top-posts.php */ |
||
401 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
402 | ?> |
||
403 | </li> |
||
404 | <?php |
||
405 | endforeach; |
||
406 | echo '</ul>'; |
||
407 | } |
||
408 | |||
409 | echo $args['after_widget']; |
||
410 | } |
||
411 | |||
585 |
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: