Passed
Push — master ( 203229...773eda )
by Bruno
09:51
created

RenderableBootstrapWrapperTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A wrapper() 0 10 1
B bootstrapify() 0 55 6
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\Bootstrap;
4
5
use Formularium\Field;
6
use Formularium\HTMLNode;
7
use Formularium\Frontend\HTML\Renderable;
8
9
trait RenderableBootstrapWrapperTrait
10
{
11
    public function wrapper($value, Field $field, HTMLNode $previous): HTMLNode
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

11
    public function wrapper(/** @scrutinizer ignore-unused */ $value, Field $field, HTMLNode $previous): HTMLNode

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
12
    {
13
        /** @var HTMLNode $base */
14
        $previous->addAttributes([
15
            'class' => "form-group",
16
            'data-attribute' => $field->getName(),
17
            'data-datatype' => $field->getDatatype()->getName(),
18
            'data-basetype' => $field->getDatatype()->getBasetype()
19
        ]);
20
        return $previous;
21
    }
22
23
    /**
24
     *
25
     *
26
     * @param mixed $value
27
     * @param Field $field
28
     * @param HTMLNode $previous
29
     * @return HTMLNode
30
     */
31
    public function bootstrapify($value, Field $field, HTMLNode $previous, $tag = 'input'): HTMLNode
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

31
    public function bootstrapify(/** @scrutinizer ignore-unused */ $value, Field $field, HTMLNode $previous, $tag = 'input'): HTMLNode

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
    {
33
        // add extra classes
34
        $input = $previous->get($tag)[0];
35
        $input->addAttributes([
36
            'class' => 'form-control',
37
        ]);
38
        $comment = $previous->get('.formularium-comment');
39
        if (!empty($comment)) {
40
            $comment[0]->setTag('small')->addAttributes([
41
                'class' => 'form-text text-muted',
42
            ]);
43
        }
44
        $size = $field->getRenderable(Renderable::SIZE, '');
45
        switch ($size) {
46
            case Renderable::SIZE_LARGE:
47
                $input->addAttribute('class', 'form-control-lg');
48
                break;
49
            case Renderable::SIZE_SMALL:
50
                $input->addAttribute('class', 'form-control-sm');
51
                break;
52
        }
53
54
        $icon = $field->getRenderable(Renderable::ICON, '');
55
        if ($icon) {
56
            $iconData = [];
57
            $iconPack = $field->getRenderable(Renderable::ICON_PACK, '');
58
            if ($iconPack) {
59
                $iconData[] = $iconPack;
60
            }
61
            $iconData[] = $icon;
62
            $group = HTMLNode::factory(
63
                'div',
64
                [ 'class' => "input-group mb-3" ],
65
                [
66
                    HTMLNode::factory(
67
                        'div',
68
                        [ 'class' => "input-group-prepend" ],
69
                        HTMLNode::factory(
70
                            'span',
71
                            [ 'class' => "input-group-text" ],
72
                            HTMLNode::factory(
73
                                'i',
74
                                [ 'class' => $iconData ],
75
                                []
76
                            )
77
                        )
78
                    ),
79
                    clone $input
80
                ]
81
            );
82
            $input->replace($group);
83
        }
84
85
        return $previous;
86
    }
87
}
88