Test Failed
Pull Request — master (#159)
by Sergei
03:08
created

AbstractField   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 73
dl 0
loc 184
rs 10
c 2
b 0
f 0
wmc 24

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getInputName() 0 3 1
A containerTag() 0 5 1
A generateContent() 0 10 1
A run() 0 13 2
A hintConfig() 0 5 1
A setInputIdAttribute() 0 5 1
A inputId() 0 5 1
A prepareIdInInputTag() 0 13 4
A hint() 0 5 1
A labelConfig() 0 5 1
A getInputId() 0 7 2
A generateError() 0 3 1
A generateLabel() 0 14 3
A generateHint() 0 5 1
A template() 0 5 1
A withoutContainer() 0 5 1
A label() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Base;
6
7
use Yiisoft\Form\Field\Part\Hint;
8
use Yiisoft\Form\Field\Part\Label;
9
use Yiisoft\Form\Helper\HtmlForm;
10
use Yiisoft\Html\Tag\Base\ContentTagInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Html\Tag\Base\ContentTagInterface 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...
11
use Yiisoft\Html\Tag\Base\Tag;
12
use Yiisoft\Html\Tag\Div;
13
use Yiisoft\Widget\Widget;
14
15
abstract class AbstractField extends Widget
16
{
17
    use FormAttributeTrait;
18
19
    private ?ContentTagInterface $containerTag = null;
20
    private bool $withoutContainer = false;
21
22
    private string $template = "{label}\n{input}\n{hint}\n{error}";
23
24
    private ?string $inputId = null;
25
    private bool $setInputIdAttribute = true;
26
27
    private array $labelConfig = [];
28
    private array $hintConfig = [];
29
30
    final public function containerTag(?ContentTagInterface $tag): self
31
    {
32
        $new = clone $this;
33
        $new->containerTag = $tag;
34
        return $new;
35
    }
36
37
    final public function withoutContainer(): self
38
    {
39
        $new = clone $this;
40
        $new->withoutContainer = true;
41
        return $new;
42
    }
43
44
    /**
45
     * Set layout template for render a field.
46
     *
47
     * @param string $template
48
     *
49
     * @return static
50
     */
51
    final public function template(string $template): self
52
    {
53
        $new = clone $this;
54
        $new->template = $template;
55
        return $new;
56
    }
57
58
    /**
59
     * @return static
60
     */
61
    final public function inputId(?string $inputId): self
62
    {
63
        $new = clone $this;
64
        $new->inputId = $inputId;
65
        return $new;
66
    }
67
68
    /**
69
     * @return static
70
     */
71
    final public function setInputIdAttribute(bool $value): self
72
    {
73
        $new = clone $this;
74
        $new->setInputIdAttribute = $value;
75
        return $new;
76
    }
77
78
    /**
79
     * @return static
80
     */
81
    final public function labelConfig(array $config): self
82
    {
83
        $new = clone $this;
84
        $new->labelConfig = $config;
85
        return $new;
86
    }
87
88
    /**
89
     * @return static
90
     */
91
    final public function label(?string $content): self
92
    {
93
        $new = clone $this;
94
        $new->labelConfig['content()'] = [$content];
95
        return $new;
96
    }
97
98
    /**
99
     * @return static
100
     */
101
    final public function hintConfig(array $config): self
102
    {
103
        $new = clone $this;
104
        $new->hintConfig = $config;
105
        return $new;
106
    }
107
108
    final public function hint(?string $content): self
109
    {
110
        $new = clone $this;
111
        $new->hintConfig['content()'] = [$content];
112
        return $new;
113
    }
114
115
    final protected function getInputName(): string
116
    {
117
        return HtmlForm::getInputName($this->getFormModel(), $this->attribute);
118
    }
119
120
    final protected function getInputId(): ?string
121
    {
122
        if (!$this->setInputIdAttribute) {
123
            return null;
124
        }
125
126
        return $this->inputId ?? HtmlForm::getInputId($this->getFormModel(), $this->attribute);
127
    }
128
129
    final protected function prepareIdInInputTag(Tag $tag): Tag
130
    {
131
        if (
132
            $this->setInputIdAttribute
133
            && $tag->getAttribute('id') === null
0 ignored issues
show
Bug introduced by
The method getAttribute() does not exist on Yiisoft\Html\Tag\Base\Tag. ( Ignorable by Annotation )

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

133
            && $tag->/** @scrutinizer ignore-call */ getAttribute('id') === null

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
        ) {
135
            $id = $this->getInputId();
136
            if ($id !== null) {
137
                $tag = $tag->id($id);
138
            }
139
        }
140
141
        return $tag;
142
    }
143
144
    final protected function run(): string
145
    {
146
        if ($this->withoutContainer) {
147
            return $this->generateContent();
148
        }
149
150
        $containerTag = $this->containerTag ?? Div::tag();
151
152
        return $containerTag->open()
153
            . PHP_EOL
154
            . $this->generateContent()
155
            . PHP_EOL
156
            . $containerTag->close();
157
    }
158
159
    abstract protected function generateInput(): string;
160
161
    private function generateContent(): string
162
    {
163
        $parts = [
164
            '{label}' => $this->generateLabel(),
165
            '{input}' => $this->generateInput(),
166
            '{hint}' => $this->generateHint(),
167
            '{error}' => $this->generateError(),
168
        ];
169
170
        return preg_replace('/^\h*\v+/m', '', trim(strtr($this->template, $parts)));
171
    }
172
173
    private function generateLabel(): string
174
    {
175
        $config = $this->labelConfig;
176
177
        if (
178
            $this->setInputIdAttribute === false
179
            && ($config['useInputIdAttribute()'] ?? [null]) === [null]
180
        ) {
181
            $config['useInputIdAttribute()'] = [false];
182
        }
183
184
        return Label::widget($config)
185
            ->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\Base\AbstractField or Yiisoft\Form\Field\Part\Hint. ( Ignorable by Annotation )

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

185
            ->/** @scrutinizer ignore-call */ attribute($this->getFormModel(), $this->attribute)
Loading history...
186
            ->render();
187
    }
188
189
    private function generateHint(): string
190
    {
191
        return Hint::widget($this->hintConfig)
192
            ->attribute($this->getFormModel(), $this->attribute)
193
            ->render();
194
    }
195
196
    private function generateError(): string
197
    {
198
        return '';
199
    }
200
}
201