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