Issues (138)

src/Elements/MultiElement.php (3 issues)

Labels
Severity
1
<?php
2
3
use Nip\Form\Elements\AbstractElement;
4
5
/**
6
 * Class Nip_Form_Element_MultiElement
7
 */
8
class Nip_Form_Element_MultiElement extends AbstractElement
9
{
10
    protected $_type = 'multiElement';
11
12
    /**
13
     * @var AbstractElement[]
14
     */
15
    protected $elements = [];
16
17
    /**
18
     * @inheritdoc
19
     */
20 2
    public function setName($name)
21
    {
22 2
        $return = parent::setName($name);
23 2
        $this->updateElementNames();
24 2
        return $return;
25
    }
26
27
    /**
28
     * @param AbstractElement $element
29
     * @return $this
30
     */
31 2
    public function addElement(AbstractElement $element)
32
    {
33 2
        $key = $element->getName();
0 ignored issues
show
Are you sure the assignment to $key is correct as $element->getName() targeting Nip\Form\Elements\AbstractElement::getName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
34 2
        $this->elements[$key] = $element;
35
36 2
        $inputName = $this->getName();
0 ignored issues
show
Are you sure the assignment to $inputName is correct as $this->getName() targeting Nip\Form\Elements\AbstractElement::getName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
37 2
        $element->setName($inputName . '[' . $key . ']');
38 2
        return $this;
39
    }
40
41 2
    protected function updateElementNames()
42
    {
43 2
        $inputName = $this->getName();
0 ignored issues
show
Are you sure the assignment to $inputName is correct as $this->getName() targeting Nip\Form\Elements\AbstractElement::getName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
44 2
        $elements = $this->getElements();
45 2
        foreach ($elements as $key => $element) {
46
            $element->setName($inputName . '[' . $key . ']');
47
        }
48 2
    }
49
50
    /** @noinspection PhpMissingParentCallCommonInspection
51
     * @param $request
52
     * @return $this
53
     */
54
    public function getDataFromRequest($request)
55
    {
56
        if (is_array($request)) {
57
            $elements = $this->getElements();
58
            foreach ($elements as $key => $element) {
59
                $value = isset($request[$key]) ? $request[$key] : null;
60
                if ($value !== null) {
61
                    $element->setValue($value);
62
                }
63
            }
64
        }
65
        return $this;
66
    }
67
68
    /**
69
     * @return Nip\Form\Elements\AbstractElement[]
70
     */
71 4
    public function getElements()
72
    {
73 4
        reset($this->elements);
74 4
        return $this->elements;
75
    }
76
77
    /**
78
     * @param $name
79
     * @return Nip\Form\Elements\AbstractElement
80
     * @throws \Exception
81
     */
82
    public function getElement($name)
83
    {
84
        if ($this->hasElement($name)) {
85
            return $this->elements[$name];
86
        }
87
        throw new \Exception("Invalid child element");
88
    }
89
90
    /**
91
     * @param $name
92
     * @return bool
93
     */
94
    public function hasElement($name)
95
    {
96
        return isset($this->elements[$name]);
97
    }
98
}
99