Completed
Push — update/mailchimp-ui-changes ( f4a7d6...e71283 )
by
unknown
11:27
created

business-hours.php ➔ jetpack_business_hours_render()   C

Complexity

Conditions 13
Paths 25

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 25
nop 2
dl 0
loc 72
rs 5.9042
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Business Hours Block.
4
 *
5
 * @since 7.1.0
6
 *
7
 * @package Jetpack
8
 */
9
10
register_block_type(
11
	'jetpack/business-hours',
12
	array( 'render_callback' => 'jetpack_business_hours_render' )
13
);
14
15
/**
16
 * Dynamic rendering of the block.
17
 *
18
 * @param array  $attributes Array containing the business hours block attributes.
19
 * @param string $content    String containing the business hours block content.
20
 *
21
 * @return string
22
 */
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'] : '';
0 ignored issues
show
Unused Code introduced by
$custom_class_name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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