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

Renderable_string::editable()   F

Complexity

Conditions 12
Paths 384

Size

Total Lines 51
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 34
c 1
b 0
f 0
nc 384
nop 3
dl 0
loc 51
ccs 0
cts 33
cp 0
crap 156
rs 3.8333

How to fix   Long Method    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\HTMLElement;
10
11
class Renderable_string extends \Formularium\Renderable implements \Formularium\Frontend\HTML\RenderableInterface
12
{
13
    public const MAX_STRING_SIZE = 1024;
14
    
15
    use \Formularium\Frontend\HTML\RenderableViewableTrait;
16
17
    public function editable($value, Field $f, HTMLElement $previous): HTMLElement
18
    {
19
        $input = new HTMLElement('input');
20
21
        $extensions = $f->getExtensions();
22
        $validators = $f->getValidators();
23
        $input->setAttributes([
24
            'id' => $f->getName() . Framework::counter(),
25
            'type' => ($extensions[static::HIDDEN] ?? false ? 'hidden' : 'text'),
26
            'name' => $f->getName(),
27
            'class' => '',
28
            'data-attribute' => $f->getName(),
29
            'data-datatype' => $f->getDatatype()->getName(),
30
            'data-basetype' => $f->getDatatype()->getBasetype(),
31
            'value' => $value,
32
            'maxlength' => static::MAX_STRING_SIZE,
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(Datatype_string::MIN_LENGTH, $validators)) {
49
            $input->setAttribute('minlength', $validators[Datatype_string::MIN_LENGTH]);
50
        }
51
        if (array_key_exists(Datatype_string::MAX_LENGTH, $validators)
52
            && $validators[Datatype_string::MAX_LENGTH] < static::MAX_STRING_SIZE // TODO: datatype
53
        ) {
54
            $input->setAttribute('maxlength', $validators[Datatype_string::MAX_LENGTH]);
55
        }
56
        if (isset($extensions[static::NO_AUTOCOMPLETE])) {
57
            $input->setAttribute('autocomplete', 'off');
58
        }
59
60
        $container = new HTMLElement(Framework::getEditableContainerTag(), [], $input);
61
        if (array_key_exists('label', $extensions)) {
62
            $container->prependContent(new HTMLElement('label', ['for' => $input->getAttribute('id')], $extensions['label']));
63
        }
64
        if (array_key_exists('comment', $extensions)) {
65
            $container->appendContent(new HTMLElement('div', ['class' => 'comment'], $extensions['comment']));
66
        }
67
        return $container;
68
    }
69
}
70