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

Passed
Push — master ( 2ab1f1...70eb87 )
by Henrique
05:41
created

Each::check()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 11.1035

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 3
cts 8
cp 0.375
rs 9.5222
c 0
b 0
f 0
cc 5
nc 6
nop 1
crap 11.1035
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
class Each extends AbstractRule
21
{
22
    use CanValidateIterable;
23
24
    public $itemValidator;
25
    public $keyValidator;
26
27 4
    public function __construct(Validatable $itemValidator = null, Validatable $keyValidator = null)
28
    {
29 4
        $this->itemValidator = $itemValidator;
30 4
        $this->keyValidator = $keyValidator;
31 4
    }
32
33 3
    public function assert($input): void
34
    {
35 3
        $exceptions = [];
36
37 3
        if (!$this->isIterable($input)) {
38 1
            throw $this->reportError($input);
39
        }
40
41 2
        foreach ($input as $key => $item) {
42 2
            if (isset($this->itemValidator)) {
43
                try {
44 1
                    $this->itemValidator->assert($item);
45 1
                } catch (ValidationException $e) {
46 1
                    $exceptions[] = $e;
47
                }
48
            }
49
50 2
            if (isset($this->keyValidator)) {
51
                try {
52 1
                    $this->keyValidator->assert($key);
53 1
                } catch (ValidationException $e) {
54 2
                    $exceptions[] = $e;
55
                }
56
            }
57
        }
58
59 2
        if (!empty($exceptions)) {
60 2
            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

60
            throw $this->reportError($input)->/** @scrutinizer ignore-call */ setRelated($exceptions);
Loading history...
61
        }
62
    }
63
64 1
    public function check($input): void
65
    {
66 1
        if (!$this->isIterable($input)) {
67 1
            throw $this->reportError($input);
68
        }
69
70
        foreach ($input as $key => $item) {
71
            if (isset($this->itemValidator)) {
72
                $this->itemValidator->check($item);
73
            }
74
75
            if (isset($this->keyValidator)) {
76
                $this->keyValidator->check($key);
77
            }
78
        }
79
    }
80
81 11
    public function validate($input): bool
82
    {
83 11
        if (!$this->isIterable($input)) {
84 4
            return false;
85
        }
86
87 7
        foreach ($input as $key => $item) {
88 7
            if (isset($this->itemValidator) && !$this->itemValidator->validate($item)) {
89 1
                return false;
90
            }
91
92 6
            if (isset($this->keyValidator) && !$this->keyValidator->validate($key)) {
93 6
                return false;
94
            }
95
        }
96
97 5
        return true;
98
    }
99
}
100