CheckBox::checkValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Del\Form\Field;
6
7
use Del\Form\Renderer\Field\CheckboxRender;
8
use Del\Form\Traits\CanRenderInlineTrait;
9
use Del\Form\Traits\HasOptionsTrait;
10
11
class CheckBox extends FieldAbstract implements ArrayValueInterface
12
{
13
14
    use CanRenderInlineTrait;
15
    use HasOptionsTrait;
16
17
    /*
18
     * We end up ignoring this during rendering Checkboxes, see the renderer for info
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
    }
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
    }
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
}
58