Passed
Pull Request — master (#331)
by
unknown
03:53 queued 01:09
created

PreValidateTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B preValidate() 0 25 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
            return ($rule->getWhen() !== null) && !($rule->getWhen())($value, $context);
40
        }
41
42 3
        return false;
43
    }
44
}
45