CountableLimitHandlerTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B validateCountableLimits() 0 38 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\Trait;
6
7
use InvalidArgumentException;
8
use Yiisoft\Validator\CountableLimitInterface;
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. Requires a rule to implement {@LimitInterface} / include
15
 * {@see CountableLimitTrait}.
16
 */
17
trait CountableLimitHandlerTrait
18
{
19
    /**
20
     * Runs countable limits specific validation.
21
     *
22
     * @param CountableLimitInterface|RuleInterface $rule A rule matching to this handler.
23
     * @param ValidationContext $context Validation context.
24
     * @param int $number A validated number to compare with set limits.
25
     * @param Result $result Result for adding errors.
26
     *
27
     * @see CountableLimitTrait for information about limits and messages.
28
     */
29
    private function validateCountableLimits(
30
        CountableLimitInterface|RuleInterface $rule,
31
        ValidationContext $context,
32
        int $number,
33
        Result $result
34
    ): void {
35
        if (!$rule instanceof CountableLimitInterface || !$rule instanceof RuleInterface) {
36
            throw new InvalidArgumentException('$rule must implement both LimitInterface and RuleInterface.');
37
        }
38
39
        /**
40
         * @var CountableLimitTrait|RuleInterface $rule
41
         *
42
         * @psalm-ignore-var
43
         */
44
        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\Count or Yiisoft\Validator\Rule\Length. ( Ignorable by Annotation )

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

44
        if ($rule->/** @scrutinizer ignore-call */ getExactly() !== null && $number !== $rule->getExactly()) {
Loading history...
45
            $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\Count or Yiisoft\Validator\Rule\Length. ( Ignorable by Annotation )

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

45
            $result->addError($rule->/** @scrutinizer ignore-call */ getNotExactlyMessage(), [
Loading history...
46
                'exactly' => $rule->getExactly(),
47
                'attribute' => $context->getTranslatedAttribute(),
48
                'number' => $number,
49
            ]);
50
51
            return;
52
        }
53
54
        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\Count or Yiisoft\Validator\Rule\Length or Yiisoft\Validator\Rule\AbstractNumber. ( Ignorable by Annotation )

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

54
        if ($rule->/** @scrutinizer ignore-call */ getMin() !== null && $number < $rule->getMin()) {
Loading history...
55
            $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\Count or Yiisoft\Validator\Rule\Length or Yiisoft\Validator\Rule\AbstractNumber. ( Ignorable by Annotation )

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

55
            $result->addError($rule->/** @scrutinizer ignore-call */ getLessThanMinMessage(), [
Loading history...
56
                'min' => $rule->getMin(),
57
                'attribute' => $context->getTranslatedAttribute(),
58
                'number' => $number,
59
            ]);
60
        }
61
62
        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\Count or Yiisoft\Validator\Rule\Length or Yiisoft\Validator\Rule\AbstractNumber. ( Ignorable by Annotation )

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

62
        if ($rule->/** @scrutinizer ignore-call */ getMax() !== null && $number > $rule->getMax()) {
Loading history...
63
            $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\Count or Yiisoft\Validator\Rule\Length or Yiisoft\Validator\Rule\AbstractNumber. ( Ignorable by Annotation )

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

63
            $result->addError($rule->/** @scrutinizer ignore-call */ getGreaterThanMaxMessage(), [
Loading history...
64
                'max' => $rule->getMax(),
65
                'attribute' => $context->getTranslatedAttribute(),
66
                'number' => $number,
67
            ]);
68
        }
69
    }
70
}
71