Passed
Pull Request — master (#192)
by Alexander
05:47 queued 02:55
created

FieldFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 0
c 2
b 1
f 0
nc 1
nop 16
dl 0
loc 18
ccs 1
cts 1
cp 1
crap 1
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\Field\Base\EnrichmentFromRules\EnrichmentFromRulesInterface;
9
use Yiisoft\Form\Field\Base\InputField;
10
use Yiisoft\Form\Field\Base\PartsField;
11
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderInterface;
12
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
13
use Yiisoft\Form\Field\Button;
14
use Yiisoft\Form\Field\ButtonGroup;
15
use Yiisoft\Form\Field\Checkbox;
16
use Yiisoft\Form\Field\CheckboxList;
17
use Yiisoft\Form\Field\Date;
18
use Yiisoft\Form\Field\DateTime;
19
use Yiisoft\Form\Field\DateTimeLocal;
20
use Yiisoft\Form\Field\Email;
21
use Yiisoft\Form\Field\ErrorSummary;
22
use Yiisoft\Form\Field\Fieldset;
23
use Yiisoft\Form\Field\Hidden;
24
use Yiisoft\Form\Field\Image;
25
use Yiisoft\Form\Field\Number;
26
use Yiisoft\Form\Field\Part\Error;
27
use Yiisoft\Form\Field\Part\Hint;
28
use Yiisoft\Form\Field\Part\Label;
29
use Yiisoft\Form\Field\Password;
30
use Yiisoft\Form\Field\RadioList;
31
use Yiisoft\Form\Field\Range;
32
use Yiisoft\Form\Field\ResetButton;
33
use Yiisoft\Form\Field\Select;
34
use Yiisoft\Form\Field\SubmitButton;
35
use Yiisoft\Form\Field\Telephone;
36
use Yiisoft\Form\Field\Text;
37
use Yiisoft\Form\Field\Textarea;
38
use Yiisoft\Form\Field\Url;
39
use Yiisoft\Widget\WidgetFactory;
40
41
final class FieldFactory
42
{
43
    /**
44
     * @param array[] $fieldConfigs
45
     */
46 21
    public function __construct(
47
        private ?string $containerTag = null,
48
        private array $containerTagAttributes = [],
49
        private ?bool $useContainer = null,
50
        private ?string $template = null,
51
        private ?string $templateBegin = null,
52
        private ?string $templateEnd = null,
53
        private ?bool $setInputIdAttribute = null,
54
        private array $inputTagAttributes = [],
55
        private array $labelConfig = [],
56
        private array $hintConfig = [],
57
        private array $errorConfig = [],
58
        private ?bool $usePlaceholder = null,
59
        private ?string $validClass = null,
60
        private ?string $invalidClass = null,
61
        private ?bool $enrichmentFromRules = null,
62
        private array $fieldConfigs = [],
63
    ) {
64
    }
65
66 1
    public function button(array $config = []): Button
67
    {
68 1
        return $this->field(Button::class, $config);
69
    }
70
71 1
    public function buttonGroup(array $config = []): ButtonGroup
72
    {
73 1
        return $this->field(ButtonGroup::class, $config);
74
    }
75
76 1
    public function checkbox(FormModelInterface $formModel, string $attribute, array $config = []): Checkbox
77
    {
78 1
        return $this->input(Checkbox::class, $formModel, $attribute, $config);
79
    }
80
81 1
    public function checkboxList(FormModelInterface $formModel, string $attribute, array $config = []): CheckboxList
82
    {
83
        return $this
84 1
            ->field(CheckboxList::class, $config)
85 1
            ->attribute($formModel, $attribute);
86
    }
87
88 1
    public function date(FormModelInterface $formModel, string $attribute, array $config = []): Date
89
    {
90 1
        return $this->input(Date::class, $formModel, $attribute, $config);
91
    }
92
93 1
    public function dateTime(FormModelInterface $formModel, string $attribute, array $config = []): DateTime
94
    {
95 1
        return $this->input(DateTime::class, $formModel, $attribute, $config);
96
    }
97
98 1
    public function dateTimeLocal(FormModelInterface $formModel, string $attribute, array $config = []): DateTimeLocal
99
    {
100 1
        return $this->input(DateTimeLocal::class, $formModel, $attribute, $config);
101
    }
102
103 1
    public function email(FormModelInterface $formModel, string $attribute, array $config = []): Email
104
    {
105 1
        return $this->input(Email::class, $formModel, $attribute, $config);
106
    }
107
108 3
    public function errorSummary(FormModelInterface $formModel, array $config = []): ErrorSummary
109
    {
110
        return $this
111 3
            ->field(ErrorSummary::class, $config)
112 3
            ->formModel($formModel);
113
    }
114
115 1
    public function fieldset(array $config = []): Fieldset
116
    {
117 1
        return $this->field(Fieldset::class, $config);
118
    }
119
120 1
    public function hidden(FormModelInterface $formModel, string $attribute, array $config = []): Hidden
121
    {
122 1
        return $this->input(Hidden::class, $formModel, $attribute, $config);
123
    }
124
125 1
    public function image(array $config = []): Image
126
    {
127 1
        return $this->field(Image::class, $config);
128
    }
129
130 1
    public function number(FormModelInterface $formModel, string $attribute, array $config = []): Number
131
    {
132 1
        return $this->input(Number::class, $formModel, $attribute, $config);
133
    }
134
135 1
    public function password(FormModelInterface $formModel, string $attribute, array $config = []): Password
136
    {
137 1
        return $this->input(Password::class, $formModel, $attribute, $config);
138
    }
139
140 1
    public function radioList(FormModelInterface $formModel, string $attribute, array $config = []): RadioList
141
    {
142
        return $this
143 1
            ->field(RadioList::class, $config)
144 1
            ->attribute($formModel, $attribute);
145
    }
146
147 1
    public function range(FormModelInterface $formModel, string $attribute, array $config = []): Range
148
    {
149 1
        return $this->input(Range::class, $formModel, $attribute, $config);
150
    }
151
152 1
    public function resetButton(array $config = []): ResetButton
153
    {
154 1
        return $this->field(ResetButton::class, $config);
155
    }
156
157 1
    public function select(FormModelInterface $formModel, string $attribute, array $config = []): Select
158
    {
159 1
        return $this->input(Select::class, $formModel, $attribute, $config);
160
    }
161
162 1
    public function submitButton(array $config = []): SubmitButton
163
    {
164 1
        return $this->field(SubmitButton::class, $config);
165
    }
166
167 1
    public function telephone(FormModelInterface $formModel, string $attribute, array $config = []): Telephone
168
    {
169 1
        return $this->input(Telephone::class, $formModel, $attribute, $config);
170
    }
171
172 14
    public function text(FormModelInterface $formModel, string $attribute, array $config = []): Text
173
    {
174 14
        return $this->input(Text::class, $formModel, $attribute, $config);
175
    }
176
177 1
    public function textarea(FormModelInterface $formModel, string $attribute, array $config = []): Textarea
178
    {
179 1
        return $this->input(Textarea::class, $formModel, $attribute, $config);
180
    }
181
182 1
    public function url(FormModelInterface $formModel, string $attribute, array $config = []): Url
183
    {
184 1
        return $this->input(Url::class, $formModel, $attribute, $config);
185
    }
186
187 4
    public function label(FormModelInterface $formModel, string $attribute, array $config = []): Label
188
    {
189 4
        $widgetConfig = array_merge(
190 4
            $this->labelConfig,
191
            $config,
192
        );
193 4
        return Label::widget($widgetConfig)->attribute($formModel, $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

193
        return Label::widget($widgetConfig)->/** @scrutinizer ignore-call */ attribute($formModel, $attribute);
Loading history...
194
    }
195
196 3
    public function hint(FormModelInterface $formModel, string $attribute, array $config = []): Hint
197
    {
198 3
        $widgetConfig = array_merge(
199 3
            $this->hintConfig,
200
            $config,
201
        );
202 3
        return Hint::widget($widgetConfig)->attribute($formModel, $attribute);
203
    }
204
205 3
    public function error(FormModelInterface $formModel, string $attribute, array $config = []): Error
206
    {
207 3
        $widgetConfig = array_merge(
208 3
            $this->errorConfig,
209
            $config,
210
        );
211 3
        return Error::widget($widgetConfig)->attribute($formModel, $attribute);
212
    }
213
214
    /**
215
     * @psalm-template T
216
     * @psalm-param class-string<T> $class
217
     * @psalm-return T
218
     */
219 27
    public function input(string $class, FormModelInterface $formModel, string $attribute, array $config = []): object
220
    {
221 27
        $widget = $this->field($class, $config);
222 27
        if (!$widget instanceof InputField) {
223
            throw new InvalidArgumentException(
224
                sprintf(
225
                    'Input widget must be instance of "%s".',
226
                    InputField::class
227
                )
228
            );
229
        }
230
231 27
        return $widget->attribute($formModel, $attribute);
232
    }
233
234
    /**
235
     * @psalm-template T
236
     * @psalm-param class-string<T> $class
237
     * @psalm-return T
238
     */
239 38
    public function field(string $class, array $config = []): object
240
    {
241 38
        $config = array_merge(
242 38
            $this->makeFieldConfig($class),
243 38
            $this->fieldConfigs[$class] ?? [],
244
            $config,
245 38
            ['class' => $class],
246
        );
247
248
        /** @psalm-var T */
249 38
        return WidgetFactory::createWidget($config);
250
    }
251
252
    /**
253
     * @psalm-param class-string $class
254
     */
255 38
    private function makeFieldConfig(string $class): array
256
    {
257 38
        $config = [];
258
259 38
        if ($this->containerTag !== null) {
260 2
            $config['containerTag()'] = [$this->containerTag];
261
        }
262 38
        if ($this->containerTagAttributes !== []) {
263 4
            $config['containerTagAttributes()'] = [$this->containerTagAttributes];
264
        }
265 38
        if ($this->useContainer !== null) {
266 1
            $config['useContainer()'] = [$this->useContainer];
267
        }
268
269 38
        if (is_a($class, PartsField::class, true)) {
270 35
            if ($this->template !== null) {
271 1
                $config['template()'] = [$this->template];
272
            }
273 35
            if ($this->templateBegin !== null) {
274
                $config['templateBegin()'] = [$this->templateBegin];
275
            }
276 35
            if ($this->templateEnd !== null) {
277
                $config['templateEnd()'] = [$this->templateEnd];
278
            }
279 35
            if ($this->labelConfig !== []) {
280 1
                $config['labelConfig()'] = [$this->labelConfig];
281
            }
282 35
            if ($this->hintConfig !== []) {
283 1
                $config['hintConfig()'] = [$this->hintConfig];
284
            }
285 35
            if ($this->errorConfig !== []) {
286 1
                $config['errorConfig()'] = [$this->errorConfig];
287
            }
288
        }
289
290 38
        if (is_a($class, InputField::class, true)) {
291 27
            if ($this->setInputIdAttribute !== null) {
292 1
                $config['setInputIdAttribute()'] = [$this->setInputIdAttribute];
293 1
                if ($this->setInputIdAttribute === false) {
294 1
                    $config['labelConfig()'] = [
295 1
                        $this->labelConfig + ['useInputIdAttribute()' => [false]],
296
                    ];
297
                }
298
            }
299 27
            if ($this->inputTagAttributes !== []) {
300 2
                $config['inputTagAttributes()'] = [$this->inputTagAttributes];
301
            }
302
        }
303
304 38
        if (is_a($class, PlaceholderInterface::class, true)) {
305 20
            if ($this->usePlaceholder !== null) {
306 2
                $config['usePlaceholder()'] = [$this->usePlaceholder];
307
            }
308
        }
309
310 38
        if (is_a($class, EnrichmentFromRulesInterface::class, true)) {
311 25
            if ($this->enrichmentFromRules !== null) {
312
                $config['enrichmentFromRules()'] = [$this->enrichmentFromRules];
313
            }
314
        }
315
316 38
        if (is_a($class, ValidationClassInterface::class, true)) {
317 28
            if ($this->validClass !== null) {
318 1
                $config['validClass()'] = [$this->validClass];
319
            }
320 28
            if ($this->invalidClass !== null) {
321 1
                $config['invalidClass()'] = [$this->invalidClass];
322
            }
323
        }
324
325 38
        return $config;
326
    }
327
}
328