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

ValidatorFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 37
ccs 17
cts 17
cp 1
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeRules() 0 9 3
A __construct() 0 1 1
A normalizeRule() 0 13 3
A create() 0 3 1
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