Passed
Push — master ( bc2de9...c15889 )
by Sergei
04:06 queued 01:06
created

YiiValidatorRulesEnricher   F

Complexity

Total Complexity 95

Size/Duplication

Total Lines 280
Duplicated Lines 0 %

Test Coverage

Coverage 98.13%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 155
c 1
b 0
f 0
dl 0
loc 280
ccs 157
cts 160
cp 0.9813
rs 2
wmc 95

1 Method

Rating   Name   Duplication   Size   Complexity  
F process() 0 278 95

How to fix   Complexity   

Complex Class

Complex classes like YiiValidatorRulesEnricher 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 YiiValidatorRulesEnricher, 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\BaseField;
8
use Yiisoft\Form\Field\Base\DateTimeInputField;
9
use Yiisoft\Form\Field\Email;
10
use Yiisoft\Form\Field\File;
11
use Yiisoft\Form\Field\Number;
12
use Yiisoft\Form\Field\Password;
13
use Yiisoft\Form\Field\Range;
14
use Yiisoft\Form\Field\Select;
15
use Yiisoft\Form\Field\Telephone;
16
use Yiisoft\Form\Field\Text;
17
use Yiisoft\Form\Field\Textarea;
18
use Yiisoft\Form\Field\Url;
19
use Yiisoft\Html\Html;
20
use Yiisoft\Validator\Rule\Length;
21
use Yiisoft\Validator\Rule\Number as NumberRule;
22
use Yiisoft\Validator\Rule\Regex;
23
use Yiisoft\Validator\Rule\Required;
24
use Yiisoft\Validator\Rule\Url as UrlRule;
25
use Yiisoft\Validator\WhenInterface;
26
27
use function is_iterable;
28
29
final class YiiValidatorRulesEnricher implements ValidationRulesEnricherInterface
30
{
31 45
    public function process(BaseField $field, mixed $rules): ?array
32
    {
33 45
        if (!is_iterable($rules)) {
34
            return null;
35
        }
36
37 45
        if ($field instanceof DateTimeInputField) {
38 2
            $enrichment = [];
39 2
            foreach ($rules as $rule) {
40 2
                if ($rule instanceof WhenInterface && $rule->getWhen() !== null) {
41 1
                    continue;
42
                }
43
44 1
                if ($rule instanceof Required) {
45 1
                    $enrichment['inputAttributes']['required'] = true;
46
                }
47
            }
48 2
            return $enrichment;
49
        }
50
51 43
        if ($field instanceof Email) {
52 5
            $enrichment = [];
53 5
            foreach ($rules as $rule) {
54 5
                if ($rule instanceof WhenInterface && $rule->getWhen() !== null) {
55 1
                    continue;
56
                }
57
58 4
                if ($rule instanceof Required) {
59 1
                    $enrichment['inputAttributes']['required'] = true;
60
                }
61
62 4
                if ($rule instanceof Length) {
63 1
                    if (null !== $min = $rule->getMin()) {
64 1
                        $enrichment['inputAttributes']['minlength'] = $min;
65
                    }
66 1
                    if (null !== $max = $rule->getMax()) {
67 1
                        $enrichment['inputAttributes']['maxlength'] = $max;
68
                    }
69
                }
70
71 4
                if ($rule instanceof Regex) {
72 2
                    if (!$rule->isNot()) {
73 1
                        $enrichment['inputAttributes']['pattern'] = Html::normalizeRegexpPattern(
74 1
                            $rule->getPattern()
75 1
                        );
76
                    }
77
                }
78
            }
79 5
            return $enrichment;
80
        }
81
82 38
        if ($field instanceof File) {
83 2
            $enrichment = [];
84 2
            foreach ($rules as $rule) {
85 2
                if ($rule instanceof WhenInterface && $rule->getWhen() !== null) {
86 1
                    continue;
87
                }
88
89 1
                if ($rule instanceof Required) {
90 1
                    $enrichment['inputAttributes']['required'] = true;
91
                }
92
            }
93 2
            return $enrichment;
94
        }
95
96 36
        if ($field instanceof Number) {
97 3
            $enrichment = [];
98 3
            foreach ($rules as $rule) {
99 3
                if ($rule instanceof WhenInterface && $rule->getWhen() !== null) {
100 1
                    continue;
101
                }
102
103 2
                if ($rule instanceof Required) {
104 1
                    $enrichment['inputAttributes']['required'] = true;
105
                }
106
107 2
                if ($rule instanceof NumberRule) {
108 1
                    if (null !== $min = $rule->getMin()) {
109 1
                        $enrichment['inputAttributes']['min'] = $min;
110
                    }
111 1
                    if (null !== $max = $rule->getMax()) {
112 1
                        $enrichment['inputAttributes']['max'] = $max;
113
                    }
114
                }
115
            }
116 3
            return $enrichment;
117
        }
118
119 33
        if ($field instanceof Password) {
120 5
            $enrichment = [];
121 5
            foreach ($rules as $rule) {
122 5
                if ($rule instanceof WhenInterface && $rule->getWhen() !== null) {
123 1
                    continue;
124
                }
125
126 4
                if ($rule instanceof Required) {
127 1
                    $enrichment['inputAttributes']['required'] = true;
128
                }
129
130 4
                if ($rule instanceof Length) {
131 1
                    if (null !== $min = $rule->getMin()) {
132 1
                        $enrichment['inputAttributes']['minlength'] = $min;
133
                    }
134 1
                    if (null !== $max = $rule->getMax()) {
135 1
                        $enrichment['inputAttributes']['maxlength'] = $max;
136
                    }
137
                }
138
139 4
                if ($rule instanceof Regex) {
140 2
                    if (!$rule->isNot()) {
141 1
                        $enrichment['inputAttributes']['pattern'] = Html::normalizeRegexpPattern(
142 1
                            $rule->getPattern()
143 1
                        );
144
                    }
145
                }
146
            }
147 5
            return $enrichment;
148
        }
149
150 28
        if ($field instanceof Range) {
151 3
            $enrichment = [];
152 3
            foreach ($rules as $rule) {
153 3
                if ($rule instanceof WhenInterface && $rule->getWhen() !== null) {
154 1
                    continue;
155
                }
156
157 2
                if ($rule instanceof Required) {
158 1
                    $enrichment['inputAttributes']['required'] = true;
159
                }
160
161 2
                if ($rule instanceof NumberRule) {
162 1
                    if (null !== $min = $rule->getMin()) {
163 1
                        $enrichment['inputAttributes']['min'] = $min;
164
                    }
165 1
                    if (null !== $max = $rule->getMax()) {
166 1
                        $enrichment['inputAttributes']['max'] = $max;
167
                    }
168
                }
169
            }
170 3
            return $enrichment;
171
        }
172
173 25
        if ($field instanceof Select) {
174 2
            $enrichment = [];
175 2
            foreach ($rules as $rule) {
176 2
                if ($rule instanceof WhenInterface && $rule->getWhen() !== null) {
177 1
                    continue;
178
                }
179
180 1
                if ($rule instanceof Required) {
181 1
                    $enrichment['inputAttributes']['required'] = true;
182
                }
183
            }
184 2
            return $enrichment;
185
        }
186
187 23
        if ($field instanceof Telephone) {
188 5
            $enrichment = [];
189 5
            foreach ($rules as $rule) {
190 5
                if ($rule instanceof WhenInterface && $rule->getWhen() !== null) {
191 1
                    continue;
192
                }
193
194 4
                if ($rule instanceof Required) {
195 1
                    $enrichment['inputAttributes']['required'] = true;
196
                }
197
198 4
                if ($rule instanceof Length) {
199 1
                    if (null !== $min = $rule->getMin()) {
200 1
                        $enrichment['inputAttributes']['minlength'] = $min;
201
                    }
202 1
                    if (null !== $max = $rule->getMax()) {
203 1
                        $enrichment['inputAttributes']['maxlength'] = $max;
204
                    }
205
                }
206
207 4
                if ($rule instanceof Regex) {
208 2
                    if (!$rule->isNot()) {
209 1
                        $enrichment['inputAttributes']['pattern'] = Html::normalizeRegexpPattern(
210 1
                            $rule->getPattern()
211 1
                        );
212
                    }
213
                }
214
            }
215 5
            return $enrichment;
216
        }
217
218 18
        if ($field instanceof Text) {
219 6
            $enrichment = [];
220 6
            foreach ($rules as $rule) {
221 6
                if ($rule instanceof WhenInterface && $rule->getWhen() !== null) {
222 1
                    continue;
223
                }
224
225 5
                if ($rule instanceof Required) {
226 2
                    $enrichment['inputAttributes']['required'] = true;
227
                }
228
229 5
                if ($rule instanceof Length) {
230 1
                    if (null !== $min = $rule->getMin()) {
231 1
                        $enrichment['inputAttributes']['minlength'] = $min;
232
                    }
233 1
                    if (null !== $max = $rule->getMax()) {
234 1
                        $enrichment['inputAttributes']['maxlength'] = $max;
235
                    }
236
                }
237
238 5
                if ($rule instanceof Regex) {
239 2
                    if (!$rule->isNot()) {
240 1
                        $enrichment['inputAttributes']['pattern'] = Html::normalizeRegexpPattern(
241 1
                            $rule->getPattern()
242 1
                        );
243
                    }
244
                }
245
            }
246 6
            return $enrichment;
247
        }
248
249 12
        if ($field instanceof Textarea) {
250 3
            $enrichment = [];
251 3
            foreach ($rules as $rule) {
252 3
                if ($rule instanceof WhenInterface && $rule->getWhen() !== null) {
253 1
                    continue;
254
                }
255
256 2
                if ($rule instanceof Required) {
257 1
                    $enrichment['inputAttributes']['required'] = true;
258
                }
259
260 2
                if ($rule instanceof Length) {
261 1
                    if (null !== $min = $rule->getMin()) {
262 1
                        $enrichment['inputAttributes']['minlength'] = $min;
263
                    }
264 1
                    if (null !== $max = $rule->getMax()) {
265 1
                        $enrichment['inputAttributes']['maxlength'] = $max;
266
                    }
267
                }
268
            }
269 3
            return $enrichment;
270
        }
271
272 9
        if ($field instanceof Url) {
273 9
            $enrichment = [];
274 9
            foreach ($rules as $rule) {
275 9
                if ($rule instanceof WhenInterface && $rule->getWhen() !== null) {
276
                    continue;
277
                }
278
279 9
                if ($rule instanceof Required) {
280 1
                    $enrichment['inputAttributes']['required'] = true;
281
                }
282
283 9
                if ($rule instanceof Length) {
284 1
                    if (null !== $min = $rule->getMin()) {
285 1
                        $enrichment['inputAttributes']['minlength'] = $min;
286
                    }
287 1
                    if (null !== $max = $rule->getMax()) {
288 1
                        $enrichment['inputAttributes']['maxlength'] = $max;
289
                    }
290
                }
291
292 9
                $pattern = null;
293 9
                if ($rule instanceof UrlRule) {
294 5
                    $pattern = $rule->isIdnEnabled() ? null : $rule->getPattern();
295
                }
296 9
                if ($pattern === null && $rule instanceof Regex) {
297 5
                    if (!$rule->isNot()) {
298 4
                        $pattern = $rule->getPattern();
299
                    }
300
                }
301 9
                if ($pattern !== null) {
302 5
                    $enrichment['inputAttributes']['pattern'] = Html::normalizeRegexpPattern($pattern);
303
                }
304
            }
305 9
            return $enrichment;
306
        }
307
308
        return null;
309
    }
310
}
311