Passed
Pull Request — master (#222)
by Rustam
02:32
created

NumberHandler::validate()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 40
ccs 27
cts 27
cp 1
rs 6.9666
c 0
b 0
f 0
cc 12
nc 10
nop 3
crap 12

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Strings\NumericHelper;
8
use Yiisoft\Validator\Exception\UnexpectedRuleException;
9
use Yiisoft\Validator\Formatter;
10
use Yiisoft\Validator\FormatterInterface;
11
use Yiisoft\Validator\Result;
12
use Yiisoft\Validator\ValidationContext;
13
14
/**
15
 * Validates that the value is a number.
16
 *
17
 * The format of the number must match the regular expression specified in {@see Number::$integerPattern}
18
 * or {@see Number::$numberPattern}. Optionally, you may configure the {@see Number::min()} and {@see Number::max()}
19
 * to ensure the number is within certain range.
20
 */
21
final class NumberHandler implements RuleHandlerInterface
22
{
23
    private FormatterInterface $formatter;
24
25 76
    public function __construct(?FormatterInterface $formatter = null)
26
    {
27 76
        $this->formatter = $formatter ?? new Formatter();
28
    }
29
30 76
    public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
31
    {
32 76
        if (!$rule instanceof Number) {
33 1
            throw new UnexpectedRuleException(Number::class, $rule);
34
        }
35
36 75
        $result = new Result();
37
38 75
        if (is_bool($value) || !is_scalar($value)) {
39 5
            $formattedMessage = $this->formatter->format(
40 5
                $rule->isAsInteger() ? 'Value must be an integer.' : 'Value must be a number.',
41 5
                ['attribute' => $context?->getAttribute(), 'value' => $value]
42
            );
43 5
            $result->addError($formattedMessage);
44 5
            return $result;
45
        }
46
47 70
        $pattern = $rule->isAsInteger() ? $rule->getIntegerPattern() : $rule->getNumberPattern();
48
49 70
        if (!preg_match($pattern, NumericHelper::normalize($value))) {
50 21
            $formattedMessage = $this->formatter->format(
51 21
                $rule->isAsInteger() ? 'Value must be an integer.' : 'Value must be a number.',
52 21
                ['attribute' => $context?->getAttribute(), 'value' => $value]
53
            );
54 21
            $result->addError($formattedMessage);
55 49
        } elseif ($rule->getMin() !== null && $value < $rule->getMin()) {
56 13
            $formattedMessage = $this->formatter->format(
57 13
                $rule->getTooSmallMessage(),
58 13
                ['min' => $rule->getMin(), 'attribute' => $context?->getAttribute(), 'value' => $value]
59
            );
60 13
            $result->addError($formattedMessage);
61 39
        } elseif ($rule->getMax() !== null && $value > $rule->getMax()) {
62 6
            $formattedMessage = $this->formatter->format(
63 6
                $rule->getTooBigMessage(),
64 6
                ['max' => $rule->getMax(), 'attribute' => $context?->getAttribute(), 'value' => $value]
65
            );
66 6
            $result->addError($formattedMessage);
67
        }
68
69 70
        return $result;
70
    }
71
}
72