FormElementSelectMultiple   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 61
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHTML() 0 25 4
A value() 0 3 1
1
<?php
2
3
namespace Anax\HTMLForm;
4
5
/**
6
 * Form element
7
 */
8
class FormElementSelectMultiple extends FormElementSelect
9
{
10
    /**
11
     * Constructor
12
     *
13
     * @param string $name       of the element.
14
     * @param array  $attributes to set to the element. Default is an
15
     *                           empty array.
16
     */
17 4
    public function __construct($name, $attributes = [])
18
    {
19 4
        parent::__construct($name, $attributes);
20 3
        $this['type'] = 'select-multiple';
21 3
    }
22
23
24
25
    /**
26
     * Get the value of the form element.
27
     *
28
     * @return array the checked values of the form element.
29
     */
30
    public function value()
31
    {
32
        return $this['checked'];
33
    }
34
35
36
37
    /**
38
     * Get HTML code for a element.
39
     *
40
     * @return string HTML code for the element.
41
     *
42
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
43
     */
44 2
    public function getHTML()
45
    {
46 2
        $details = $this->getHTMLDetails();
47 2
        extract($details);
0 ignored issues
show
Bug introduced by
$details of type Anax\HTMLForm\HTML is incompatible with the type array expected by parameter $var_array of extract(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        extract(/** @scrutinizer ignore-type */ $details);
Loading history...
48
        
49 2
        $name = " name='{$this['name']}[]'";
50 2
        $options = null;
51 2
        $selectedValues = is_array($this['checked']) ? $this['checked'] : [];
52
        
53 2
        foreach ($this['options'] as $optValue => $optText) {
54 1
            $selected = in_array($optValue, $selectedValues)
0 ignored issues
show
Bug introduced by
It seems like $selectedValues can also be of type null; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
            $selected = in_array($optValue, /** @scrutinizer ignore-type */ $selectedValues)
Loading history...
55
                ? " selected"
56 1
                : null;
57 1
            $options .= "<option value='{$optValue}'{$selected}>{$optText}</option>\n";
58
        }
59
60
        return <<<EOD
61
<p>
62 2
<label for='$id'>$label</label>$brAfterLabel
63 2
<select id='$id'{$size}{$class}{$name}{$autofocus}{$required}{$readonly}{$title} multiple>
64 2
{$options}
65
</select>
66 2
{$messages}
67
</p>
68 2
<p class='cf-desc'>{$description}</p>
69
EOD;
70
    }
71
}
72