Completed
Push — master ( 8431a9...0bccd4 )
by Gabriel
04:28
created

Nip_Form_Element_CheckboxGroup   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 25
c 1
b 0
f 0
dl 0
loc 71
ccs 0
cts 27
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDataFromRequest() 0 14 4
A getValue() 0 13 4
A getNewElement() 0 10 2
A isRequestArray() 0 3 1
A setValue() 0 3 1
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
Unused Code introduced by
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();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data[] is correct as $element->getValue() targeting Nip\Form\Elements\AbstractElement::getValue() 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...
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,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_Element_Abstract
60
     */
61
    public function getNewElement()
62
    {
63
        $element = $this->getForm()->getNewElement('checkbox');
64
        $name = $this->getName();
0 ignored issues
show
Bug introduced by
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, '[]')) {
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