Passed
Pull Request — master (#159)
by Sergei
02:40
created

FieldFactory::makeBaseFieldConfig()   F

Complexity

Conditions 11
Paths 513

Size

Total Lines 43
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 11

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 513
nop 0
dl 0
loc 43
ccs 23
cts 23
cp 1
crap 11
rs 3.8263
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form;
6
7
use Yiisoft\Form\Field\InputText;
8
use Yiisoft\Form\Field\Part\Error;
9
use Yiisoft\Form\Field\Part\Hint;
10
use Yiisoft\Form\Field\Part\Label;
11
12
use function in_array;
13
14
final class FieldFactory
15
{
16
    private const PLACEHOLDER = 1;
17
18
    private const TAG_ATTRIBUTES_PROPERTIES = [
19
        'containerTagAttributes()',
20
        'formElementTagAttributes()',
21
    ];
22
23
    //
24
    // Common
25
    //
26
27
    private ?string $containerTag;
28
    private array $containerTagAttributes;
29
    private ?bool $useContainer;
30
31
    private ?string $template;
32
33
    private ?bool $setInputIdAttribute;
34
35
    private array $formElementTagAttributes;
36
37
    private array $labelConfig;
38
    private array $hintConfig;
39
    private array $errorConfig;
40
41
    //
42
    // Placeholder
43
    //
44
45
    private ?bool $usePlaceholder;
46
47
    //
48
    // Field configurations
49
    //
50
51
    private array $inputTextConfig;
52
53
    //
54
    // Internal properties
55
    //
56
57
    private ?array $baseFieldConfig = null;
58
59 20
    public function __construct(FieldFactoryConfig $config)
60
    {
61 20
        $this->containerTag = $config->getContainerTag();
62 20
        $this->containerTagAttributes = $config->getContainerTagAttributes();
63 20
        $this->useContainer = $config->getUseContainer();
64
65 20
        $this->template = $config->getTemplate();
66
67 20
        $this->setInputIdAttribute = $config->getSetInputIdAttribute();
68
69 20
        $this->formElementTagAttributes = $config->getFormElementTagAttributes();
70
71 20
        $this->labelConfig = $config->getLabelConfig();
72 20
        $this->hintConfig = $config->getHintConfig();
73 20
        $this->errorConfig = $config->getErrorConfig();
74
75 20
        $this->usePlaceholder = $config->getUsePlaceholder();
76
77 20
        $this->inputTextConfig = $config->getInputTextConfig();
78 20
    }
79
80 4
    public function label(FormModelInterface $formModel, string $attribute): Label
81
    {
82 4
        return Label::widget($this->makeLabelConfig())->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\Base\AbstractField or Yiisoft\Form\Field\Part\Error 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

82
        return Label::widget($this->makeLabelConfig())->/** @scrutinizer ignore-call */ attribute($formModel, $attribute);
Loading history...
83
    }
84
85 3
    public function hint(FormModelInterface $formModel, string $attribute): Hint
86
    {
87 3
        return Hint::widget($this->hintConfig)->attribute($formModel, $attribute);
88
    }
89
90 3
    public function error(FormModelInterface $formModel, string $attribute): Error
91
    {
92 3
        return Error::widget($this->errorConfig)->attribute($formModel, $attribute);
93
    }
94
95 10
    public function inputText(FormModelInterface $formModel, string $attribute): InputText
96
    {
97 10
        $config = $this->mergeFieldConfigs(
98 10
            $this->makeFieldConfig(self::PLACEHOLDER),
99 10
            $this->inputTextConfig
100
        );
101
102 10
        return InputText::widget($config)->attribute($formModel, $attribute);
103
    }
104
105 10
    private function mergeFieldConfigs(array $a, array $b): array
106
    {
107 10
        $c = [];
108
109 10
        foreach ($a as $key => $value) {
110
            if (
111 8
                in_array($key, self::TAG_ATTRIBUTES_PROPERTIES, true)
112 8
                && isset($b[$key])
113
            ) {
114 1
                $c[$key] = [array_merge($value[0], $b[$key][0])];
115
            }
116
        }
117
118 10
        return array_merge($a, $b, $c);
119
    }
120
121 10
    private function makeFieldConfig(int ...$supports): array
122
    {
123 10
        $config = $this->makeBaseFieldConfig();
124 10
        foreach ($supports as $support) {
125
            switch ($support) {
126 10
                case self::PLACEHOLDER:
127 10
                    if ($this->usePlaceholder !== null) {
128 2
                        $config['usePlaceholder()'] = [$this->usePlaceholder];
129
                    }
130 10
                    break;
131
            }
132
        }
133 10
        return $config;
134
    }
135
136 10
    private function makeBaseFieldConfig(): array
137
    {
138 10
        if ($this->baseFieldConfig === null) {
139 10
            $this->baseFieldConfig = [];
140
141 10
            if ($this->containerTag !== null) {
142 2
                $this->baseFieldConfig['containerTag()'] = [$this->containerTag];
143
            }
144
145 10
            if ($this->containerTagAttributes !== []) {
146 2
                $this->baseFieldConfig['containerTagAttributes()'] = [$this->containerTagAttributes];
147
            }
148
149 10
            if ($this->useContainer !== null) {
150 1
                $this->baseFieldConfig['useContainer()'] = [$this->useContainer];
151
            }
152
153 10
            if ($this->template !== null) {
154 1
                $this->baseFieldConfig['template()'] = [$this->template];
155
            }
156
157 10
            if ($this->setInputIdAttribute !== null) {
158 1
                $this->baseFieldConfig['setInputIdAttribute()'] = [$this->setInputIdAttribute];
159
            }
160
161 10
            if ($this->formElementTagAttributes !== []) {
162 2
                $this->baseFieldConfig['formElementTagAttributes()'] = [$this->formElementTagAttributes];
163
            }
164
165 10
            $labelConfig = $this->makeLabelConfig();
166 10
            if ($labelConfig !== []) {
167 2
                $this->baseFieldConfig['labelConfig()'] = [$labelConfig];
168
            }
169
170 10
            if ($this->hintConfig !== []) {
171 1
                $this->baseFieldConfig['hintConfig()'] = [$this->hintConfig];
172
            }
173
174 10
            if ($this->errorConfig !== []) {
175 1
                $this->baseFieldConfig['errorConfig()'] = [$this->errorConfig];
176
            }
177
        }
178 10
        return $this->baseFieldConfig;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->baseFieldConfig could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
179
    }
180
181 14
    private function makeLabelConfig(): array
182
    {
183 14
        $config = [];
184
185 14
        if ($this->setInputIdAttribute === false) {
186 2
            $config['useInputIdAttribute()'] = [false];
187
        }
188
189 14
        return array_merge($config, $this->labelConfig);
190
    }
191
}
192