Passed
Pull Request — master (#192)
by Alexander
28:18 queued 25:44
created

Email::beforeRender()   B

Complexity

Conditions 10
Paths 32

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 15
c 1
b 0
f 0
nc 32
nop 0
dl 0
loc 23
ccs 16
cts 16
cp 1
crap 10
rs 7.6666

How to fix   Complexity   

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
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\Field\Base\EnrichmentFromRules\EnrichmentFromRulesInterface;
9
use Yiisoft\Form\Field\Base\EnrichmentFromRules\EnrichmentFromRulesTrait;
10
use Yiisoft\Form\Field\Base\InputField;
11
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderInterface;
12
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
13
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
14
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
15
use Yiisoft\Html\Html;
16
use Yiisoft\Validator\Rule\HasLength;
17
use Yiisoft\Validator\Rule\Regex;
18
use Yiisoft\Validator\Rule\Required;
19
20
use function is_string;
21
22
/**
23
 * @link https://html.spec.whatwg.org/multipage/input.html#email-state-(type=email)
24
 * @link https://developer.mozilla.org/docs/Web/HTML/Element/input/email
25
 */
26
final class Email extends InputField implements EnrichmentFromRulesInterface, PlaceholderInterface, ValidationClassInterface
27
{
28
    use EnrichmentFromRulesTrait;
29
    use PlaceholderTrait;
30
    use ValidationClassTrait;
31
32
    /**
33
     * Maximum length of value.
34
     *
35
     * @param int|null $value A limit on the number of characters a user can input.
36
     *
37
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-maxlength
38
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-maxlength
39
     */
40 2
    public function maxlength(?int $value): self
41
    {
42 2
        $new = clone $this;
43 2
        $new->inputTagAttributes['maxlength'] = $value;
44 2
        return $new;
45
    }
46
47
    /**
48
     * Minimum length of value.
49
     *
50
     * @param int|null $value A lower bound on the number of characters a user can input.
51
     *
52
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-minlength
53
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-minlength
54
     */
55 2
    public function minlength(?int $value): self
56
    {
57 2
        $new = clone $this;
58 2
        $new->inputTagAttributes['minlength'] = $value;
59 2
        return $new;
60
    }
61
62
    /**
63
     * Allow to specify more than one value.
64
     *
65
     * @param bool $value Whether the user is to be allowed to specify more than one value.
66
     *
67
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-multiple
68
     */
69 2
    public function multiple(bool $value = true): self
70
    {
71 2
        $new = clone $this;
72 2
        $new->inputTagAttributes['multiple'] = $value;
73 2
        return $new;
74
    }
75
76
    /**
77
     * Pattern to be matched by the form control's value.
78
     *
79
     * @param string|null $value A regular expression against which the control's value.
80
     *
81
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-pattern
82
     */
83 2
    public function pattern(?string $value): self
84
    {
85 2
        $new = clone $this;
86 2
        $new->inputTagAttributes['pattern'] = $value;
87 2
        return $new;
88
    }
89
90
    /**
91
     * A boolean attribute that controls whether or not the user can edit the form control.
92
     *
93
     * @param bool $value Whether to allow the value to be edited by the user.
94
     *
95
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly
96
     */
97 2
    public function readonly(bool $value = true): self
98
    {
99 2
        $new = clone $this;
100 2
        $new->inputTagAttributes['readonly'] = $value;
101 2
        return $new;
102
    }
103
104
    /**
105
     * A boolean attribute. When specified, the element is required.
106
     *
107
     * @param bool $value Whether the control is required for form submission.
108
     *
109
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required
110
     */
111 2
    public function required(bool $value = true): self
112
    {
113 2
        $new = clone $this;
114 2
        $new->inputTagAttributes['required'] = $value;
115 2
        return $new;
116
    }
117
118
    /**
119
     * The size of the control.
120
     *
121
     * @param int|null $value The number of characters that allow the user to see while editing the element's value.
122
     *
123
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-size
124
     */
125 2
    public function size(?int $value): self
126
    {
127 2
        $new = clone $this;
128 2
        $new->inputTagAttributes['size'] = $value;
129 2
        return $new;
130
    }
131
132
    /**
133
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
134
     */
135 2
    public function disabled(bool $disabled = true): self
136
    {
137 2
        $new = clone $this;
138 2
        $new->inputTagAttributes['disabled'] = $disabled;
139 2
        return $new;
140
    }
141
142
    /**
143
     * Identifies the element (or elements) that describes the object.
144
     *
145
     * @link https://w3c.github.io/aria/#aria-describedby
146
     */
147 2
    public function ariaDescribedBy(?string $value): self
148
    {
149 2
        $new = clone $this;
150 2
        $new->inputTagAttributes['aria-describedby'] = $value;
151 2
        return $new;
152
    }
153
154
    /**
155
     * Defines a string value that labels the current element.
156
     *
157
     * @link https://w3c.github.io/aria/#aria-label
158
     */
159 2
    public function ariaLabel(?string $value): self
160
    {
161 2
        $new = clone $this;
162 2
        $new->inputTagAttributes['aria-label'] = $value;
163 2
        return $new;
164
    }
165
166
    /**
167
     * Focus on the control (put cursor into it) when the page loads. Only one form element could be in focus
168
     * at the same time.
169
     *
170
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-fe-autofocus
171
     */
172 2
    public function autofocus(bool $value = true): self
173
    {
174 2
        $new = clone $this;
175 2
        $new->inputTagAttributes['autofocus'] = $value;
176 2
        return $new;
177
    }
178
179
    /**
180
     * The `tabindex` attribute indicates that its element can be focused, and where it participates in sequential
181
     * keyboard navigation (usually with the Tab key, hence the name).
182
     *
183
     * It accepts an integer as a value, with different results depending on the integer's value:
184
     *
185
     * - A negative value (usually `tabindex="-1"`) means that the element is not reachable via sequential keyboard
186
     *   navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets
187
     *   with JavaScript.
188
     * - `tabindex="0"` means that the element should be focusable in sequential keyboard navigation, but its order is
189
     *   defined by the document's source order.
190
     * - A positive value means the element should be focusable in sequential keyboard navigation, with its order
191
     *   defined by the value of the number. That is, `tabindex="4"` is focused before `tabindex="5"`, but after
192
     *   `tabindex="3"`.
193
     *
194
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex
195
     */
196 2
    public function tabIndex(?int $value): self
197
    {
198 2
        $new = clone $this;
199 2
        $new->inputTagAttributes['tabindex'] = $value;
200 2
        return $new;
201
    }
202
203
    /**
204
     * @psalm-suppress MixedAssignment,MixedArgument Remove after fix https://github.com/yiisoft/validator/issues/225
205
     */
206 19
    protected function beforeRender(): void
207
    {
208 19
        parent::beforeRender();
209 19
        if ($this->enrichmentFromRules && $this->hasFormModelAndAttribute()) {
210 4
            $rules = $this->getFormModel()->getRules()[$this->getAttributeName()] ?? [];
211 4
            foreach ($rules as $rule) {
212 4
                if ($rule instanceof Required) {
213 1
                    $this->inputTagAttributes['required'] = true;
214
                }
215
216 4
                if ($rule instanceof HasLength) {
217 1
                    if (null !== $min = $rule->getOptions()['min']) {
218 1
                        $this->inputTagAttributes['minlength'] = $min;
219
                    }
220 1
                    if (null !== $max = $rule->getOptions()['max']) {
221 1
                        $this->inputTagAttributes['maxlength'] = $max;
222
                    }
223
                }
224
225 4
                if ($rule instanceof Regex) {
226 2
                    if (!($rule->getOptions()['not'])) {
227 1
                        $this->inputTagAttributes['pattern'] = Html::normalizeRegexpPattern(
228 1
                            $rule->getOptions()['pattern']
229
                        );
230
                    }
231
                }
232
            }
233
        }
234
    }
235
236 19
    protected function generateInput(): string
237
    {
238 19
        $value = $this->getAttributeValue();
239
240 19
        if (!is_string($value) && $value !== null) {
241 1
            throw new InvalidArgumentException('Email field requires a string or null value.');
242
        }
243
244 18
        $tagAttributes = $this->getInputTagAttributes();
245
246 18
        return Html::input('email', $this->getInputName(), $value, $tagAttributes)->render();
247
    }
248
249 14
    protected function prepareContainerTagAttributes(array &$attributes): void
250
    {
251 14
        if ($this->hasFormModelAndAttribute()) {
252 14
            $this->addValidationClassToTagAttributes(
253
                $attributes,
254 14
                $this->getFormModel(),
255 14
                $this->getAttributeName(),
256
            );
257
        }
258
    }
259
260 18
    protected function prepareInputTagAttributes(array &$attributes): void
261
    {
262 18
        $this->preparePlaceholderInInputTagAttributes($attributes);
263
    }
264
}
265