Passed
Push — master ( e6861f...bdfe92 )
by Sergei
03:23
created

Theme::__construct()   B

Complexity

Conditions 7
Paths 27

Size

Total Lines 36
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 6
nc 27
nop 26
dl 0
loc 36
ccs 7
cts 7
cp 1
crap 7
rs 8.8333
c 1
b 0
f 0

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 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 44
    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 44
        if ($labelClass !== null) {
47 2
            $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 44
        if ($hintClass !== null) {
50 2
            $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 44
        if ($errorClass !== null) {
53 2
            $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 30
    public function getLabelConfig(): array
58
    {
59 30
        return $this->labelConfig;
60
    }
61
62 29
    public function getHintConfig(): array
63
    {
64 29
        return $this->hintConfig;
65
    }
66
67 29
    public function getErrorConfig(): array
68
    {
69 29
        return $this->errorConfig;
70
    }
71
72
    /**
73
     * @psalm-param class-string $class
74
     */
75 28
    public function getFieldConfig(string $class): array
76
    {
77 28
        $config = [];
78
79 28
        if ($this->containerTag !== null) {
80 2
            $config['containerTag()'] = [$this->containerTag];
81
        }
82 28
        if ($this->containerAttributes !== []) {
83 6
            $config['containerAttributes()'] = [$this->containerAttributes];
84
        }
85 28
        if ($this->containerClass !== null) {
86 3
            $config['containerClass()'] = is_array($this->containerClass)
87 1
                ? $this->containerClass
88 2
                : [$this->containerClass];
89
        }
90 28
        if ($this->useContainer !== null) {
91 1
            $config['useContainer()'] = [$this->useContainer];
92
        }
93
94 28
        if (is_a($class, PartsField::class, true)) {
95 25
            if ($this->template !== null) {
96 1
                $config['template()'] = [$this->template];
97
            }
98 25
            if ($this->templateBegin !== null) {
99 1
                $config['templateBegin()'] = [$this->templateBegin];
100
            }
101 25
            if ($this->templateEnd !== null) {
102 1
                $config['templateEnd()'] = [$this->templateEnd];
103
            }
104 25
            if ($this->inputContainerTag !== null) {
105 4
                $config['inputContainerTag()'] = [$this->inputContainerTag];
106
            }
107 25
            if ($this->inputContainerAttributes !== []) {
108 1
                $config['inputContainerAttributes()'] = [$this->inputContainerAttributes];
109
            }
110 25
            if ($this->inputContainerClass !== null) {
111 3
                $config['inputContainerClass()'] = is_array($this->inputContainerClass)
112 2
                    ? $this->inputContainerClass
113 1
                    : [$this->inputContainerClass];
114
            }
115 25
            if ($this->labelConfig !== []) {
116 1
                $config['labelConfig()'] = [$this->labelConfig];
117
            }
118 25
            if ($this->hintConfig !== []) {
119 1
                $config['hintConfig()'] = [$this->hintConfig];
120
            }
121 25
            if ($this->errorConfig !== []) {
122 1
                $config['errorConfig()'] = [$this->errorConfig];
123
            }
124
        }
125
126 28
        if (is_a($class, InputField::class, true)) {
127 23
            if ($this->setInputId !== null) {
128 1
                $config['setInputId()'] = [$this->setInputId];
129 1
                if ($this->setInputId === false) {
130 1
                    $config['labelConfig()'] = [
131 1
                        $this->labelConfig + ['useInputId()' => [false]],
132 1
                    ];
133
                }
134
            }
135 23
            if ($this->inputAttributes !== []) {
136 2
                $config['inputAttributes()'] = [$this->inputAttributes];
137
            }
138 23
            if ($this->inputClass !== null) {
139 2
                $config['inputClass()'] = is_array($this->inputClass)
140 1
                    ? $this->inputClass
141 1
                    : [$this->inputClass];
142
            }
143
        }
144
145 28
        if (is_a($class, PlaceholderInterface::class, true)) {
146 22
            if ($this->usePlaceholder !== null) {
147 2
                $config['usePlaceholder()'] = [$this->usePlaceholder];
148
            }
149
        }
150
151 28
        if (is_a($class, EnrichmentFromRulesInterface::class, true)) {
152 22
            if ($this->enrichmentFromRules !== null) {
153 1
                $config['enrichmentFromRules()'] = [$this->enrichmentFromRules];
154
            }
155
        }
156
157 28
        if (is_a($class, ValidationClassInterface::class, true)) {
158 22
            if ($this->validClass !== null) {
159 1
                $config['validClass()'] = [$this->validClass];
160
            }
161 22
            if ($this->invalidClass !== null) {
162 1
                $config['invalidClass()'] = [$this->invalidClass];
163
            }
164 22
            if ($this->inputValidClass !== null) {
165 1
                $config['inputValidClass()'] = [$this->inputValidClass];
166
            }
167 22
            if ($this->inputInvalidClass !== null) {
168 1
                $config['inputInvalidClass()'] = [$this->inputInvalidClass];
169
            }
170
        }
171
172 28
        if (!empty($this->fieldConfigs[$class])) {
173 4
            $config = array_merge($config, $this->fieldConfigs[$class]);
174
        }
175
176 28
        return $config;
177
    }
178
}
179