TRadio::getValue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
namespace kalanis\kw_tree_controls\Controls;
4
5
6
/**
7
 * Trait TRadio
8
 * @package kalanis\kw_tree_controls\Controls
9
 * Trait for accessing grouped radio buttons as single element
10
 */
11
trait TRadio
12
{
13
    /** @var Radio[] */
14
    protected array $inputs = [];
15
16 1
    public function getValue()
17
    {
18 1
        foreach ($this->inputs as $input) {
19
            /** @var Radio $input */
20 1
            if ($input->getAttribute('checked')) {
21 1
                return $input->getOriginalValue();
22
            }
23
        }
24 1
        return null;
25
    }
26
27 2
    public function setValue($value): void
28
    {
29 2
        foreach ($this->inputs as $input) {
30
            /** @var Radio $input */
31 2
            if ($input->getOriginalValue() == $value) {
32 2
                $input->setAttribute('checked', 'checked');
33
            } else {
34 2
                $input->removeAttribute('checked');
35
            }
36
        }
37 2
    }
38
}
39