Passed
Push — master ( 31045a...52c533 )
by Bruno
06:54
created

Renderable_string::editable()   B

Complexity

Conditions 10
Paths 96

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 29
c 2
b 0
f 0
nc 96
nop 3
dl 0
loc 44
ccs 0
cts 30
cp 0
crap 110
rs 7.6666

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\Datatype\Datatype_string;
7
use Formularium\Field;
8
use Formularium\Frontend\HTML\Framework;
9
use Formularium\Frontend\HTML\Renderable;
10
use Formularium\HTMLElement;
11
12
class Renderable_string extends Renderable
13
{
14
    public const MAX_STRING_SIZE = 1024;
15
    
16
    use \Formularium\Frontend\HTML\RenderableViewableTrait;
17
18
    public function editable($value, Field $f, HTMLElement $previous): HTMLElement
19
    {
20
        $input = new HTMLElement('input');
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' : 'text'),
27
            'name' => $f->getName(),
28
            'class' => '',
29
            'data-attribute' => $f->getName(),
30
            'data-datatype' => $f->getDatatype()->getName(),
31
            'data-basetype' => $f->getDatatype()->getBasetype(),
32
            'value' => $value,
33
            'maxlength' => static::MAX_STRING_SIZE,
34
            'title' => $f->getExtension(static::LABEL, '')
35
        ]);
36
37
        if (isset($extensions[static::PLACEHOLDER])) {
38
            $input->setAttribute('placeholder', $extensions[static::PLACEHOLDER]);
39
        }
40
        if ($validators[Datatype::REQUIRED] ?? false) {
41
            $input->setAttribute('required', 'required');
42
        }
43
        foreach ([static::DISABLED, static::READONLY] as $v) {
44
            if ($f->getExtension($v, false)) {
45
                $input->setAttribute($v, $v);
46
            }
47
        }
48
49
        if (array_key_exists(Datatype_string::MIN_LENGTH, $validators)) {
50
            $input->setAttribute('minlength', $validators[Datatype_string::MIN_LENGTH]);
51
        }
52
        if (array_key_exists(Datatype_string::MAX_LENGTH, $validators)
53
            && $validators[Datatype_string::MAX_LENGTH] < static::MAX_STRING_SIZE // TODO: datatype
54
        ) {
55
            $input->setAttribute('maxlength', $validators[Datatype_string::MAX_LENGTH]);
56
        }
57
        if (isset($extensions[static::NO_AUTOCOMPLETE])) {
58
            $input->setAttribute('autocomplete', 'off');
59
        }
60
61
        return $this->container($input, $f);
62
    }
63
}
64