| Conditions | 12 |
| Paths | 28 |
| Total Lines | 40 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 12 | function jetpack_breadcrumbs() { |
||
| 13 | $taxonomy = is_category() ? 'category' : get_query_var( 'taxonomy' ); |
||
| 14 | $is_taxonomy_hierarchical = is_taxonomy_hierarchical( $taxonomy ); |
||
| 15 | |||
| 16 | $post_type = is_page() ? 'page' : get_query_var( 'post_type' ); |
||
| 17 | $is_post_type_hierarchical = is_post_type_hierarchical( $post_type ); |
||
| 18 | |||
| 19 | if ( ! ( $is_post_type_hierarchical || $is_taxonomy_hierarchical ) || is_front_page() ) { |
||
| 20 | return; |
||
| 21 | } |
||
| 22 | |||
| 23 | $breadcrumb = ''; |
||
| 24 | |||
| 25 | if ( $is_post_type_hierarchical ) { |
||
| 26 | $post_id = get_queried_object_id(); |
||
| 27 | $ancestors = array_reverse( get_post_ancestors( $post_id ) ); |
||
| 28 | if ( $ancestors ) { |
||
|
|
|||
| 29 | foreach ( $ancestors as $ancestor ) { |
||
| 30 | $breadcrumb .= '<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . esc_url( get_permalink( $ancestor ) ) . '" itemprop="item"><span itemprop="name">' . esc_html( get_the_title( $ancestor ) ) . '</span></a></span>'; |
||
| 31 | } |
||
| 32 | } |
||
| 33 | $breadcrumb .= '<span class="current-page" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">' . esc_html( get_the_title( $post_id ) ) . '</span></span>'; |
||
| 34 | } elseif ( $is_taxonomy_hierarchical ) { |
||
| 35 | $current = get_term( get_queried_object_id(), $taxonomy ); |
||
| 36 | |||
| 37 | if ( is_wp_error( $current ) ) { |
||
| 38 | return; |
||
| 39 | } |
||
| 40 | |||
| 41 | if ( $current->parent ) { |
||
| 42 | $breadcrumb = jetpack_get_term_parents( $current->parent, $taxonomy ); |
||
| 43 | } |
||
| 44 | |||
| 45 | $breadcrumb .= '<span class="current-category" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">' . esc_html( $current->name ) . '</span></span>'; |
||
| 46 | } |
||
| 47 | |||
| 48 | $home = '<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . esc_url( home_url( '/' ) ) . '" class="home-link" itemprop="item" rel="home"><span itemprop="name">' . esc_html__( 'Home', 'jetpack' ) . '</span></a></span>'; |
||
| 49 | |||
| 50 | echo '<nav class="entry-breadcrumbs" itemscope itemtype="http://schema.org/BreadcrumbList">' . $home . $breadcrumb . '</nav>'; |
||
| 51 | } |
||
| 52 | |||
| 80 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.