Passed
Pull Request — master (#99)
by Def
03:05 queued 46s
created

ValidatorFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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

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