Passed
Push — master ( 089a6d...7b13b9 )
by Bruno
06:38
created

Renderable_file::editable()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 63
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 40
c 1
b 0
f 0
nc 36
nop 3
dl 0
loc 63
ccs 0
cts 60
cp 0
crap 56
rs 8.3466

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 declare(strict_types=1);
2
3
namespace Formularium\Frontend\Buefy\Renderable;
4
5
use Formularium\Datatype;
6
use Formularium\Datatype\Datatype_file;
7
use Formularium\Field;
8
use Formularium\Frontend\HTML\Renderable\Renderable_file as HTMLRenderable_file;
9
use Formularium\HTMLElement;
10
use Formularium\Renderable;
11
12
class Renderable_file extends \Formularium\Renderable
13
{
14
    use \Formularium\Frontend\HTML\RenderableViewableTrait;
15
16
    public function editable($value, Field $field, HTMLElement $previous): HTMLElement
17
    {
18
        $extensions = $field->getExtensions();
19
        $validators = $field->getValidators();
20
21
        $inputAtts = [
22
            'name' => $field->getName(),
23
            'v-model' => $field->getName(),
24
            'drag-drop' => ''
25
        ];
26
        if ($extensions[HTMLRenderable_file::ACCEPT] ?? false) {
27
            if (is_array($extensions[HTMLRenderable_file::ACCEPT])) {
28
                $accept = join(',', $extensions[HTMLRenderable_file::ACCEPT]);
29
            } else {
30
                $accept = $extensions[HTMLRenderable_file::ACCEPT];
31
            }
32
            $inputAtts['accept'] = htmlspecialchars($accept);
33
        }
34
        if ($validators[Datatype::REQUIRED] ?? false) {
35
            $inputAtts['required'] = 'required';
36
        }
37
        if ($validators[Datatype_file::MAX_SIZE] ?? false) {
38
            $inputAtts['data-max-size'] = $validators[Datatype_file::MAX_SIZE];
39
        }
40
        foreach ([static::DISABLED, static::READONLY] as $v) {
41
            if ($field->getExtension($v, false)) {
42
                $inputAtts[$v] = true;
43
            }
44
        }
45
46
        $container = HTMLElement::factory(
47
            'b-field',
48
            [],
49
            HTMLElement::factory(
50
                'b-upload',
51
                $inputAtts,
52
                HTMLElement::factory(
53
                    'section',
54
                    ['class' => 'section'],
55
                    HTMLElement::factory(
56
                        'div',
57
                        ['class' => "content has-text-centered"],
58
                        [
59
                            HTMLElement::factory(
60
                                'p',
61
                                [],
62
                                HTMLElement::factory(
63
                                    'b-icon',
64
                                    [ 'icon' => "upload", 'size' => "is-large" ]
65
                                )
66
                            ),
67
                            HTMLElement::factory(
68
                                'p',
69
                                [ 'class' => 'formularium-label'],
70
                                $extensions[Renderable::LABEL] ?? ''
71
                            )
72
                        ]
73
                    )
74
                )
75
            )
76
        );
77
78
        return $container;
79
    }
80
}
81