Completed
Branch FET-Wait-List (92c8c5)
by
unknown
134:10 queued 122:28
created

EE_Checkbox_Display_Strategy::display()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 51
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 44
nc 10
nop 0
dl 0
loc 51
rs 8.6588
c 0
b 0
f 0

How to fix   Long Method   

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
defined('EVENT_ESPRESSO_VERSION') || exit;
3
4
5
6
/**
7
 * Class EE_Checkbox_Display_Strategy
8
 * displays a set of checkbox inputs
9
 *
10
 * @package       Event Espresso
11
 * @subpackage    core
12
 * @author        Mike Nelson
13
 * @since         4.6
14
 */
15
class EE_Checkbox_Display_Strategy extends EE_Compound_Input_Display_Strategy
16
{
17
18
    /**
19
     * @throws EE_Error
20
     * @return string of html to display the field
21
     */
22
    public function display()
23
    {
24
        $input = $this->get_input();
25
        $multi = count($input->options()) > 1;
0 ignored issues
show
Unused Code introduced by
$multi 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...
26
        $input->set_label_sizes();
27
        $label_size_class = $input->get_label_size_class();
28
        $html             = '';
29
        if (! is_array($input->raw_value()) && $input->raw_value() !== null) {
30
            EE_Error::doing_it_wrong(
31
                'EE_Checkbox_Display_Strategy::display()',
32
                sprintf(
33
                    esc_html__(
34
                        'Input values for checkboxes should be an array of values, but the value for input "%1$s" is "%2$s". Please verify that the input name is exactly "%3$s"',
35
                        'event_espresso'
36
                    ),
37
                    $input->html_id(),
38
                    var_export($input->raw_value(), true),
39
                    $input->html_name() . '[]'
40
                ),
41
                '4.8.1'
42
            );
43
        }
44
        $input_raw_value = (array)$input->raw_value();
45
        foreach ($input->options() as $value => $display_text) {
46
            $value = $input->get_normalization_strategy()->unnormalize_one($value);
47
            $html_id = $this->get_sub_input_id($value);
48
            $html .= EEH_HTML::nl(0, 'checkbox');
49
            $html .= '<label for="'
50
                     . $html_id
51
                     . '" id="'
52
                     . $html_id
53
                     . '-lbl" class="ee-checkbox-label-after'
54
                     . $label_size_class
55
                     . '">';
56
            $html .= EEH_HTML::nl(1, 'checkbox');
57
            $html .= '<input type="checkbox"';
58
            $html .= ' name="' . $input->html_name() . '[]"';
59
            $html .= ' id="' . $html_id . '"';
60
            $html .= ' class="' . $input->html_class() . '"';
61
            $html .= ' style="' . $input->html_style() . '"';
62
            $html .= ' value="' . esc_attr($value) . '"';
63
            $html .= ! empty($input_raw_value) && in_array($value, $input_raw_value, true)
64
                ? ' checked="checked"'
65
                : '';
66
            $html .= ' ' . $this->_input->other_html_attributes();
67
            $html .= '>&nbsp;';
68
            $html .= $display_text;
69
            $html .= EEH_HTML::nl(-1, 'checkbox') . '</label>';
70
        }
71
        return $html;
72
    }
73
74
75
76
}
77