| Conditions | 13 |
| Paths | 25 |
| Total Lines | 72 |
| 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 |
||
| 23 | function jetpack_business_hours_render( $attributes, $content ) { |
||
| 24 | global $wp_locale; |
||
| 25 | |||
| 26 | if ( empty( $attributes['hours'] ) || ! is_array( $attributes['hours'] ) ) { |
||
| 27 | return $content; |
||
| 28 | } |
||
| 29 | |||
| 30 | $start_of_week = (int) get_option( 'start_of_week', 0 ); |
||
| 31 | $time_format = get_option( 'time_format' ); |
||
| 32 | $today = current_time( 'D' ); |
||
| 33 | $custom_class_name = isset( $attributes['className'] ) ? $attributes['className'] : ''; |
||
|
|
|||
| 34 | $content = sprintf( |
||
| 35 | '<dl class="jetpack-business-hours %s">', |
||
| 36 | ! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : '' |
||
| 37 | ); |
||
| 38 | |||
| 39 | $days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ); |
||
| 40 | |||
| 41 | if ( $start_of_week ) { |
||
| 42 | $chunk1 = array_slice( $attributes['hours'], 0, $start_of_week ); |
||
| 43 | $chunk2 = array_slice( $attributes['hours'], $start_of_week ); |
||
| 44 | $attributes['hours'] = array_merge( $chunk2, $chunk1 ); |
||
| 45 | } |
||
| 46 | |||
| 47 | foreach ( $attributes['hours'] as $day => $hours ) { |
||
| 48 | $opening = strtotime( $hours['opening'] ); |
||
| 49 | $closing = strtotime( $hours['closing'] ); |
||
| 50 | |||
| 51 | $content .= '<dt class="' . esc_attr( $day ) . '">' . |
||
| 52 | ucfirst( $wp_locale->get_weekday( array_search( $day, $days ) ) ) . |
||
| 53 | '</dt>'; |
||
| 54 | $content .= '<dd class="' . esc_attr( $day ) . '">'; |
||
| 55 | if ( $hours['opening'] && $hours['closing'] ) { |
||
| 56 | $content .= sprintf( |
||
| 57 | /* Translators: Business opening hours info. */ |
||
| 58 | _x( 'From %1$s to %2$s', 'from business opening hour to closing hour', 'jetpack' ), |
||
| 59 | date( $time_format, $opening ), |
||
| 60 | date( $time_format, $closing ) |
||
| 61 | ); |
||
| 62 | |||
| 63 | if ( $today === $day ) { |
||
| 64 | $now = strtotime( current_time( 'H:i' ) ); |
||
| 65 | if ( $now < $opening ) { |
||
| 66 | $content .= '<br />'; |
||
| 67 | $content .= esc_html( |
||
| 68 | sprintf( |
||
| 69 | /* Translators: Amount of time until business opens. */ |
||
| 70 | _x( 'Opening in %s', 'Amount of time until business opens', 'jetpack' ), |
||
| 71 | human_time_diff( $now, $opening ) |
||
| 72 | ) |
||
| 73 | ); |
||
| 74 | } elseif ( $now >= $opening && $now < $closing ) { |
||
| 75 | $content .= '<br />'; |
||
| 76 | $content .= esc_html( |
||
| 77 | sprintf( |
||
| 78 | /* Translators: Amount of time until business closes. */ |
||
| 79 | _x( 'Closing in %s', 'Amount of time until business closes', 'jetpack' ), |
||
| 80 | human_time_diff( $now, $closing ) |
||
| 81 | ) |
||
| 82 | ); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } else { |
||
| 86 | $content .= esc_html__( 'CLOSED', 'jetpack' ); |
||
| 87 | } |
||
| 88 | $content .= '</dd>'; |
||
| 89 | } |
||
| 90 | |||
| 91 | $content .= '</dl>'; |
||
| 92 | |||
| 93 | return $content; |
||
| 94 | } |
||
| 95 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.