Passed
Push — master ( a49c1d...c1bebe )
by Smoren
02:58
created

AndRule::execute()   A

Complexity

Conditions 5
Paths 7

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
cc 5
eloc 14
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 24
ccs 14
cts 14
cp 1
crap 5
rs 9.4888
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 AndRule extends CompositeRule
12
{
13
    /**
14
     * {@inheritDoc}
15
     */
16 7
    protected function execute($value): ValidationResultInterface
17
    {
18 7
        $result = parent::execute($value);
19 6
        if ($result->preventNextChecks()) {
20 1
            return $result;
21
        }
22
23 6
        $result = new ValidationSuccessResult(false);
24
25 6
        $errors = [];
26 6
        foreach ($this->rules as $rule) {
27
            try {
28 5
                $rule->validate($value);
29 3
            } catch (ValidationError $e) {
30 3
                $errors[] = $e;
31 3
                break;
32
            }
33
        }
34
35 6
        if (\count($errors) === 0) {
36 3
            return $result;
37
        }
38
39 3
        throw ValidationError::fromValidationErrors($this->name, $value, $errors);
40
    }
41
}
42