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