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

FormElementSelect::getHTML()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 0
dl 0
loc 23
ccs 14
cts 15
cp 0.9333
crap 3.0026
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\HTMLForm;
4
5
/**
6
 * Form element
7
 */
8
class FormElementSelect 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
     * @throws Exception if missing <options>
18
     */
19 8
    public function __construct($name, $attributes = [])
20
    {
21 8
        parent::__construct($name, $attributes);
22 8
        $this['type'] = 'select';
23 8
        $this->UseNameAsDefaultLabel();
24
        
25 8
        if (!is_array($this['options'])) {
26 2
            throw new Exception("Select needs options, did you forget to specify them when creating the element?");
27
        }
28 6
    }
29
30
31
32
    /**
33
     * Get HTML code for a element.
34
     *
35
     * @return string HTML code for the element.
36
     *
37
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
38
     */
39 2
    public function getHTML()
40
    {
41 2
        $details = $this->getHTMLDetails();
42 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

42
        extract(/** @scrutinizer ignore-type */ $details);
Loading history...
43
        
44 2
        $options = null;
45 2
        foreach ($this['options'] as $optValue => $optText) {
46 1
            $options .= "<option value='{$optValue}'"
47 1
                . (($this['value'] == $optValue)
48
                    ? " selected=\"selected\""
49 1
                    : null)
50 1
                . ">{$optText}</option>\n";
51
        }
52
53
        return <<<EOD
0 ignored issues
show
Bug Best Practice introduced by
The expression return '<p> <label for='...'>'.$description.'</p>' 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...
54
<p>
55 2
<label for='$id'>$label</label>$brAfterLabel
56 2
<select id='$id'{$class}{$name}{$autofocus}{$required}{$readonly}{$checked}{$title}>
57 2
{$options}
58
</select>
59 2
{$messages}
60
</p>
61 2
<p class='cf-desc'>{$description}</p>
62
EOD;
63
    }
64
}
65