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

80
        return Label::widget($widgetConfig)->/** @scrutinizer ignore-call */ attribute($formModel, $attribute);
Loading history...
81
    }
82
83 4
    public function hint(FormModelInterface $formModel, string $attribute, array $config = []): Hint
84
    {
85 4
        $widgetConfig = array_merge(
86 4
            $this->hintConfig,
87
            $config,
88
        );
89 4
        return Hint::widget($widgetConfig)->attribute($formModel, $attribute);
90
    }
91
92 4
    public function error(FormModelInterface $formModel, string $attribute, array $config = []): Error
93
    {
94 4
        $widgetConfig = array_merge(
95 4
            $this->errorConfig,
96
            $config,
97
        );
98 4
        return Error::widget($widgetConfig)->attribute($formModel, $attribute);
99
    }
100
101 11
    public function widget(
102
        string $class,
103
        FormModelInterface $formModel,
104
        string $attribute,
105
        array $config = []
106
    ): object {
107 11
        $supports = [];
108 11
        if (is_subclass_of($class, PlaceholderInterface::class)) {
109 11
            $supports[] = self::PLACEHOLDER;
110
        }
111
112 11
        $config = array_merge(
113 11
            $this->makeFieldConfig($supports),
114 11
            $this->fieldConfigs[$class] ?? [],
115
            $config,
116
        );
117 11
        return InputText::widget($config)->attribute($formModel, $attribute);
118
    }
119
120
    /**
121
     * @param int[] $supports
122
     */
123 11
    private function makeFieldConfig(array $supports): array
124
    {
125 11
        $config = $this->makeBaseFieldConfig();
126 11
        foreach ($supports as $support) {
127
            switch ($support) {
128 11
                case self::PLACEHOLDER:
129 11
                    if ($this->usePlaceholder !== null) {
130 2
                        $config['usePlaceholder()'] = [$this->usePlaceholder];
131
                    }
132 11
                    break;
133
            }
134
        }
135 11
        return $config;
136
    }
137
138 11
    private function makeBaseFieldConfig(): array
139
    {
140 11
        if ($this->baseFieldConfig === null) {
141 11
            $this->baseFieldConfig = [];
142
143 11
            if ($this->containerTag !== null) {
144 2
                $this->baseFieldConfig['containerTag()'] = [$this->containerTag];
145
            }
146
147 11
            if ($this->containerTagAttributes !== []) {
148 2
                $this->baseFieldConfig['containerTagAttributes()'] = [$this->containerTagAttributes];
149
            }
150
151 11
            if ($this->useContainer !== null) {
152 1
                $this->baseFieldConfig['useContainer()'] = [$this->useContainer];
153
            }
154
155 11
            if ($this->template !== null) {
156 1
                $this->baseFieldConfig['template()'] = [$this->template];
157
            }
158
159 11
            if ($this->setInputIdAttribute !== null) {
160 1
                $this->baseFieldConfig['setInputIdAttribute()'] = [$this->setInputIdAttribute];
161
            }
162
163 11
            if ($this->formElementTagAttributes !== []) {
164 2
                $this->baseFieldConfig['formElementTagAttributes()'] = [$this->formElementTagAttributes];
165
            }
166
167 11
            $labelConfig = $this->makeLabelConfig();
168 11
            if ($labelConfig !== []) {
169 2
                $this->baseFieldConfig['labelConfig()'] = [$labelConfig];
170
            }
171
172 11
            if ($this->hintConfig !== []) {
173 1
                $this->baseFieldConfig['hintConfig()'] = [$this->hintConfig];
174
            }
175
176 11
            if ($this->errorConfig !== []) {
177 1
                $this->baseFieldConfig['errorConfig()'] = [$this->errorConfig];
178
            }
179
        }
180 11
        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...
181
    }
182
183 16
    private function makeLabelConfig(): array
184
    {
185 16
        $config = [];
186
187 16
        if ($this->setInputIdAttribute === false) {
188 2
            $config['useInputIdAttribute()'] = [false];
189
        }
190
191 16
        return array_merge($config, $this->labelConfig);
192
    }
193
}
194