Completed
Push — master ( 8b38a2...313d97 )
by Mikael
04:15
created

FormElementCheckboxMultiple   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 89.29%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 71
ccs 25
cts 28
cp 0.8929
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getHTML() 0 31 4
A __construct() 0 7 2
A value() 0 3 1
1
<?php
2
3
namespace Anax\HTMLForm;
4
5
/**
6
 * Form element
7
 */
8
class FormElementCheckboxMultiple extends FormElement
9
{
10
11
    /**
12
     * Constructor
13
     *
14
     * @param string $name       of the element.
15
     * @param array  $attributes to set to the element. Default is an empty array.
16
     */
17 4
    public function __construct($name, $attributes = [])
18
    {
19 4
        parent::__construct($name, $attributes);
20 4
        $this['type'] = 'checkbox-multiple';
21
        
22 4
        if (!isset($this['values'])) {
23 1
            throw new Exception("Missing values for the checkbox.");
24
        }
25 3
    }
26
27
28
29
    /**
30
     * Get the value of the form element.
31
     *
32
     * @return array the checked values of the form element.
33
     */
34
    public function value()
35
    {
36
        return $this['checked'];
37
    }
38
39
40
41
    /**
42
     * Get HTML code for a element.
43
     *
44
     * @return string HTML code for the element.
45
     *
46
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
47
     */
48 2
    public function getHTML()
49
    {
50 2
        $details = $this->getHTMLDetails();
51 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

51
        extract(/** @scrutinizer ignore-type */ $details);
Loading history...
52
53 2
        $type = "type='checkbox'";
54 2
        $name = " name='{$this['name']}[]'";
55 2
        $ret = null;
56 2
        $checkedValues = is_array($this['checked']) ? $this['checked'] : [];
57
58 2
        foreach ($this['values'] as $val) {
59 1
            $id .= $val;
60 1
            $item = $onlyValue  = htmlentities($val, ENT_QUOTES, $this->characterEncoding);
61 1
            $value = " value='{$onlyValue}'";
62 1
            $checked = in_array($val, $checkedValues)
0 ignored issues
show
Bug introduced by
It seems like $checkedValues 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

62
            $checked = in_array($val, /** @scrutinizer ignore-type */ $checkedValues)
Loading history...
63
                ? " checked='checked'"
64 1
                : null;
65
66
            $ret .= <<<EOD
67
<p>
68 1
<input id='$id'{$type}{$class}{$name}{$value}{$autofocus}{$readonly}{$checked}{$title} />
69 1
<label for='$id'>$item</label>
70 1
{$messages}
71
</p>
72
EOD;
73
        }
74
        return <<<EOD
0 ignored issues
show
Bug Best Practice introduced by
The expression return '<div> <p>'.$labe...scription.'</p> </div>' returns the type string which is incompatible with the return type mandated by Anax\HTMLForm\FormElement::getHTML() of Anax\HTMLForm\HTML.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
75
<div>
76 2
<p>{$label}</p>
77 2
{$ret}
78 2
<p class='cf-desc'>{$description}</p>
79
</div>
80
EOD;
81
    }
82
}
83