Completed
Push — master ( a5dee7...adf519 )
by Changwan
04:02
created

CollectionValidator::assert()   D

Complexity

Conditions 10
Paths 26

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 11.7759

Importance

Changes 0
Metric Value
cc 10
eloc 20
nc 26
nop 1
dl 0
loc 31
ccs 17
cts 23
cp 0.7391
crap 11.7759
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\Rules;
3
4
use Traversable;
5
use Wandu\Validator\Exception\InvalidValueException;
6
use function Wandu\Validator\validator;
7
8
class CollectionValidator extends ValidatorAbstract
9
{
10
    const ERROR_TYPE = 'collection';
11
12
    /** @var \Wandu\Validator\Contracts\ValidatorInterface */
13
    protected $validator;
14
15
    /**
16
     * @param mixed $rule
17
     */
18 12
    public function __construct($rule = null)
19
    {
20 12
        if ($rule) {
21 10
            $this->validator = validator()->from($rule);
22 10
        }
23 12
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 12
    function test($items)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
29
    {
30 12
        return is_array($items) || $items instanceof Traversable;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 8
    public function assert($items)
37
    {
38 8
        if (!isset($items)) $items = [];
39
        /** @var \Wandu\Validator\Exception\InvalidValueException[] $exceptions */
40 8
        $exceptions = [];
41 8
        if (!$this->test($items)) {
42 4
            throw $this->createException();
43
        }
44 8
        if ($this->validator) {
45 8
            foreach ($items as $key => $item) {
46 8
                if (!is_int($key)) {
47 2
                    $exceptions['.'] = $this->createException();
48
                    continue;
49
                }
50
                try {
51 8
                    $this->validator->assert($item);
52 8
                } catch (InvalidValueException $e) {
53 8
                    $exceptions[$key] = $e;
54
                }
55 8
            }
56 8
        } else {
57
            foreach ($items as $key => $item) {
58
                if (!is_int($key)) {
59
                    $exceptions['.'] = $this->createException();
60
                }
61
            }
62
        }
63 8
        if (count($exceptions)) {
64 8
            throw InvalidValueException::merge($exceptions);
65
        }
66 8
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 4
    public function validate($items)
72
    {
73 4
        if (!isset($items)) $items = [];
74 4
        if (!$this->test($items)) return false;
75
76 4
        if ($this->validator) {
77 2
            foreach ($items as $key => $item) {
78 2
                if (!is_int($key)) return false;
79 2
                if (!$this->validator->validate($item)) return false;
80 2
            }
81 2
        } else {
82 2
            foreach ($items as $key => $item) {
83 2
                if (!is_int($key)) return false;
84 2
            }
85
        }
86 4
        return true;
87
    }
88
}
89