Passed
Push — master ( 79bb78...087da0 )
by Alexander
02:53
created

Email::beforeRender()   C

Complexity

Conditions 12
Paths 33

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 17
c 1
b 0
f 0
nc 33
nop 0
dl 0
loc 27
ccs 18
cts 18
cp 1
crap 12
rs 6.9666

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\BeforeValidationInterface;
17
use Yiisoft\Validator\Rule\HasLength;
18
use Yiisoft\Validator\Rule\Regex;
19
use Yiisoft\Validator\Rule\Required;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Validator\Rule\Required was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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