Passed
Pull Request — master (#274)
by
unknown
02:35
created

LimitHandlerTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 17
c 1
b 0
f 0
dl 0
loc 33
ccs 17
cts 17
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B validateLimits() 0 31 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\Trait;
6
7
use Yiisoft\Validator\Result;
8
use Yiisoft\Validator\ValidationContext;
9
10
trait LimitHandlerTrait
11
{
12 43
    private function validateLimits(
13
        mixed $value,
14
        object $rule,
15
        ValidationContext $context,
16
        int $number,
17
        Result $result
18
    ): void {
19 43
        if ($rule->getExactly() !== null && $number !== $rule->getExactly()) {
20 3
            $formattedMessage = $this->formatter->format(
21 3
                $rule->getNotExactlyMessage(),
22 3
                ['exactly' => $rule->getExactly(), 'attribute' => $context->getAttribute(), 'value' => $value]
23
            );
24 3
            $result->addError($formattedMessage);
25
26 3
            return;
27
        }
28
29 40
        if ($rule->getMin() !== null && $number < $rule->getMin()) {
30 13
            $formattedMessage = $this->formatter->format(
31 13
                $rule->getLessThanMinMessage(),
32 13
                ['min' => $rule->getMin(), 'attribute' => $context->getAttribute(), 'value' => $value]
33
            );
34 13
            $result->addError($formattedMessage);
35
        }
36
37 40
        if ($rule->getMax() !== null && $number > $rule->getMax()) {
38 4
            $formattedMessage = $this->formatter->format(
39 4
                $rule->getGreaterThanMaxMessage(),
40 4
                ['max' => $rule->getMax(), 'attribute' => $context->getAttribute(), 'value' => $value]
41
            );
42 4
            $result->addError($formattedMessage);
43
        }
44
    }
45
}
46