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 — master (#1022)
by William
02:28
created

Each::assert()   C

Complexity

Conditions 8
Paths 21

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 33.2361

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 4
cts 15
cp 0.2667
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 16
nc 21
nop 1
crap 33.2361
1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Respect\Validation\Rules;
15
16
use Respect\Validation\Exceptions\ValidationException;
17
use Respect\Validation\Helpers\CanValidateIterable;
18
use Respect\Validation\Validatable;
19
20
/**
21
 * Iterates over an array or Iterator and validates the value or key
22
 * of each entry:
23
 *
24
 * @author Alexandre Gomes Gaigalas <[email protected]>
25
 * @author Henrique Moody <[email protected]>
26
 * @author Nick Lombard <[email protected]>
27
 * @author William Espindola <[email protected]>
28
 */
29
final class Each extends AbstractRule
30
{
31
    use CanValidateIterable;
32
33
    /**
34
     * @var Validatable
35
     */
36
    private $itemValidator;
37
38
    /**
39
     * @var Validatable
40
     */
41
    private $keyValidator;
42
43
    /**
44
     * @param Validatable $keyValidator
45
     * @param Validatable $itemValidator
46
     */
47 1
    public function __construct(Validatable $itemValidator = null, Validatable $keyValidator = null)
48
    {
49 1
        $this->itemValidator = $itemValidator;
50 1
        $this->keyValidator = $keyValidator;
51 1
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 1
    public function assert($input): void
57
    {
58 1
        $exceptions = [];
59
60 1
        if (!$this->isIterable($input)) {
61 1
            throw $this->reportError($input);
62
        }
63
64
        foreach ($input as $key => $item) {
65
            if (isset($this->itemValidator)) {
66
                try {
67
                    $this->itemValidator->assert($item);
68
                } catch (ValidationException $e) {
69
                    $exceptions[] = $e;
70
                }
71
            }
72
73
            if (isset($this->keyValidator)) {
74
                try {
75
                    $this->keyValidator->assert($key);
76
                } catch (ValidationException $e) {
77
                    $exceptions[] = $e;
78
                }
79
            }
80
        }
81
82
        if (!empty($exceptions)) {
83
            throw $this->reportError($input)->setRelated($exceptions);
0 ignored issues
show
Bug introduced by
The method setRelated() does not exist on Respect\Validation\Exceptions\ValidationException. It seems like you code against a sub-type of Respect\Validation\Exceptions\ValidationException such as Respect\Validation\Excep...stedValidationException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
            throw $this->reportError($input)->/** @scrutinizer ignore-call */ setRelated($exceptions);
Loading history...
84
        }
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 1
    public function check($input): void
91
    {
92 1
        if (!$this->isIterable($input)) {
93 1
            throw $this->reportError($input);
94
        }
95
96
        foreach ($input as $key => $item) {
97
            if (isset($this->itemValidator)) {
98
                $this->itemValidator->check($item);
99
            }
100
101
            if (isset($this->keyValidator)) {
102
                $this->keyValidator->check($key);
103
            }
104
        }
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 14
    public function validate($input): bool
111
    {
112 14
        if (!$this->isIterable($input)) {
113 4
            return false;
114
        }
115
116 10
        foreach ($input as $key => $item) {
117 10
            if (isset($this->itemValidator) && !$this->itemValidator->validate($item)) {
118 2
                return false;
119
            }
120
121 8
            if (isset($this->keyValidator) && !$this->keyValidator->validate($key)) {
122 8
                return false;
123
            }
124
        }
125
126 6
        return true;
127
    }
128
}
129