Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — 0.9 (#694)
by
unknown
03:32
created

Each::assert()   D

Complexity

Conditions 10
Paths 22

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 10.0107

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 20
cts 21
cp 0.9524
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 20
nc 22
nop 1
crap 10.0107

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 Respect\Validation\Rules;
3
4
use Traversable;
5
use Respect\Validation\Validatable;
6
use Respect\Validation\Exceptions\ValidationException;
7
8
class Each extends AbstractRule
9
{
10
    public $itemValidator;
11
    public $keyValidator;
12
13 8
    public function __construct(Validatable $itemValidator = null, Validatable $keyValidator = null)
14
    {
15 8
        $this->itemValidator = $itemValidator;
16 8
        $this->keyValidator = $keyValidator;
17 8
    }
18
19 6
    public function assert($input)
20
    {
21 6
        $exceptions = array();
22
23 6
        if (!is_array($input) || $input instanceof Traversable) {
24 1
            throw $this->reportError($input);
25
        }
26
27 5
        if (empty($input)) {
28
            return true;
29
        }
30
31 5
        foreach ($input as $key => $item) {
32 5
            if (isset($this->itemValidator)) {
33
                try {
34 3
                    $this->itemValidator->assert($item);
35 3
                } catch (ValidationException $e) {
36 1
                    $exceptions[] = $e;
37
                }
38 3
            }
39
40 5
            if (isset($this->keyValidator)) {
41
                try {
42 3
                    $this->keyValidator->assert($key);
43 3
                } catch (ValidationException $e) {
44 1
                    $exceptions[] = $e;
45
                }
46 3
            }
47 5
        }
48
49 5
        if (!empty($exceptions)) {
50 2
            throw $this->reportError($input)->setRelated($exceptions);
51
        }
52
53 3
        return true;
54
    }
55
56 3
    public function check($input)
57
    {
58 3
        if (empty($input)) {
59
            return true;
60
        }
61
62 3
        if (!is_array($input) || $input instanceof Traversable) {
63
            throw $this->reportError($input);
64
        }
65
66 3
        foreach ($input as $key => $item) {
67 3
            if (isset($this->itemValidator)) {
68 2
                $this->itemValidator->check($item);
69 2
            }
70
71 3
            if (isset($this->keyValidator)) {
72 2
                $this->keyValidator->check($key);
73 2
            }
74 3
        }
75
76 3
        return true;
77
    }
78
79 5
    public function validate($input)
80
    {
81 5
        if (!is_array($input) || $input instanceof Traversable) {
82 1
            return false;
83
        }
84
85 4
        if (empty($input)) {
86
            return true;
87
        }
88
89 4
        foreach ($input as $key => $item) {
90 4
            if (isset($this->itemValidator) && !$this->itemValidator->validate($item)) {
91 1
                return false;
92
            }
93
94 3
            if (isset($this->keyValidator) && !$this->keyValidator->validate($key)) {
95
                return false;
96
            }
97 3
        }
98
99 3
        return true;
100
    }
101
}
102