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

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