Passed
Pull Request — master (#192)
by Sergei
02:45
created

AbstractInputField::generateLabel()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 6
nop 0
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
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 67
    final protected function getInputName(): string
83
    {
84 67
        return HtmlForm::getInputName($this->getFormModel(), $this->attribute);
85
    }
86
87 67
    final protected function getInputTagAttributes(): array
88
    {
89 67
        $attributes = $this->inputTagAttributes;
90
91 67
        $this->prepareIdInInputTagAttributes($attributes);
92
93 67
        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 said class. However, the method does not exist in Yiisoft\Form\Field\Checkbox or Yiisoft\Form\Field\Range or Yiisoft\Form\Field\Base\AbstractDateTimeField or Yiisoft\Form\Field\Hidden or Yiisoft\Form\Field\Date or Yiisoft\Form\Field\DateTimeLocal. Are you sure you never get one of those? ( 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 67
        return $attributes;
99
    }
100
101 67
    final protected function prepareIdInInputTagAttributes(array &$attributes): void
102
    {
103
        /** @var mixed $idFromTag */
104 67
        $idFromTag = $attributes['id'] ?? null;
105 67
        if ($idFromTag !== null) {
106 2
            $this->inputIdFromTag = (string) $idFromTag;
107
        }
108
109 67
        if ($this->setInputIdAttribute) {
110 65
            if ($this->inputId !== null) {
111 2
                $attributes['id'] = $this->inputId;
112 63
            } elseif ($idFromTag === null) {
113 62
                $attributes['id'] = $this->getInputId();
114
            }
115
        }
116
    }
117
118 52
    final protected function generateLabel(): string
119
    {
120 52
        $label = Label::widget($this->labelConfig)
121 52
            ->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 52
        if ($this->setInputIdAttribute === false) {
124 2
            $label = $label->useInputIdAttribute(false);
125
        }
126
127 52
        if ($this->inputId !== null) {
128 2
            $label = $label->forId($this->inputId);
129 50
        } elseif ($this->inputIdFromTag !== null) {
130 1
            $label = $label->forId($this->inputIdFromTag);
131
        }
132
133 52
        return $label->render();
134
    }
135
136 67
    final protected function generateHint(): string
137
    {
138 67
        return Hint::widget($this->hintConfig)
139 67
            ->attribute($this->getFormModel(), $this->attribute)
140 67
            ->render();
141
    }
142
143 67
    final protected function generateError(): string
144
    {
145 67
        return Error::widget($this->errorConfig)
146 67
            ->attribute($this->getFormModel(), $this->attribute)
147 67
            ->render();
148
    }
149
150 67
    private function isUsePlaceholder(): bool
151
    {
152 67
        $traits = class_uses($this);
153 67
        return in_array(PlaceholderTrait::class, $traits, true);
154
    }
155
}
156