ValidatorNormalizer::normalize()   D
last analyzed

Complexity

Conditions 10
Paths 18

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 18
nc 18
nop 1
dl 0
loc 27
ccs 16
cts 16
cp 1
crap 10
rs 4.8196
c 0
b 0
f 0

How to fix   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
namespace Wandu\Validator;
3
4
use Wandu\Validator\Contracts\Rule;
5
use Wandu\Validator\Contracts\RuleNormalizable;
6
7
class ValidatorNormalizer implements RuleNormalizable
8
{
9 12
    public function normalize($rule): array
10
    {
11 12
        while (is_callable($rule) || (is_object($rule) && $rule instanceof Rule)) {
12 5
            if (is_callable($rule)) {
13 4
                $rule = call_user_func($rule);
14
            } else {
15 4
                $rule = $rule->rules();
16
            }
17
        }
18
        // string -> array
19 12
        if (!is_array($rule)) {
20 11
            $rule = [$rule];
21
        }
22 12
        $normalized = [[], []];
23 12
        foreach ($rule as $key => $value) {
24 12
            if (is_int($key) || $key === '' || $key === null) {
25 12
                $normalized[0] = array_merge($normalized[0] ?? [], (array) $value);
26
            } else {
27 9
                $target = TargetName::parse($key);
28 9
                $normalized[1][] = [
29 9
                    [$target->getName(), $target->getIterator(), $target->isOptional(), ],
30 12
                    $this->normalize($value),
31
                ];
32
            }
33
        }
34 12
        return $normalized;
35
    }
36
}
37