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

PreValidateTrait::preValidate()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 25
ccs 11
cts 11
cp 1
rs 8.4444
c 1
b 0
f 0
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
            return ($rule->getWhen() !== null) && !($rule->getWhen())($value, $context);
40
        }
41
42 3
        return false;
43
    }
44
}
45