Passed
Pull Request — master (#364)
by
unknown
04:18 queued 01:31
created

LimitHandlerTrait::validateLimits()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 34
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 9.0197

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 34
ccs 15
cts 16
cp 0.9375
rs 8.0555
cc 9
nc 6
nop 4
crap 9.0197
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\Trait;
6
7
use InvalidArgumentException;
8
use Yiisoft\Validator\LimitInterface;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\RuleInterface;
11
use Yiisoft\Validator\ValidationContext;
12
13
/**
14
 * A trait attachable to a handler of rule with limits.
15
 */
16
trait LimitHandlerTrait
17
{
18
    /**
19
     * Runs limits specific validation.
20
     *
21
     * @param LimitInterface|RuleInterface $rule A rule matching to this handler.
22
     * @param ValidationContext $context Validation context.
23
     * @param int $number A validated number to compare with set limits.
24
     * @param Result $result Result for adding errors.
25
     *
26
     * @see LimitTrait for information about limits and messages.
27
     */
28 83
    private function validateLimits(
29
        LimitInterface|RuleInterface $rule,
30
        ValidationContext $context,
31
        int $number,
32
        Result $result
33
    ): void {
34 83
        if (!$rule instanceof LimitInterface || !$rule instanceof RuleInterface) {
35
            throw new InvalidArgumentException('$rule must implement both LimitInterface and RuleInterface.');
36
        }
37
38
        /**
39
         * @var LimitTrait|RuleInterface $rule
40
         * @psalm-ignore-var
41
         */
42 83
        if ($rule->getExactly() !== null && $number !== $rule->getExactly()) {
0 ignored issues
show
Bug introduced by
The method getExactly() does not exist on Yiisoft\Validator\RuleInterface. It seems like you code against a sub-type of Yiisoft\Validator\RuleInterface such as Yiisoft\Validator\Rule\HasLength or Yiisoft\Validator\Rule\Count. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        if ($rule->/** @scrutinizer ignore-call */ getExactly() !== null && $number !== $rule->getExactly()) {
Loading history...
43 3
            $result->addError($rule->getNotExactlyMessage(), [
0 ignored issues
show
Bug introduced by
The method getNotExactlyMessage() does not exist on Yiisoft\Validator\RuleInterface. It seems like you code against a sub-type of Yiisoft\Validator\RuleInterface such as Yiisoft\Validator\Rule\HasLength or Yiisoft\Validator\Rule\Count. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
            $result->addError($rule->/** @scrutinizer ignore-call */ getNotExactlyMessage(), [
Loading history...
44 3
                'exactly' => $rule->getExactly(),
45 3
                'attribute' => $context->getAttribute(),
46
            ]);
47
48 3
            return;
49
        }
50
51 80
        if ($rule->getMin() !== null && $number < $rule->getMin()) {
0 ignored issues
show
Bug introduced by
The method getMin() does not exist on Yiisoft\Validator\RuleInterface. It seems like you code against a sub-type of Yiisoft\Validator\RuleInterface such as Yiisoft\Validator\Rule\AtLeast or Yiisoft\Validator\Rule\HasLength or Yiisoft\Validator\Rule\Number or Yiisoft\Validator\Rule\Count. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        if ($rule->/** @scrutinizer ignore-call */ getMin() !== null && $number < $rule->getMin()) {
Loading history...
52 42
            $result->addError($rule->getLessThanMinMessage(), [
0 ignored issues
show
Bug introduced by
The method getLessThanMinMessage() does not exist on Yiisoft\Validator\RuleInterface. It seems like you code against a sub-type of Yiisoft\Validator\RuleInterface such as Yiisoft\Validator\Rule\HasLength or Yiisoft\Validator\Rule\Count. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
            $result->addError($rule->/** @scrutinizer ignore-call */ getLessThanMinMessage(), [
Loading history...
53 42
                'min' => $rule->getMin(),
54 42
                'attribute' => $context->getAttribute(),
55
            ]);
56
        }
57
58 80
        if ($rule->getMax() !== null && $number > $rule->getMax()) {
0 ignored issues
show
Bug introduced by
The method getMax() does not exist on Yiisoft\Validator\RuleInterface. It seems like you code against a sub-type of Yiisoft\Validator\RuleInterface such as Yiisoft\Validator\Rule\HasLength or Yiisoft\Validator\Rule\Number or Yiisoft\Validator\Rule\Count. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        if ($rule->/** @scrutinizer ignore-call */ getMax() !== null && $number > $rule->getMax()) {
Loading history...
59 5
            $result->addError($rule->getGreaterThanMaxMessage(), [
0 ignored issues
show
Bug introduced by
The method getGreaterThanMaxMessage() does not exist on Yiisoft\Validator\RuleInterface. It seems like you code against a sub-type of Yiisoft\Validator\RuleInterface such as Yiisoft\Validator\Rule\HasLength or Yiisoft\Validator\Rule\Count. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
            $result->addError($rule->/** @scrutinizer ignore-call */ getGreaterThanMaxMessage(), [
Loading history...
60 5
                'max' => $rule->getMax(),
61 5
                'attribute' => $context->getAttribute(),
62
            ]);
63
        }
64
    }
65
}
66