Passed
Pull Request — master (#192)
by Sergei
03:23
created

AbstractInputField::generateHint()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Base;
6
7
use Yiisoft\Form\Field\Part\Error;
8
use Yiisoft\Form\Field\Part\Hint;
9
use Yiisoft\Form\Field\Part\Label;
10
use Yiisoft\Form\Helper\HtmlForm;
11
12
use function in_array;
13
14
abstract class AbstractInputField extends AbstractField
15
{
16
    use FormAttributeTrait;
17
18
    protected ?string $inputId = null;
19
    protected ?string $inputIdFromTag = null;
20
    protected bool $setInputIdAttribute = true;
21
22
    protected array $inputTagAttributes = [];
23
24
    /**
25
     * Identifies the element (or elements) that describes the object.
26
     *
27
     * @link https://w3c.github.io/aria/#aria-describedby
28
     */
29
    final public function ariaDescribedBy(string $value): static
30
    {
31
        $new = clone $this;
32
        $new->inputTagAttributes['aria-describedby'] = $value;
33
        return $new;
34
    }
35
36
    /**
37
     * Defines a string value that labels the current element.
38
     *
39
     * @link https://w3c.github.io/aria/#aria-label
40
     */
41
    final public function ariaLabel(string $value): static
42
    {
43
        $new = clone $this;
44
        $new->inputTagAttributes['aria-label'] = $value;
45
        return $new;
46
    }
47
48
    /**
49
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the ID
50
     * attribute of a form element in the same document.
51
     *
52
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
53
     */
54
    final public function form(string $value): static
55
    {
56
        $new = clone $this;
57
        $new->inputTagAttributes['form'] = $value;
58
        return $new;
59
    }
60
61 2
    final public function inputId(?string $inputId): static
62
    {
63 2
        $new = clone $this;
64 2
        $new->inputId = $inputId;
65 2
        return $new;
66
    }
67
68 2
    final public function setInputIdAttribute(bool $value): static
69
    {
70 2
        $new = clone $this;
71 2
        $new->setInputIdAttribute = $value;
72 2
        return $new;
73
    }
74
75 6
    final public function inputTagAttributes(array $attributes): static
76
    {
77 6
        $new = clone $this;
78 6
        $new->inputTagAttributes = $attributes;
79 6
        return $new;
80
    }
81
82 72
    final protected function getInputName(): string
83
    {
84 72
        return HtmlForm::getInputName($this->getFormModel(), $this->attribute);
85
    }
86
87 68
    final protected function getInputTagAttributes(): array
88
    {
89 68
        $attributes = $this->inputTagAttributes;
90
91 68
        $this->prepareIdInInputTagAttributes($attributes);
92
93 68
        if ($this->isUsePlaceholder()) {
94
            /** @psalm-suppress UndefinedMethod */
95 40
            $this->preparePlaceholderInInputTagAttributes($attributes);
0 ignored issues
show
Bug introduced by
The method preparePlaceholderInInputTagAttributes() does not exist on Yiisoft\Form\Field\Base\AbstractInputField. It seems like you code against a sub-type of Yiisoft\Form\Field\Base\AbstractInputField such as Yiisoft\Form\Field\Email or Yiisoft\Form\Field\Url or Yiisoft\Form\Field\Text or Yiisoft\Form\Field\Number or Yiisoft\Form\Field\Telephone or Yiisoft\Form\Field\Password or Yiisoft\Form\Field\Textarea. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
            $this->/** @scrutinizer ignore-call */ 
96
                   preparePlaceholderInInputTagAttributes($attributes);
Loading history...
96
        }
97
98 68
        return $attributes;
99
    }
100
101 68
    final protected function prepareIdInInputTagAttributes(array &$attributes): void
102
    {
103
        /** @var mixed $idFromTag */
104 68
        $idFromTag = $attributes['id'] ?? null;
105 68
        if ($idFromTag !== null) {
106 2
            $this->inputIdFromTag = (string) $idFromTag;
107
        }
108
109 68
        if ($this->setInputIdAttribute) {
110 66
            if ($this->inputId !== null) {
111 2
                $attributes['id'] = $this->inputId;
112 64
            } elseif ($idFromTag === null) {
113 63
                $attributes['id'] = $this->getInputId();
114
            }
115
        }
116
    }
117
118 57
    final protected function generateLabel(): string
119
    {
120 57
        $label = Label::widget($this->labelConfig)
121 57
            ->attribute($this->getFormModel(), $this->attribute);
0 ignored issues
show
Bug introduced by
The method attribute() does not exist on Yiisoft\Widget\Widget. It seems like you code against a sub-type of Yiisoft\Widget\Widget such as Yiisoft\Form\Field\Part\Label or Yiisoft\Form\Field\Part\Error or Yiisoft\Form\Field\Part\Hint or Yiisoft\Form\Field\Base\AbstractInputField. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
            ->/** @scrutinizer ignore-call */ attribute($this->getFormModel(), $this->attribute);
Loading history...
122
123 57
        if ($this->setInputIdAttribute === false) {
124 2
            $label = $label->useInputIdAttribute(false);
125
        }
126
127 57
        if ($this->inputId !== null) {
128 2
            $label = $label->forId($this->inputId);
129 55
        } elseif ($this->inputIdFromTag !== null) {
130 1
            $label = $label->forId($this->inputIdFromTag);
131
        }
132
133 57
        return $label->render();
134
    }
135
136 72
    final protected function generateHint(): string
137
    {
138 72
        return Hint::widget($this->hintConfig)
139 72
            ->attribute($this->getFormModel(), $this->attribute)
140 72
            ->render();
141
    }
142
143 72
    final protected function generateError(): string
144
    {
145 72
        return Error::widget($this->errorConfig)
146 72
            ->attribute($this->getFormModel(), $this->attribute)
147 72
            ->render();
148
    }
149
150 68
    private function isUsePlaceholder(): bool
151
    {
152 68
        $traits = class_uses($this);
153 68
        return in_array(PlaceholderTrait::class, $traits, true);
154
    }
155
}
156