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

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