Passed
Push — master ( d3853d...011878 )
by Smoren
02:21
created

OrRule::execute()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 24
ccs 14
cts 14
cp 1
rs 9.4888
cc 5
nc 6
nop 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Rules;
6
7
use Smoren\Validator\Exceptions\ValidationSuccessException;
8
use Smoren\Validator\Exceptions\ValidationError;
9
use Smoren\Validator\Interfaces\ExecutionResultInterface;
10
use Smoren\Validator\Structs\ExecutionResult;
11
12
class OrRule extends CompositeRule
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17 9
    protected function execute($value): ExecutionResultInterface
18
    {
19 9
        $result = parent::execute($value);
20 7
        if ($result->areChecksSufficient()) {
21 2
            return $result;
22
        }
23
24 7
        $result = new ExecutionResult(false);
25
26 7
        $errors = [];
27 7
        foreach ($this->rules as $rule) {
28
            try {
29 6
                $rule->validate($value);
30 3
                return $result;
31 6
            } catch (ValidationError $e) {
32 6
                $errors[] = $e;
33
            }
34
        }
35
36 4
        if (\count($errors) === 0) {
37 1
            return $result;
38
        }
39
40 3
        throw ValidationError::fromValidationErrors($value, $errors);
41
    }
42
}
43