FieldSelect::fill()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 3
nop 0
1
<?php
2
3
namespace hemio\form;
4
5
use hemio\html;
6
use \RecursiveIteratorIterator as RecursIter;
7
8
/**
9
 *
10
 * @since 1.0 
11
 */
12
class FieldSelect extends Abstract_\FormFieldDefault {
13
14
    /**
15
     * 
16
     * 
17
     * @var hemio\html\Select
18
     */
19
    protected $controlElement;
20
21
    /**
22
     *
23
     * @var boolean
24
     */
25
    protected $filled = false;
26
27
    public function __construct($name, $title) {
28
        $this->init($name, $title, new html\Select);
29
    }
30
31
    /**
32
     * 
33
     * @return html\Select
34
     */
35
    public function getControlElement() {
36
        return $this->control;
37
    }
38
39
    /**
40
     * 
41
     * @return \hemio\form\Abstract_\TemplateFormField
42
     */
43
    public function fill() {
44
        foreach (new RecursIter($this->getControlElement(), RecursIter::SELF_FIRST) as $option)
45
            if (
46
                    $option instanceof html\Option &&
47
                    $option->getAttribute('value') == $this->getValueToUse()
48
            )
49
                $option->setAttribute('selected', true);
50
51
        $template = $this->getFieldTemplateClone('SELECT');
52
53
        $this['_TEMPLATE'] = $template;
54
        $template->init($this, $this->control);
55
56
        $this->filled = true;
57
58
        return $template;
59
    }
60
61
    /**
62
     * 
63
     * @param mixed $value
64
     * @param mixed $content
65
     * @return html\Option
66
     */
67
    public function addOption($value, $content = null) {
68
        if ($content === null)
69
            $content = $value;
70
71
        $option = new html\Option($value, new html\Str($content));
72
        $this->getControlElement()->addChild($option);
0 ignored issues
show
Unused Code introduced by
The call to the method hemio\html\Select::addChild() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
73
74
        return $option;
75
    }
76
77
}
78