Passed
Pull Request — master (#99)
by Def
02:29 queued 11s
created

ValidatorFactory::normalizeRule()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Yiisoft\Validator\Rule\Callback;
8
9
final class ValidatorFactory implements ValidatorFactoryInterface
10
{
11 2
    public function __construct() {
12 2
    }
13
14 2
    public function create(array $rules): ValidatorInterface
15
    {
16 2
        return new Validator($this->normalizeRules($rules));
17
    }
18
19 2
    private function normalizeRules(array $rules)
20
    {
21 2
        foreach ($rules as $attribute => $ruleSets) {
22 2
            foreach ($ruleSets as $index => $rule) {
23 2
                $ruleSets[$index] = $this->normalizeRule($rule);
24
            }
25 1
            $rules[$attribute] = $ruleSets;
26
        }
27 1
        return $rules;
28
    }
29
30
    /**
31
     * @param callable|Rule $rule
32
     */
33 2
    private function normalizeRule($rule): Rule
34
    {
35 2
        if (is_callable($rule)) {
36 1
            $rule = new Callback($rule);
0 ignored issues
show
Bug introduced by
It seems like $rule can also be of type Yiisoft\Validator\Rule; however, parameter $callback of Yiisoft\Validator\Rule\Callback::__construct() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
            $rule = new Callback(/** @scrutinizer ignore-type */ $rule);
Loading history...
37
        }
38
39 2
        if (!$rule instanceof Rule) {
40 1
            throw new \InvalidArgumentException(
41 1
                'Rule should be either instance of Rule class or a callable'
42
            );
43
        }
44
45 1
        return $rule;
46
    }
47
}
48