RadioRender::renderRadioInline()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 9
c 3
b 0
f 0
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.9666
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Del\Form\Renderer\Field;
4
5
use Del\Form\Field\FieldInterface;
6
use Del\Form\Field\Radio;
7
use DOMDocumentFragment;
8
use DOMElement;
9
use DOMNode;
10
use DOMText;
11
use InvalidArgumentException;
12
use LogicException;
13
14
class RadioRender extends AbstractFieldRender
15
{
16
    /** @var DOMDocumentFragment $div */
17
    private $fragment;
18
19
    private $counter = 0;
20
21
    /**
22
     * @param FieldInterface $field
23
     * @param DOMElement $element
24
     * @return DOMNode
25
     */
26 10
    public function renderBlock(FieldInterface $field, DOMElement $element)
27
    {
28
        // We don't really want a containing div, so we'll ignore $element
29
        // and instead create a DOMDocumentFragment
30 10
        unset($element);
31 10
        $this->fragment = $this->getDom()->createDocumentFragment();
32
33
        // Make sure the FieldInterface is actually a Radio
34 10
        if (!$field instanceof Radio) {
35 1
            throw new InvalidArgumentException('Must be a Del\Form\Field\Radio');
36
        }
37
38 9
        $inline = $field->isRenderInline();
39
40 9
        $options = $field->getOptions();
41 9
        if (empty($options)) {
42 1
            throw new LogicException('You must set at least one option.');
43
        }
44
45
        // Loop through each radio element (the options)
46 8
        foreach ($options as $value => $label) {
47 8
            $radio = $this->renderRadio($field, $value, $label, $inline);
48 8
            $this->fragment->appendChild($radio);
49
        }
50
51 8
        return $this->fragment;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fragment returns the type DOMDocumentFragment which is incompatible with the return type mandated by Del\Form\Renderer\Field\...ldRender::renderBlock() of DOMElement.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
52
    }
53
54
    /**
55
     * @param FieldInterface $field
56
     * @param $value
57
     * @param $labelText
58
     * @param $inline
59
     * @return DOMElement
60
     */
61 8
    private function renderRadio(FieldInterface $field, $value, $labelText, $inline)
62
    {
63 8
        $div = $this->createElement('div');
64 8
        $class = $inline ? 'form-check-inline' : 'form-check';
65 8
        $div->setAttribute('class', $class);
66 8
        $radio = $this->renderRadioInline($field, $value, $labelText);
67 8
        $label = $this->getLabel($field, $labelText);
68 8
        $div->appendChild($radio);
69 8
        $div->appendChild($label);
70
71 8
        return $div;
72
    }
73
74
    /**
75
     * @param FieldInterface $field
76
     * @param string $labelText
77
     * @return DOMElement
78
     */
79 8
    private function getLabel(FieldInterface $field, string $labelText): DOMElement
80
    {
81 8
        $this->counter ++;
82 8
        $label = $this->getDom()->createElement('label');
83 8
        $label->setAttribute('for', $field->getId() . $this->counter);
84 8
        $label->setAttribute('class', 'form-check-label');
85 8
        $text = $this->createText($labelText);
86 8
        $label->appendChild($text);
87
88 8
        return $label;
89
    }
90
91
    /**
92
     * @param FieldInterface $field
93
     * @param $value
94
     * @param $labelText
95
     * @return DOMElement
96
     */
97 8
    private function renderRadioInline(FieldInterface $field, $value, $labelText)
98
    {
99 8
        $radio = $this->createElement('input');
100 8
        $radio->setAttribute('class', 'form-check-input');
101 8
        $radio->setAttribute('type', 'radio');
102 8
        $radio->setAttribute('name', $field->getName());
103 8
        $radio->setAttribute('value', $value);
104 8
        $text = $this->createText($labelText);
0 ignored issues
show
Unused Code introduced by
The assignment to $text is dead and can be removed.
Loading history...
105
106 8
        if ($field->getValue() == $radio->getAttribute('value')) {
107 2
            $radio->setAttribute('checked', 'checked');
108
        }
109
110 8
        return $radio;
111
    }
112
}
113