Passed
Push — master ( d764eb...5a8560 )
by Bruno
06:29
created

Renderable_choice::editableRadio()   B

Complexity

Conditions 9
Paths 26

Size

Total Lines 54
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 31
c 1
b 0
f 0
nc 26
nop 3
dl 0
loc 54
ccs 0
cts 45
cp 0
crap 90
rs 8.0555

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Formularium\Frontend\HTML\Renderable;
4
5
use Formularium\Datatype;
6
use Formularium\Datatype\Datatype_choice;
7
use Formularium\Field;
8
use Formularium\Frontend\HTML\Framework;
9
use Formularium\Frontend\HTML\Renderable;
10
use Formularium\HTMLElement;
11
12
class Renderable_choice extends Renderable
13
{
14
    public const FORMAT_CHOOSER = 'format_chooser';
15
    public const FORMAT_CHOOSER_SELECT = 'format_chooser_select';
16
    public const FORMAT_CHOOSER_RADIO = 'format_chooser_radio';
17
    public const LAYOUT_RADIO = 'inline';
18
    public const LAYOUT_RADIO_INLINE = 'inline';
19
20
    use \Formularium\Frontend\HTML\RenderableViewableTrait {
21
        viewable as _viewable;
22
    }
23
    
24
    public function viewable($value, Field $field, HTMLElement $previous): HTMLElement
25
    {
26
        $formatted = $field->getDatatype()->format($value, $field);
27
28
        return $this->_viewable($formatted, $field, $previous);
29
    }
30
31
    public function editable($value, Field $field, HTMLElement $previous): HTMLElement
32
    {
33
        $format = $field->getExtension(static::FORMAT_CHOOSER, static::FORMAT_CHOOSER_SELECT);
34
        
35
        if ($format == static::FORMAT_CHOOSER_SELECT) {
36
            $element = $this->editableSelect($value, $field, $previous);
37
        } else {
38
            $element = $this->editableRadio($value, $field, $previous);
39
        }
40
41
        return $this->container($element, $field);
42
    }
43
44
    /**
45
     * @param mixed $value
46
     * @param Field $field
47
     * @param HTMLElement $previous
48
     * @return HTMLElement
49
     */
50
    protected function editableRadio($value, Field $field, HTMLElement $previous): HTMLElement
0 ignored issues
show
Unused Code introduced by
The parameter $previous 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

50
    protected function editableRadio($value, Field $field, /** @scrutinizer ignore-unused */ HTMLElement $previous): HTMLElement

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...
51
    {
52
        if (empty($value) && array_key_exists(static::DEFAULTVALUE, $field->getExtensions())) {
53
            $value = $field->getExtensions()[static::DEFAULTVALUE];
54
        }
55
56
        $element = new HTMLElement('div', ['class' => 'formularium-radio-group']);
57
58
        $idcounter = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $idcounter is dead and can be removed.
Loading history...
59
        /**
60
         * @var Datatype_choice $field
61
         */
62
        $datatype = $field->getDatatype();
0 ignored issues
show
Bug introduced by
The method getDatatype() does not exist on Formularium\Datatype\Datatype_choice. ( Ignorable by Annotation )

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

62
        /** @scrutinizer ignore-call */ 
63
        $datatype = $field->getDatatype();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
        foreach ($datatype->getChoices() as $v => $label) {
64
            $input = new HTMLElement('input');
65
66
            // send ids to delete/edit data later correctly.
67
            if ($value !== null && $v == $value) {
68
                $input->addAttribute('checked', 'checked');
69
            }
70
            $elementname = $field->getName(); // 'loh:' . $this->name . '[' . $attrid . '][value]' . '[' . $attrid . ']';
71
            $id = $field->getName() . Framework::counter();
72
            $input->addAttributes([
73
                'id' => $id,
74
                'name' => $elementname,
75
                'data-attribute' => $field->getName(),
76
                'data-datatype' => $datatype->getName(),
77
                'data-basetype' => $datatype->getBasetype(),
78
                'value' => $value,
79
                'type' => 'radio'
80
            ]);
81
82
            if ($field->getValidators()[Datatype::REQUIRED] ?? false) {
0 ignored issues
show
Bug introduced by
The method getValidators() does not exist on Formularium\Datatype\Datatype_choice. ( Ignorable by Annotation )

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

82
            if ($field->/** @scrutinizer ignore-call */ getValidators()[Datatype::REQUIRED] ?? false) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
                $element->setAttribute('required', 'required');
84
            }
85
            foreach ([static::DISABLED, static::READONLY] as $p) {
86
                if ($field->getExtension($p, false)) {
0 ignored issues
show
Bug introduced by
The method getExtension() does not exist on Formularium\Datatype\Datatype_choice. ( Ignorable by Annotation )

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

86
                if ($field->/** @scrutinizer ignore-call */ getExtension($p, false)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
                    $input->setAttribute($p, $p);
88
                }
89
            }
90
    
91
            $li = new HTMLElement(
92
                'div',
93
                [
94
                    'class' => 'formularium-radio-item'
95
                ],
96
                [
97
                    $input,
98
                    new HTMLElement('label', ['class' => 'formularium-radio-label', 'for' => $id], [$label])
99
                ]
100
            );
101
            $element->addContent($li);
102
        }
103
        return $element;
104
    }
105
106
    /**
107
     * @param mixed $value
108
     * @param Field $field
109
     * @param HTMLElement $previous
110
     * @return HTMLElement
111
     */
112
    protected function editableSelect($value, Field $field, HTMLElement $previous): HTMLElement
0 ignored issues
show
Unused Code introduced by
The parameter $previous 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

112
    protected function editableSelect($value, Field $field, /** @scrutinizer ignore-unused */ HTMLElement $previous): HTMLElement

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...
113
    {
114
        $element = new HTMLElement('select');
115
        $element->setAttributes([
116
            'id' => $field->getName() . Framework::counter(),
117
            'name' => $field->getName(),
118
            'class' => '',
119
            'data-attribute' => $field->getName(),
120
            'data-datatype' => $field->getDatatype()->getName(),
121
            'data-basetype' => $field->getDatatype()->getBasetype(),
122
            'title' => $field->getExtension(static::LABEL, '')
123
        ]);
124
125
        $optionEmpty = new HTMLElement('option', ['value' => ''], '', true);
0 ignored issues
show
Unused Code introduced by
The assignment to $optionEmpty is dead and can be removed.
Loading history...
126
        /**
127
         * @var Datatype_choice $field
128
         */
129
        $datatype = $field->getDatatype();
130
131
        if ($field->getValidators()[Datatype::REQUIRED] ?? false) {
132
            $optionEmpty = new HTMLElement('option', ['value' => ''], '', true);
133
            $element->addContent($optionEmpty);
134
        }
135
136
        foreach ($datatype->getChoices() as $v => $label) {
137
            $option = new HTMLElement('option', ['value' => $v], $label, true);
138
139
            if ($value == $v) {
140
                $option->setAttribute('selected', 'selected');
141
            }
142
            $element->addContent($option);
143
        }
144
145
        foreach ([static::DISABLED, static::READONLY] as $v) {
146
            if ($field->getExtension($v, false)) {
147
                $element->setAttribute($v, $v);
148
            }
149
        }
150
151
        return $element;
152
    }
153
}
154