Passed
Push — master ( bb4653...2ab0f7 )
by Bruno
07:17
created

Button::render()   B

Complexity

Conditions 7
Paths 19

Size

Total Lines 73
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 55
nc 19
nop 2
dl 0
loc 73
rs 8.0484
c 1
b 0
f 1

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\Bootstrap\Element;
4
5
use Formularium\Element;
6
use Formularium\Exception\Exception;
7
use Formularium\Frontend\HTML\Element\Button as HTMLButton;
8
use Formularium\HTMLNode;
9
use Formularium\Metadata;
10
use Formularium\MetadataParameter;
11
12
class Button extends Element
13
{
14
    public function render(array $parameters, HTMLNode $previous): HTMLNode
15
    {
16
        $previous->addAttribute('class', 'btn');
17
18
        $color = $parameters[HTMLButton::COLOR] ?? 'btn-primary';
19
        $colorMap = [
20
            HTMLButton::COLOR_PRIMARY => 'btn-primary',
21
            HTMLButton::COLOR_LINK => 'btn-link',
22
            HTMLButton::COLOR_INFO => 'btn-info',
23
            HTMLButton::COLOR_SUCCESS => 'btn-success',
24
            HTMLButton::COLOR_WARNING => 'btn-warning',
25
            HTMLButton::COLOR_ERROR => 'btn-error',
26
        ];
27
        if (array_key_exists($color, $colorMap)) {
28
            $color = $colorMap[$color];
29
        } else {
30
            $colors = [
31
                'btn-primary',
32
                'btn-secondary',
33
                'btn-success',
34
                'btn-danger',
35
                'btn-warning',
36
                'btn-info',
37
                'btn-light',
38
                'btn-dark',
39
                'btn-link',
40
            
41
                'btn-outline-primary',
42
                'btn-outline-secondary',
43
                'btn-outline-success',
44
                'btn-outline-danger',
45
                'btn-outline-warning',
46
                'btn-outline-info',
47
                'btn-outline-light',
48
                'btn-outline-dark',
49
            ];
50
            if (!in_array($color, $colors)) {
51
                throw new Exception('Invalid button color.');
52
            }
53
        }
54
        $previous->addAttribute('class', $color);
55
56
        $size = $parameters[self::SIZE] ?? '';
57
        switch ($size) {
58
            case self::SIZE_LARGE:
59
                $previous->addAttribute('class', 'btn-lg');
60
                break;
61
            case self::SIZE_SMALL:
62
                $previous->addAttribute('class', 'btn-sm');
63
                break;
64
        }
65
66
        $icon = $parameters[self::ICON] ?? '';
67
        if ($icon) {
68
            $iconData = [];
69
            $iconPack = $parameters[self::ICON_PACK] ?? '';
70
            if ($iconPack) {
71
                $iconData[] = $iconPack;
72
            }
73
            $iconData[] = $icon;
74
            $iconElement = HTMLNode::factory(
75
                'i',
76
                [
77
                    'class' => $iconData,
78
                    'aria-hidden' => "true"
79
                ],
80
                []
81
            );
82
            $previous->addAttribute('class', 'has-icons-left');
83
            $previous->appendContent($iconElement);
84
        }
85
86
        return $previous;
87
    }
88
89
    public static function getMetadata(): Metadata
90
    {
91
        $metadata = HTMLButton::getMetadata();
92
        $metadata->appendParameter(
93
            new MetadataParameter(
94
                HTMLButton::COLOR,
95
                'string',
96
                'Button color. Supports HTMLButton::COLOR_PRIMARY, HTMLButton::COLOR_LINK, HTMLButton::COLOR_INFO, HTMLButton::COLOR_SUCCESS, HTMLButton::COLOR_WARNING, HTMLButton::COLOR_ERROR and any Bootstrap button color classes. Default: primary.'
97
            ),
98
        );
99
        return $metadata;
100
    }
101
}
102