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

ValidatorFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 13
c 3
b 1
f 0
dl 0
loc 38
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 2 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
    {
13 2
    }
14
15 2
    public function create(array $rules): ValidatorInterface
16
    {
17 2
        return new Validator($this->normalizeRules($rules));
18
    }
19
20 2
    private function normalizeRules(array $rules)
21
    {
22 2
        foreach ($rules as $attribute => $ruleSets) {
23 2
            foreach ($ruleSets as $index => $rule) {
24 2
                $ruleSets[$index] = $this->normalizeRule($rule);
25
            }
26 1
            $rules[$attribute] = $ruleSets;
27
        }
28 1
        return $rules;
29
    }
30
31
    /**
32
     * @param callable|Rule $rule
33
     */
34 2
    private function normalizeRule($rule): Rule
35
    {
36 2
        if (is_callable($rule)) {
37 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

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