Passed
Push — master ( 0b0ed1...849c84 )
by Bruno
02:30
created

Renderable_string::editable()   C

Complexity

Conditions 11
Paths 192

Size

Total Lines 48
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 32
c 1
b 0
f 0
nc 192
nop 3
dl 0
loc 48
rs 6.55

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\Field;
6
use Formularium\Frontend\HTML\Framework;
7
use Formularium\HTMLElement;
8
9
class Renderable_string extends \Formularium\Renderable implements \Formularium\Frontend\HTML\RenderableInterface
10
{
11
    public const MIN_LENGTH = "min_length";
12
    public const MAX_LENGTH = "max_length";
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
        foreach ([static::DISABLED, static::READONLY, static::REQUIRED] as $v) {
40
            if ($f->getExtension($v, false)) {
41
                $input->setAttribute($v, $v);
42
            }
43
        }
44
45
        if (array_key_exists(static::MIN_LENGTH, $validators)) {
46
            $input->setAttribute('minlength', $validators[static::MIN_LENGTH]);
47
        }
48
        if (array_key_exists(static::MAX_LENGTH, $validators)
49
            && $validators[static::MAX_LENGTH] < static::MAX_STRING_SIZE
50
        ) {
51
            $input->setAttribute('maxlength', $validators[static::MAX_LENGTH]);
52
        }
53
        if (isset($extensions[static::NO_AUTOCOMPLETE])) {
54
            $input->setAttribute('autocomplete', 'off');
55
        }
56
57
        $container = new HTMLElement('div', [], $input);
58
        if (array_key_exists('label', $extensions)) {
59
            $container->prependContent(new HTMLElement('label', ['for' => $input->getAttribute('id')], $extensions['label']));
60
        }
61
        if (array_key_exists('comment', $extensions)) {
62
            $container->appendContent(new HTMLElement('div', ['class' => 'comment'], $extensions['comment']));
63
        }
64
        return $container;
65
    }
66
}
67