Passed
Pull Request — master (#331)
by Sergei
02:46
created

PreValidateTrait::preValidate()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 26
ccs 12
cts 12
cp 1
rs 8.4444
cc 8
nc 5
nop 3
crap 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\Trait;
6
7
use Yiisoft\Validator\RuleInterface;
8
use Yiisoft\Validator\SkipOnEmptyInterface;
9
use Yiisoft\Validator\SkipOnEmptyNormalizer;
10
use Yiisoft\Validator\SkipOnErrorInterface;
11
use Yiisoft\Validator\ValidationContext;
12
use Yiisoft\Validator\WhenInterface;
13
14
trait PreValidateTrait
15
{
16
    private string $parameterPreviousRulesErrored = 'previousRulesErrored';
17
18 124
    private function preValidate(
19
        $value,
20
        ValidationContext $context,
21
        RuleInterface $rule
22
    ): bool {
23
        if (
24 124
            $rule instanceof SkipOnEmptyInterface &&
25 124
            (SkipOnEmptyNormalizer::normalize($rule->getSkipOnEmpty()))($value, $context->isAttributeMissing())
26
        ) {
27 22
            return true;
28
        }
29
30
        if (
31 119
            $rule instanceof SkipOnErrorInterface
32 119
            && $rule->shouldSkipOnError()
33 119
            && $context->getParameter($this->parameterPreviousRulesErrored) === true
34
        ) {
35 4
            return true;
36
        }
37
38 118
        if ($rule instanceof WhenInterface) {
39 115
            $when = $rule->getWhen();
40 115
            return $when !== null && !$when($value, $context);
41
        }
42
43 3
        return false;
44
    }
45
}
46