Passed
Push — master ( acf3f9...98cfe3 )
by Derek Stephen
03:04
created

CheckBox   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 17
c 1
b 0
f 0
dl 0
loc 46
ccs 19
cts 19
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkValue() 0 5 1
A uncheckValue() 0 10 3
A getTag() 0 3 1
A init() 0 5 1
1
<?php
2
3
namespace Del\Form\Field;
4
5
use Del\Form\Renderer\Field\CheckboxRender;
6
use Del\Form\Traits\CanRenderInlineTrait;
7
use Del\Form\Traits\HasOptionsTrait;
8
9
class CheckBox extends FieldAbstract implements ArrayValueInterface
10
{
11
12
    use CanRenderInlineTrait;
13
    use HasOptionsTrait;
14
15
    /**
16
     * We end up ignoring this during rendering Checkboxes, see the renderer for info
17
     *
18
     * @return string
19
     */
20 8
    public function getTag(): string
21
    {
22 8
        return 'div';
23
    }
24
25 11
    public function init(): void
26
    {
27 11
        $this->setValue([]);
0 ignored issues
show
Bug introduced by
array() of type array is incompatible with the type string expected by parameter $value of Del\Form\Field\FieldAbstract::setValue(). ( Ignorable by Annotation )

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

27
        $this->setValue(/** @scrutinizer ignore-type */ []);
Loading history...
28 11
        $this->setRenderInline(false);
29 11
        $this->setRenderer(new CheckboxRender());
30 11
    }
31
32
    /**
33
     * @param $key
34
     */
35 2
    public function checkValue($key): void
36
    {
37 2
        $values = $this->getValue();
38 2
        $values[$key] = true;
39 2
        $this->setValue($values);
40 2
    }
41
42
    /**
43
     * @param $key
44
     */
45 1
    public function uncheckValue($key): void
46
    {
47 1
        $value = $this->getValue();
48
49 1
        if (is_array($value) && in_array($key, $value, true)) {
50 1
            $index = array_search($key, $value, true);
51 1
            unset($value[$index]);
52 1
            $this->setValue($value);
0 ignored issues
show
Bug introduced by
$value of type array is incompatible with the type string expected by parameter $value of Del\Form\Field\FieldAbstract::setValue(). ( Ignorable by Annotation )

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

52
            $this->setValue(/** @scrutinizer ignore-type */ $value);
Loading history...
53
        } else {
54 1
            $this->setValue(null);
55
        }
56
    }
57
}