Passed
Pull Request — main (#4)
by Peter
06:44 queued 03:43
created

Button::createFromUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 8
dl 0
loc 18
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Html\Factory;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Html\Attribute;
9
use AbterPhp\Framework\Html\Component\Button as ButtonComponent;
10
use AbterPhp\Framework\Html\Component\ButtonWithIcon;
11
use AbterPhp\Framework\Html\Helper\Attributes;
12
use AbterPhp\Framework\Html\Tag;
13
use Opulence\Routing\Urls\UrlException;
14
use Opulence\Routing\Urls\UrlGenerator;
15
16
class Button
17
{
18
    /** @var UrlGenerator */
19
    protected UrlGenerator $urlGenerator;
20
21
    /** @var array<string,Attribute> */
22
    protected array $attributes;
23
24
    /** @var array<string,Attribute> */
25
    protected array $iconAttributes;
26
27
    /** @var array<string,Attribute> */
28
    protected array $textAttributes;
29
30
    protected string $iconTag = Html5::TAG_I;
31
    protected string $textTag = Html5::TAG_SPAN;
32
33
    /**
34
     * ButtonFactory constructor.
35
     *
36
     * @param UrlGenerator                 $urlGenerator
37
     * @param array<string,Attribute>|null $textAttributes
38
     * @param array<string,Attribute>|null $iconAttributes
39
     * @param array<string,Attribute>|null $attributes
40
     * @param string                       $textTag
41
     * @param string                       $iconTag
42
     */
43
    public function __construct(
44
        UrlGenerator $urlGenerator,
45
        ?array $textAttributes = null,
46
        ?array $iconAttributes = null,
47
        ?array $attributes = null,
48
        string $textTag = Html5::TAG_SPAN,
49
        string $iconTag = Html5::TAG_I
50
    ) {
51
        $this->urlGenerator   = $urlGenerator;
52
        $this->textAttributes = $textAttributes ?? [];
53
        $this->iconAttributes = $iconAttributes ?? [];
54
        $this->attributes     = $attributes ?? [];
55
        $this->iconTag        = $iconTag;
56
        $this->textTag        = $textTag;
57
    }
58
59
    /**
60
     * @param string                       $text
61
     * @param string                       $url
62
     * @param string                       $icon
63
     * @param array<string,Attribute>|null $textAttributes
64
     * @param array<string,Attribute>|null $iconAttributes
65
     * @param string[]                     $intents
66
     * @param array<string,Attribute>|null $attributes
67
     * @param string|null                  $tag
68
     *
69
     * @return ButtonComponent
70
     */
71
    public function createFromUrl(
72
        string $text,
73
        string $url,
74
        string $icon = '',
75
        ?array $textAttributes = null,
76
        ?array $iconAttributes = null,
77
        $intents = [],
78
        ?array $attributes = null,
79
        ?string $tag = Html5::TAG_A
80
    ): ButtonComponent {
81
        $attributes                   ??= [];
82
        $attributes[Html5::ATTR_HREF] = new Attribute(Html5::ATTR_HREF, $url);
83
84
        if ($icon) {
85
            return $this->createWithIcon($text, $icon, $textAttributes, $iconAttributes, $intents, $attributes, $tag);
86
        }
87
88
        return $this->createSimple($text, $intents, $attributes, $tag);
89
    }
90
91
    /**
92
     * TODO: Create Opulence issue
93
     *
94
     * @suppress PhanTypeMismatchArgument issue with Opulence\Routing\Urls\UrlGenerator::createFromName
95
     *
96
     * @param string                       $text
97
     * @param string                       $urlName
98
     * @param string[]                     $urlArgs
99
     * @param string                       $icon
100
     * @param array<string,Attribute>|null $textAttributes
101
     * @param array<string,Attribute>|null $iconAttributes
102
     * @param string[]                     $intents
103
     * @param array<string,Attribute>|null $attributes
104
     * @param string|null                  $tag
105
     *
106
     * @return ButtonComponent
107
     * @throws URLException
108
     */
109
    public function createFromName(
110
        string $text,
111
        string $urlName,
112
        array $urlArgs,
113
        string $icon = '',
114
        ?array $textAttributes = null,
115
        ?array $iconAttributes = null,
116
        $intents = [],
117
        ?array $attributes = null,
118
        ?string $tag = Html5::TAG_A
119
    ): ButtonComponent {
120
        $url = $this->urlGenerator->createFromName($urlName, ...$urlArgs);
121
122
        $attributes                   ??= [];
123
        $attributes[Html5::ATTR_HREF] = new Attribute(Html5::ATTR_HREF, $url);
124
125
        if ($icon) {
126
            return $this->createWithIcon($text, $icon, $textAttributes, $iconAttributes, $intents, $attributes, $tag);
127
        }
128
129
        return $this->createSimple($text, $intents, $attributes, $tag);
130
    }
131
132
    /**
133
     * @param string                       $text
134
     * @param string[]                     $intents
135
     * @param array<string,Attribute>|null $attributes
136
     * @param string|null                  $tag
137
     *
138
     * @return ButtonComponent
139
     */
140
    public function createSimple(string $text, array $intents, ?array $attributes, ?string $tag): ButtonComponent
141
    {
142
        $attributes = Attributes::merge($attributes, $this->attributes);
143
144
        return new ButtonComponent($text, $intents, $attributes, $tag);
145
    }
146
147
    /**
148
     * @param string                       $text
149
     * @param string                       $icon
150
     * @param array<string,Attribute>|null $textAttributes
151
     * @param array<string,Attribute>|null $iconAttributes
152
     * @param string[]                     $intents
153
     * @param array<string,Attribute>|null $attributes
154
     * @param string|null                  $tag
155
     *
156
     * @return ButtonWithIcon
157
     */
158
    public function createWithIcon(
159
        string $text,
160
        string $icon,
161
        ?array $textAttributes = null,
162
        ?array $iconAttributes = null,
163
        array $intents = [],
164
        ?array $attributes = null,
165
        ?string $tag = null
166
    ): ButtonWithIcon {
167
        $iconAttributes ??= [];
168
        $iconAttributes = Attributes::merge($iconAttributes, $this->iconAttributes);
169
170
        $textAttributes ??= [];
171
        $textAttributes = Attributes::merge($textAttributes, $this->textAttributes);
172
173
        $textComponent = new Tag($text, [], $textAttributes, $this->textTag);
174
        $iconComponent = new Tag($icon, [], $iconAttributes, $this->iconTag);
175
176
        $attributes ??= [];
177
        $attributes = Attributes::merge($attributes, $this->attributes);
178
179
        return new ButtonWithIcon($textComponent, $iconComponent, $intents, $attributes, $tag);
180
    }
181
}
182