Passed
Pull Request — master (#160)
by Wilmer
04:21 queued 01:32
created

FieldValidator::getValidatorAttributes()   F

Complexity

Conditions 16
Paths 301

Size

Total Lines 52
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 16

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 22
c 1
b 0
f 0
nc 301
nop 4
dl 0
loc 52
ccs 23
cts 23
cp 1
crap 16
rs 3.4708

How to fix   Long Method    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\Widget\Validator;
6
7
use Yiisoft\Form\FormModelInterface;
8
use Yiisoft\Form\Widget\Attribute\WidgetAttributes;
9
use Yiisoft\Form\Widget\Url;
10
use Yiisoft\Html\Html;
11
use Yiisoft\Validator\Rule;
12
use Yiisoft\Validator\Rule\HasLength;
13
use Yiisoft\Validator\Rule\MatchRegularExpression;
14
use Yiisoft\Validator\Rule\Number;
15
use Yiisoft\Validator\Rule\Required;
16
use Yiisoft\Validator\Rule\Url as UrlValidator;
17
18
/**
19
 * FieldValidator is a base class for validators that can be applied to a field.
20
 *
21
 * @param WidgetAttributes $widget The field widget.
22
 * @param FormModelInterface $formModel The form model instance.
23
 * @param string $attribute The attribute name or expression.
24
 * @param array $attributes The HTML attributes for the field widget.
25
 *
26
 * @return array The attributes for validator html.
27
 */
28
final class FieldValidator
29
{
30 599
    public function getValidatorAttributes(
31
        WidgetAttributes $widget,
32
        FormModelInterface $formModel,
33
        string $attribute,
34
        array $attributes
35
    ): array {
36
        /** @psalm-var array<array-key, Rule> */
37 599
        $rules = $formModel->getRules()[$attribute] ?? [];
38
39 599
        foreach ($rules as $rule) {
40 73
            if ($rule instanceof Required) {
41 33
                $attributes['required'] = true;
42
            }
43
44 73
            if ($rule instanceof HasLength && $widget instanceof HasLengthInterface) {
45
                /** @var int|null */
46 24
                $attributes['maxlength'] = $rule->getOptions()['max'] !== 0 ? $rule->getOptions()['max'] : null;
47
                /** @var int|null */
48 24
                $attributes['minlength'] = $rule->getOptions()['min'] !== 0 ? $rule->getOptions()['min'] : null;
49
            }
50
51 73
            if ($rule instanceof MatchRegularExpression && $widget instanceof MatchRegularInterface) {
52
                /** @var string */
53 10
                $pattern = $rule->getOptions()['pattern'];
54 10
                $attributes['pattern'] = Html::normalizeRegexpPattern($pattern);
55
            }
56
57 73
            if ($rule instanceof Number && $widget instanceof NumberInterface) {
58
                /** @var int|null */
59 4
                $attributes['max'] = $rule->getOptions()['max'] !== 0 ? $rule->getOptions()['max'] : null;
60
                /** @var int|null */
61 4
                $attributes['min'] = $rule->getOptions()['min'] !== 0 ? $rule->getOptions()['min'] : null;
62
            }
63
64 73
            if ($rule instanceof UrlValidator && $widget instanceof Url) {
65
                /** @var array<array-key, string> */
66 2
                $validSchemes = $rule->getOptions()['validSchemes'];
67
68 2
                $schemes = [];
69
70 2
                foreach ($validSchemes as $scheme) {
71 2
                    $schemes[] = $this->getSchemePattern($scheme);
72
                }
73
74
                /** @var array<array-key, float|int|string>|string */
75 2
                $pattern = $rule->getOptions()['pattern'];
76 2
                $normalizePattern = str_replace('{schemes}', '(' . implode('|', $schemes) . ')', $pattern);
77 2
                $attributes['pattern'] = Html::normalizeRegexpPattern($normalizePattern);
78
            }
79
        }
80
81 599
        return $attributes;
82
    }
83
84 2
    private function getSchemePattern(string $scheme): string
85
    {
86 2
        $result = '';
87
88 2
        for ($i = 0, $length = mb_strlen($scheme); $i < $length; $i++) {
89 2
            $result .= '[' . mb_strtolower($scheme[$i]) . mb_strtoupper($scheme[$i]) . ']';
90
        }
91
92 2
        return $result;
93
    }
94
}
95