Passed
Pull Request — master (#192)
by Sergei
07:08 queued 03:58
created

InputField   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 92.59%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 48
c 1
b 0
f 0
dl 0
loc 116
ccs 50
cts 54
cp 0.9259
rs 10
wmc 19

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getInputTagAttributes() 0 12 2
A inputId() 0 5 1
A generateError() 0 5 1
A inputTagAttributes() 0 5 1
A prepareIdInInputTagAttributes() 0 13 5
A isUsePlaceholder() 0 4 1
A setInputIdAttribute() 0 5 1
A form() 0 5 1
A generateHint() 0 5 1
A generateLabel() 0 16 4
A getInputName() 0 3 1
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 InputField extends PartsField
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
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the ID
26
     * attribute of a form element in the same document.
27
     *
28
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
29
     */
30
    final public function form(string $value): static
31
    {
32
        $new = clone $this;
33
        $new->inputTagAttributes['form'] = $value;
34
        return $new;
35
    }
36
37 2
    final public function inputId(?string $inputId): static
38
    {
39 2
        $new = clone $this;
40 2
        $new->inputId = $inputId;
41 2
        return $new;
42
    }
43
44 2
    final public function setInputIdAttribute(bool $value): static
45
    {
46 2
        $new = clone $this;
47 2
        $new->setInputIdAttribute = $value;
48 2
        return $new;
49
    }
50
51 6
    final public function inputTagAttributes(array $attributes): static
52
    {
53 6
        $new = clone $this;
54 6
        $new->inputTagAttributes = $attributes;
55 6
        return $new;
56
    }
57
58 74
    final protected function getInputName(): string
59
    {
60 74
        return HtmlForm::getInputName($this->getFormModel(), $this->attribute);
61
    }
62
63 70
    final protected function getInputTagAttributes(): array
64
    {
65 70
        $attributes = $this->inputTagAttributes;
66
67 70
        $this->prepareIdInInputTagAttributes($attributes);
68
69 70
        if ($this->isUsePlaceholder()) {
70
            /** @psalm-suppress UndefinedMethod */
71 42
            $this->preparePlaceholderInInputTagAttributes($attributes);
0 ignored issues
show
Bug introduced by
The method preparePlaceholderInInputTagAttributes() does not exist on Yiisoft\Form\Field\Base\InputField. It seems like you code against a sub-type of Yiisoft\Form\Field\Base\InputField 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

71
            $this->/** @scrutinizer ignore-call */ 
72
                   preparePlaceholderInInputTagAttributes($attributes);
Loading history...
72
        }
73
74 70
        return $attributes;
75
    }
76
77 70
    final protected function prepareIdInInputTagAttributes(array &$attributes): void
78
    {
79
        /** @var mixed $idFromTag */
80 70
        $idFromTag = $attributes['id'] ?? null;
81 70
        if ($idFromTag !== null) {
82 2
            $this->inputIdFromTag = (string) $idFromTag;
83
        }
84
85 70
        if ($this->setInputIdAttribute) {
86 68
            if ($this->inputId !== null) {
87 2
                $attributes['id'] = $this->inputId;
88 66
            } elseif ($idFromTag === null) {
89 65
                $attributes['id'] = $this->getInputId();
90
            }
91
        }
92
    }
93
94 59
    final protected function generateLabel(): string
95
    {
96 59
        $label = Label::widget($this->labelConfig)
97 59
            ->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\RadioList or Yiisoft\Form\Field\Base\InputField or Yiisoft\Form\Field\CheckboxList. ( Ignorable by Annotation )

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

97
            ->/** @scrutinizer ignore-call */ attribute($this->getFormModel(), $this->attribute);
Loading history...
98
99 59
        if ($this->setInputIdAttribute === false) {
100 2
            $label = $label->useInputIdAttribute(false);
101
        }
102
103 59
        if ($this->inputId !== null) {
104 2
            $label = $label->forId($this->inputId);
105 57
        } elseif ($this->inputIdFromTag !== null) {
106 1
            $label = $label->forId($this->inputIdFromTag);
107
        }
108
109 59
        return $label->render();
110
    }
111
112 74
    final protected function generateHint(): string
113
    {
114 74
        return Hint::widget($this->hintConfig)
115 74
            ->attribute($this->getFormModel(), $this->attribute)
116 74
            ->render();
117
    }
118
119 74
    final protected function generateError(): string
120
    {
121 74
        return Error::widget($this->errorConfig)
122 74
            ->attribute($this->getFormModel(), $this->attribute)
123 74
            ->render();
124
    }
125
126 70
    private function isUsePlaceholder(): bool
127
    {
128 70
        $traits = class_uses($this);
129 70
        return in_array(PlaceholderTrait::class, $traits, true);
130
    }
131
}
132