Completed
Push — master ( e15c58...b150a8 )
by Changwan
07:08
created

CollectionValidator::assert()   D

Complexity

Conditions 10
Paths 26

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 12.8177

Importance

Changes 0
Metric Value
cc 10
eloc 20
nc 26
nop 1
dl 0
loc 31
ccs 16
cts 23
cp 0.6957
crap 12.8177
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 6
    public function __construct($rule = null)
19
    {
20 6
        if ($rule) {
21 5
            $this->validator = validator()->from($rule);
22 5
        }
23 6
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 6
    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 6
        return is_array($items) || $items instanceof Traversable;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 4
    public function assert($items)
37
    {
38 4
        if (!isset($items)) $items = [];
39
        /** @var \Wandu\Validator\Exception\InvalidValueException[] $exceptions */
40 4
        $exceptions = [];
41 4
        if (!$this->test($items)) {
42 2
            throw $this->createException();
43
        }
44 4
        if ($this->validator) {
45 4
            foreach ($items as $key => $item) {
46 4
                if (!is_int($key)) {
47
                    $exceptions['.'] = $this->createException();
48
                    continue;
49
                }
50
                try {
51 4
                    $this->validator->assert($item);
52 4
                } catch (InvalidValueException $e) {
53 3
                    $exceptions[$key] = $e;
54
                }
55 4
            }
56 4
        } else {
57
            foreach ($items as $key => $item) {
58
                if (!is_int($key)) {
59
                    $exceptions['.'] = $this->createException();
60
                }
61
            }
62
        }
63 4
        if (count($exceptions)) {
64 3
            throw InvalidValueException::merge($exceptions);
65
        }
66 4
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 2
    public function validate($items)
72
    {
73 2
        if (!isset($items)) $items = [];
74 2
        if (!$this->test($items)) return false;
75
76 2
        if ($this->validator) {
77 1
            foreach ($items as $key => $item) {
78 1
                if (!is_int($key)) return false;
79 1
                if (!$this->validator->validate($item)) return false;
80 1
            }
81 1
        } else {
82 1
            foreach ($items as $key => $item) {
83 1
                if (!is_int($key)) return false;
84 1
            }
85
        }
86 2
        return true;
87
    }
88
}
89