Passed
Push — master ( d98c91...5c9a86 )
by Bruno
08:10
created

Card::render()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 73
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 40
c 1
b 0
f 0
nc 12
nop 2
dl 0
loc 73
rs 8.9688

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\Bulma\Element;
4
5
use Formularium\Element;
6
use Formularium\HTMLNode;
7
use Formularium\Metadata;
8
use Formularium\Frontend\HTML\Element\Card as HTMLCard;
9
10
class Card extends Element
11
{
12
    public function render(array $parameters, HTMLNode $previous): HTMLNode
13
    {
14
        $base = HTMLNode::factory(
15
            'div',
16
            [
17
                'class' => 'formularium-card card'
18
            ]
19
        );
20
21
        if ($parameters[HTMLCard::IMAGE] ?? false) {
22
            $image = HTMLNode::factory(
23
                'div',
24
                [
25
                    'class' => 'card-image',
26
                ],
27
                HTMLNode::factory(
28
                    'figure',
29
                    [
30
                        'class' => 'image',
31
                    ],
32
                    HTMLNode::factory(
33
                        'img',
34
                        [
35
                            'src' => $parameters[HTMLCard::IMAGE],
36
                            'alt' => '' // TODO
37
                        ]
38
                    )
39
                )
40
            );
41
            $base->appendContent($image);
42
        }
43
44
        $body = HTMLNode::factory(
45
            'div',
46
            [
47
                'class' => 'card-content'
48
            ]
49
        );
50
        $base->appendContent($body);
51
52
        // title
53
        if ($parameters[HTMLCard::TITLE] ?? false) {
54
            $titleData = null;
55
            if ($parameters[HTMLCard::LINK] ?? false) {
56
                $titleData = HTMLNode::factory(
57
                    'a',
58
                    [ 'href' => $parameters[HTMLCard::LINK] ],
59
                    $parameters[HTMLCard::TITLE]
60
                );
61
            } else {
62
                $titleData = $parameters[HTMLCard::TITLE];
63
            }
64
            $title = HTMLNode::factory(
65
                'p',
66
                [
67
                    'class' => 'title is-4'
68
                ],
69
                $titleData
70
            );
71
            $body->appendContent($title);
72
        }
73
74
        if ($parameters[HTMLCard::CONTENT] ?? false) {
75
            $content = HTMLNode::factory(
76
                'div',
77
                [
78
                    'class' => 'content'
79
                ],
80
                $parameters[HTMLCard::CONTENT]
81
            );
82
            $body->appendContent($content);
83
        }
84
        return $base;
85
    }
86
87
    public static function getMetadata(): Metadata
88
    {
89
        $metadata = HTMLCard::getMetadata();
90
        return $metadata;
91
    }
92
}
93