Issues (138)

src/Elements/CheckboxGroup.php (5 issues)

1
<?php
2
3
/**
4
 * Class Nip_Form_Element_CheckboxGroup
5
 *
6
 * @method Nip_Form_Element_Checkbox[] getElements()
7
 */
8
class Nip_Form_Element_CheckboxGroup extends Nip_Form_Element_Input_Group
9
{
10
    protected $_type = 'checkboxGroup';
11
12
    /**
13
     * @param string $requester
14
     * @return null
15
     */
16
    public function getValue($requester = 'abstract')
0 ignored issues
show
The parameter $requester is not used and could be removed. ( Ignorable by Annotation )

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

16
    public function getValue(/** @scrutinizer ignore-unused */ $requester = 'abstract')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
17
    {
18
        $elements = $this->getElements();
19
        $data = [];
20
        if ($elements) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $elements of type Nip_Form_Element_Checkbox[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
21
            foreach ($elements as $element) {
22
                if ($element->isChecked()) {
23
                    $data[] = $element->getValue();
24
                }
25
            }
26
        }
27
28
        return $data;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $data returns the type array|array<mixed,mixed|null> which is incompatible with the documented return type null.
Loading history...
29
    }
30
31
    /** @noinspection PhpMissingParentCallCommonInspection
32
     * @inheritdoc
33
     */
34
    public function setValue($value)
35
    {
36
        return $this->getDataFromRequest($value);
37
    }
38
39
    /** @noinspection PhpMissingParentCallCommonInspection
40
     * @inheritdoc
41
     */
42
    public function getDataFromRequest($request)
43
    {
44
        $elements = $this->getElements();
45
        if (is_array($request)) {
46
            foreach ($elements as $key => $element) {
47
                $element->setChecked(in_array($key, $request));
48
            }
49
        } else {
50
            foreach ($elements as $key => $element) {
51
                $element->setChecked(false);
52
            }
53
        }
54
55
        return $this;
56
    }
57
58
    /**
59
     * @return Nip\Form\Elements\AbstractElement
60
     */
61
    public function getNewElement()
62
    {
63
        $element = $this->getForm()->getNewElement('checkbox');
64
        $name = $this->getName();
0 ignored issues
show
Are you sure the assignment to $name 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...
65
        if (!strpos($name, '[]')) {
0 ignored issues
show
$name of type null is incompatible with the type string expected by parameter $haystack of strpos(). ( Ignorable by Annotation )

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

65
        if (!strpos(/** @scrutinizer ignore-type */ $name, '[]')) {
Loading history...
66
            $name = $name . '[]';
67
        }
68
        $element->setName($name);
69
70
        return $element;
71
    }
72
73
    /**
74
     * @return bool
75
     */
76
    public function isRequestArray()
77
    {
78
        return true;
79
    }
80
}
81