Passed
Push — master ( bf8929...f56605 )
by Smoren
02:31
created

AnyOfCheck   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 1
b 0
f 0
dl 0
loc 23
ccs 10
cts 10
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Checks;
6
7
use Smoren\Validator\Exceptions\CompositeCheckError;
8
use Smoren\Validator\Exceptions\ValidationError;
9
use Smoren\Validator\Interfaces\CheckInterface;
10
use Smoren\Validator\Structs\CheckName;
11
12
final class AnyOfCheck extends CompositeCheck implements CheckInterface
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17 7
    public function __invoke($value, array $previousErrors, bool $preventDuplicate = false): void
18
    {
19 7
        $errors = [];
20
21 7
        foreach ($this->rules as $rule) {
22
            try {
23 6
                $rule->validate($value);
24 3
                return;
25 6
            } catch (ValidationError $e) {
26 6
                $errors[] = $e;
27
            }
28
        }
29
30 4
        if (\count($errors) === 0) {
31 1
            return;
32
        }
33
34 3
        throw new CompositeCheckError(CheckName::ANY_OF, $value, $errors);
35
    }
36
}
37