Conditions | 16 |
Paths | 301 |
Total Lines | 52 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
30 | 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 | $rules = $formModel->getRules()[$attribute] ?? []; |
||
38 | |||
39 | foreach ($rules as $rule) { |
||
40 | if ($rule instanceof Required) { |
||
41 | $attributes['required'] = true; |
||
42 | } |
||
43 | |||
44 | if ($rule instanceof HasLength && $widget instanceof HasLengthInterface) { |
||
45 | /** @var int|null */ |
||
46 | $attributes['maxlength'] = $rule->getOptions()['max'] !== 0 ? $rule->getOptions()['max'] : null; |
||
47 | /** @var int|null */ |
||
48 | $attributes['minlength'] = $rule->getOptions()['min'] !== 0 ? $rule->getOptions()['min'] : null; |
||
49 | } |
||
50 | |||
51 | if ($rule instanceof MatchRegularExpression && $widget instanceof MatchRegularInterface) { |
||
52 | /** @var string */ |
||
53 | $pattern = $rule->getOptions()['pattern']; |
||
54 | $attributes['pattern'] = Html::normalizeRegexpPattern($pattern); |
||
55 | } |
||
56 | |||
57 | if ($rule instanceof Number && $widget instanceof NumberInterface) { |
||
58 | /** @var int|null */ |
||
59 | $attributes['max'] = $rule->getOptions()['max'] !== 0 ? $rule->getOptions()['max'] : null; |
||
60 | /** @var int|null */ |
||
61 | $attributes['min'] = $rule->getOptions()['min'] !== 0 ? $rule->getOptions()['min'] : null; |
||
62 | } |
||
63 | |||
64 | if ($rule instanceof UrlValidator && $widget instanceof Url) { |
||
65 | /** @var array<array-key, string> */ |
||
66 | $validSchemes = $rule->getOptions()['validSchemes']; |
||
67 | |||
68 | $schemes = []; |
||
69 | |||
70 | foreach ($validSchemes as $scheme) { |
||
71 | $schemes[] = $this->getSchemePattern($scheme); |
||
72 | } |
||
73 | |||
74 | /** @var array<array-key, float|int|string>|string */ |
||
75 | $pattern = $rule->getOptions()['pattern']; |
||
76 | $normalizePattern = str_replace('{schemes}', '(' . implode('|', $schemes) . ')', $pattern); |
||
77 | $attributes['pattern'] = Html::normalizeRegexpPattern($normalizePattern); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | return $attributes; |
||
82 | } |
||
95 |