Passed
Push — master ( ab973d...b3288f )
by Smoren
02:37 queued 11s
created

AnyOfRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 29
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 24 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Rules;
6
7
use Smoren\Validator\Exceptions\ValidationError;
8
use Smoren\Validator\Interfaces\ValidationResultInterface;
9
use Smoren\Validator\Structs\ValidationSuccessResult;
10
11
class AnyOfRule extends CompositeRule
12
{
13
    /**
14
     * {@inheritDoc}
15
     */
16 9
    protected function execute($value): ValidationResultInterface
17
    {
18 9
        $result = parent::execute($value);
19 7
        if ($result->preventNextChecks()) {
20 2
            return $result;
21
        }
22
23 7
        $result = new ValidationSuccessResult(false);
24
25 7
        $errors = [];
26 7
        foreach ($this->rules as $rule) {
27
            try {
28 6
                $rule->validate($value);
29 3
                return $result;
30 6
            } catch (ValidationError $e) {
31 6
                $errors[] = $e;
32
            }
33
        }
34
35 4
        if (\count($errors) === 0) {
36 1
            return $result;
37
        }
38
39 3
        throw ValidationError::fromValidationErrors($this->name, $value, $errors);
40
    }
41
}
42