Test Failed
Pull Request — master (#244)
by Sergei
03:29
created

Theme   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 72
c 1
b 0
f 0
dl 0
loc 164
rs 8.96
wmc 43

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getHintConfig() 0 3 1
A getErrorConfig() 0 3 1
F getFieldConfig() 0 102 33
A getLabelConfig() 0 3 1
B __construct() 0 36 7

How to fix   Complexity   

Complex Class

Complex classes like Theme often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Theme, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form;
6
7
use Yiisoft\Form\Field\Base\EnrichmentFromRules\EnrichmentFromRulesInterface;
8
use Yiisoft\Form\Field\Base\InputField;
9
use Yiisoft\Form\Field\Base\PartsField;
10
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderInterface;
11
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
12
13
final class Theme
14
{
15
    /**
16
     * @param array[] $fieldConfigs
17
     */
18
    public function __construct(
19
        private ?string $containerTag = null,
20
        private array $containerAttributes = [],
21
        private string|array|null $containerClass = null,
22
        private ?bool $useContainer = null,
23
        private ?string $template = null,
24
        private ?string $templateBegin = null,
25
        private ?string $templateEnd = null,
26
        private ?bool $setInputId = null,
27
        private array $inputAttributes = [],
28
        private string|array|null $inputClass = null,
29
        private ?string $inputContainerTag = null,
30
        private array $inputContainerAttributes = [],
31
        private string|array|null $inputContainerClass = null,
32
        string|array|null $labelClass = null,
33
        private array $labelConfig = [],
34
        string|array|null $hintClass = null,
35
        private array $hintConfig = [],
36
        string|array|null $errorClass = null,
37
        private array $errorConfig = [],
38
        private ?bool $usePlaceholder = null,
39
        private ?string $validClass = null,
40
        private ?string $invalidClass = null,
41
        private ?string $inputValidClass = null,
42
        private ?string $inputInvalidClass = null,
43
        private ?bool $enrichmentFromRules = null,
44
        private array $fieldConfigs = [],
45
    ) {
46
        if ($labelClass !== null) {
47
            $this->labelConfig['class()'] = is_array($labelClass) ? $labelClass : [$labelClass];
0 ignored issues
show
introduced by
The condition is_array($labelClass) is always true.
Loading history...
48
        }
49
        if ($hintClass !== null) {
50
            $this->hintConfig['class()'] = is_array($hintClass) ? $hintClass : [$hintClass];
0 ignored issues
show
introduced by
The condition is_array($hintClass) is always true.
Loading history...
51
        }
52
        if ($errorClass !== null) {
53
            $this->errorConfig['class()'] = is_array($errorClass) ? $errorClass : [$errorClass];
0 ignored issues
show
introduced by
The condition is_array($errorClass) is always true.
Loading history...
54
        }
55
    }
56
57
    public function getLabelConfig(): array
58
    {
59
        return $this->labelConfig;
60
    }
61
62
    public function getHintConfig(): array
63
    {
64
        return $this->hintConfig;
65
    }
66
67
    public function getErrorConfig(): array
68
    {
69
        return $this->errorConfig;
70
    }
71
72
    /**
73
     * @psalm-param class-string $class
74
     */
75
    public function getFieldConfig(string $class): array
76
    {
77
        $config = [];
78
79
        if ($this->containerTag !== null) {
80
            $config['containerTag()'] = [$this->containerTag];
81
        }
82
        if ($this->containerAttributes !== []) {
83
            $config['containerAttributes()'] = [$this->containerAttributes];
84
        }
85
        if ($this->containerClass !== null) {
86
            $config['containerClass()'] = is_array($this->containerClass)
87
                ? $this->containerClass
88
                : [$this->containerClass];
89
        }
90
        if ($this->useContainer !== null) {
91
            $config['useContainer()'] = [$this->useContainer];
92
        }
93
94
        if (is_a($class, PartsField::class, true)) {
95
            if ($this->template !== null) {
96
                $config['template()'] = [$this->template];
97
            }
98
            if ($this->templateBegin !== null) {
99
                $config['templateBegin()'] = [$this->templateBegin];
100
            }
101
            if ($this->templateEnd !== null) {
102
                $config['templateEnd()'] = [$this->templateEnd];
103
            }
104
            if ($this->inputContainerTag !== null) {
105
                $config['inputContainerTag()'] = [$this->inputContainerTag];
106
            }
107
            if ($this->inputContainerAttributes !== []) {
108
                $config['inputContainerAttributes()'] = [$this->inputContainerAttributes];
109
            }
110
            if ($this->inputContainerClass !== null) {
111
                $config['inputContainerClass()'] = is_array($this->inputContainerClass)
112
                    ? $this->inputContainerClass
113
                    : [$this->inputContainerClass];
114
            }
115
            if ($this->labelConfig !== []) {
116
                $config['labelConfig()'] = [$this->labelConfig];
117
            }
118
            if ($this->hintConfig !== []) {
119
                $config['hintConfig()'] = [$this->hintConfig];
120
            }
121
            if ($this->errorConfig !== []) {
122
                $config['errorConfig()'] = [$this->errorConfig];
123
            }
124
        }
125
126
        if (is_a($class, InputField::class, true)) {
127
            if ($this->setInputId !== null) {
128
                $config['setInputId()'] = [$this->setInputId];
129
                if ($this->setInputId === false) {
130
                    $config['labelConfig()'] = [
131
                        $this->labelConfig + ['useInputId()' => [false]],
132
                    ];
133
                }
134
            }
135
            if ($this->inputAttributes !== []) {
136
                $config['inputAttributes()'] = [$this->inputAttributes];
137
            }
138
            if ($this->inputClass !== null) {
139
                $config['inputClass()'] = is_array($this->inputClass)
140
                    ? $this->inputClass
141
                    : [$this->inputClass];
142
            }
143
        }
144
145
        if (is_a($class, PlaceholderInterface::class, true)) {
146
            if ($this->usePlaceholder !== null) {
147
                $config['usePlaceholder()'] = [$this->usePlaceholder];
148
            }
149
        }
150
151
        if (is_a($class, EnrichmentFromRulesInterface::class, true)) {
152
            if ($this->enrichmentFromRules !== null) {
153
                $config['enrichmentFromRules()'] = [$this->enrichmentFromRules];
154
            }
155
        }
156
157
        if (is_a($class, ValidationClassInterface::class, true)) {
158
            if ($this->validClass !== null) {
159
                $config['validClass()'] = [$this->validClass];
160
            }
161
            if ($this->invalidClass !== null) {
162
                $config['invalidClass()'] = [$this->invalidClass];
163
            }
164
            if ($this->inputValidClass !== null) {
165
                $config['inputValidClass()'] = [$this->inputValidClass];
166
            }
167
            if ($this->inputInvalidClass !== null) {
168
                $config['inputInvalidClass()'] = [$this->inputInvalidClass];
169
            }
170
        }
171
172
        if (!empty($this->fieldConfigs[$class])) {
173
            $config = array_merge($config, $this->fieldConfigs[$class]);
174
        }
175
176
        return $config;
177
    }
178
}
179