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