Passed
Push — master ( bc0996...c35245 )
by Bruno
05:47
created

Renderable_number::editable()   C

Complexity

Conditions 10
Paths 192

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 31
c 1
b 0
f 0
nc 192
nop 3
dl 0
loc 47
ccs 0
cts 37
cp 0
crap 110
rs 6.9

How to fix   Complexity   

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\Field;
7
use Formularium\Frontend\HTML\Framework;
8
use Formularium\HTMLElement;
9
10
class Renderable_number extends \Formularium\Renderable implements \Formularium\Frontend\HTML\RenderableInterface
11
{
12
    public const STEP = 'step';
13
14
    use \Formularium\Frontend\HTML\RenderableViewableTrait;
15
16
    public function editable($value, Field $f, HTMLElement $previous): HTMLElement
17
    {
18
        $input = new HTMLElement('input');
19
        /** @var \Formularium\Datatype\Datatype_number $datatype */
20
        $datatype = $f->getDatatype();
21
    
22
        $extensions = $f->getExtensions();
23
        $validators = $f->getValidators();
24
        $input->setAttributes([
25
            'id' => $f->getName() . Framework::counter(),
26
            'type' => ($extensions[static::HIDDEN] ?? false ? 'hidden' : 'number'),
27
            'name' => $f->getName(),
28
            'class' => '',
29
            'data-attribute' => $f->getName(),
30
            'data-datatype' => $datatype->getName(),
31
            'data-basetype' => $datatype->getBasetype(),
32
            'value' => $value,
33
            'title' => $f->getExtension(static::LABEL, '')
34
        ]);
35
36
        if (isset($extensions[static::PLACEHOLDER])) {
37
            $input->setAttribute('placeholder', $extensions[static::PLACEHOLDER]);
38
        }
39
        if ($validators[Datatype::REQUIRED] ?? false) {
40
            $input->setAttribute('required', 'required');
41
        }
42
        foreach ([static::DISABLED, static::READONLY] as $v) {
43
            if ($f->getExtension($v, false)) {
44
                $input->setAttribute($v, $v);
45
            }
46
        }
47
    
48
        if (array_key_exists(static::STEP, $validators)) {
49
            $input->setAttribute('step', $validators[static::STEP]);
50
        }
51
        if (isset($extensions[static::NO_AUTOCOMPLETE])) {
52
            $input->setAttribute('autocomplete', 'off');
53
        }
54
    
55
        $container = new HTMLElement(Framework::getEditableContainerTag(), [], $input);
56
        if (array_key_exists('label', $extensions)) {
57
            $container->prependContent(new HTMLElement('label', ['for' => $input->getAttribute('id')], $extensions['label']));
58
        }
59
        if (array_key_exists('comment', $extensions)) {
60
            $container->appendContent(new HTMLElement('div', ['class' => 'comment'], $extensions['comment']));
61
        }
62
        return $container;
63
    }
64
}
65