Passed
Pull Request — master (#155)
by Wilmer
11:04
created

InputAttributes::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget\Attribute;
6
7
use Stringable;
8
9
trait InputAttributes
10
{
11
    /**
12
     * Focus on the control (put cursor into it) when the page loads.
13
     * Only one form element could be in focus at the same time.
14
     *
15
     * @param bool $value
16
     *
17
     * @return static
18
     *
19
     * @link https://www.w3.org/TR/html52/sec-forms.html#autofocusing-a-form-control-the-autofocus-attribute
20
     */
21 2
    public function autofocus(bool $value = true): self
22
    {
23 2
        $new = clone $this;
24 2
        $new->attributes['autofocus'] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25 2
        return $new;
26
    }
27
28
    /**
29
     * Set whether the element is disabled or not.
30
     *
31
     * If this attribute is set to `true`, the element is disabled. Disabled elements are usually drawn with grayed-out
32
     * text.
33
     * If the element is disabled, it does not respond to user actions, it cannot be focused, and the command event
34
     * will not fire. In the case of form elements, it will not be submitted. Do not set the attribute to true, as
35
     * this will suggest you can set it to `false` to enable the element again, which is not the case.
36
     *
37
     * @param bool $value
38
     *
39
     * @return static
40
     *
41
     * @link https://www.w3.org/TR/html52/sec-forms.html#element-attrdef-disabledformelements-disabled
42
     */
43 2
    public function disabled(bool $value = true): self
44
    {
45 2
        $new = clone $this;
46 2
        $new->attributes['disabled'] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47 2
        return $new;
48
    }
49
50
    /**
51
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the id
52
     * attribute of a {@see Form} element in the same document.
53
     *
54
     * @param string $value
55
     *
56
     * @return static
57
     *
58
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
59
     */
60 12
    public function form(string $value): self
61
    {
62 12
        $new = clone $this;
63 12
        $new->attributes['form'] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
64 12
        return $new;
65
    }
66
67
    /**
68
     * The name part of the name/value pair associated with this element for the purposes of form submission.
69
     *
70
     * @param string The name of the widget.
0 ignored issues
show
Bug introduced by
The type Yiisoft\Form\Widget\Attribute\The 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...
71
     *
72
     * @return static
73
     *
74
     * @link https://www.w3.org/TR/html52/sec-forms.html#element-attrdef-formelements-name
75
     */
76 2
    public function name(string $value): self
77
    {
78 2
        $new = clone $this;
79 2
        $new->attributes['name'] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
80 2
        return $new;
81
    }
82
83
    /**
84
     * If it is required to fill in a value in order to submit the form.
85
     *
86
     * @param bool $value
87
     *
88
     * @return static
89
     *
90
     * @link https://www.w3.org/TR/html52/sec-forms.html#the-required-attribute
91
     */
92 2
    public function required(bool $value = true): self
93
    {
94 2
        $new = clone $this;
95 2
        $new->attributes['required'] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
96 2
        return $new;
97
    }
98
99
    /**
100
     * The readonly attribute is a boolean attribute that controls whether the user can edit the form control.
101
     * When specified, the element is not mutable.
102
     *
103
     * @return static
104
     *
105
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly
106
     */
107 8
    public function readonly(): self
108
    {
109 8
        $new = clone $this;
110 8
        $new->attributes['readonly'] = true;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
111 8
        return $new;
112
    }
113
114
    /**
115
     * The value input tag.
116
     *
117
     * @param scalar|Stringable|null $value
118
     *
119
     * @return static
120
     *
121
     * @link https://www.w3.org/TR/html52/sec-forms.html#element-attrdef-input-value
122
     */
123 10
    public function value($value): self
124
    {
125 10
        $new = clone $this;
126 10
        $new->attributes['value'] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
127 10
        return $new;
128
    }
129
130
    /**
131
     * The tabindex global attribute indicates that its element can be focused, and where it participates in sequential
132
     * keyboard navigation (usually with the Tab key, hence the name).
133
     *
134
     * It accepts an integer as a value, with different results depending on the integer's value:
135
     *
136
     * - A negative value (usually tabindex="-1") means that the element is not reachable via sequential keyboard
137
     * navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets
138
     * with JavaScript.
139
     * - tabindex="0" means that the element should be focusable in sequential keyboard navigation, but its order is
140
     * defined by the document's source order.
141
     * - A positive value means the element should be focusable in sequential keyboard navigation, with its order
142
     * defined by the value of the number. That is, tabindex="4" is focused before tabindex="5", but after tabindex="3".
143
     *
144
     * @param int $value
145
     *
146
     * @return self
147
     *
148
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex
149
     */
150 2
    public function tabIndex(int $value = 0): self
151
    {
152 2
        $new = clone $this;
153 2
        $new->attributes['tabindex'] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
154 2
        return $new;
155
    }
156
}
157