Passed
Pull Request — master (#222)
by Dmitriy
02:23
created

CompareToHandler::compareValues()   B

Complexity

Conditions 10
Paths 18

Size

Total Lines 28
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10.1953

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 28
ccs 21
cts 24
cp 0.875
rs 7.6666
c 0
b 0
f 0
cc 10
nc 18
nop 4
crap 10.1953

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\Validator\Result;
8
use Yiisoft\Validator\ValidationContext;
9
use Yiisoft\Validator\ValidatorInterface;
10
use Yiisoft\Validator\Exception\UnexpectedRuleException;
11
12
/**
13
 * Compares the specified value with another value.
14
 *
15
 * The value being compared with a constant {@see CompareTo::$compareValue}, which is set
16
 * in the constructor.
17
 *
18
 * It supports different comparison operators, specified
19
 * via the {@see CompareTo::$operator}.
20
 *
21
 * The default comparison function is based on string values, which means the values
22
 * are compared byte by byte. When comparing numbers, make sure to change {@see CompareTo::$type} to
23
 * {@see CompareTo::TYPE_NUMBER} to enable numeric comparison.
24
 */
25
final class CompareToHandler implements RuleHandlerInterface
26
{
27
    /**
28
     * Constant for specifying the comparison as string values.
29
     * No conversion will be done before comparison.
30
     *
31
     * @see $type
32
     */
33
    public const TYPE_STRING = 'string';
34
    /**
35
     * Constant for specifying the comparison as numeric values.
36
     * String values will be converted into numbers before comparison.
37
     *
38
     * @see $type
39
     */
40
    public const TYPE_NUMBER = 'number';
41
42 31
    public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
43
    {
44 31
        if (!$rule instanceof CompareTo) {
45 1
            throw new UnexpectedRuleException(CompareTo::class, $rule);
46
        }
47
48 30
        $result = new Result();
49
50 30
        if (!$this->compareValues($rule->operator, $rule->type, $value, $rule->compareValue)) {
51 15
            $result->addError($rule->getMessage(), ['value' => $rule->compareValue]);
52
        }
53
54 30
        return $result;
55
    }
56
57
    /**
58
     * Compares two values with the specified operator.
59
     *
60
     * @param string $operator the comparison operator
61
     * @param string $type the type of the values being compared
62
     * @param mixed $value the value being compared
63
     * @param mixed $compareValue another value being compared
64
     *
65
     * @return bool whether the comparison using the specified operator is true.
66
     */
67 30
    private function compareValues(string $operator, string $type, $value, $compareValue): bool
68
    {
69 30
        if ($type === self::TYPE_NUMBER) {
70
            $value = (float) $value;
71
            $compareValue = (float)$compareValue;
72
        } else {
73 30
            $value = (string) $value;
74 30
            $compareValue = (string) $compareValue;
75
        }
76 30
        switch ($operator) {
77 30
            case '==':
78 5
                return $value == $compareValue;
79 25
            case '===':
80 4
                return $value === $compareValue;
81 21
            case '!=':
82 5
                return $value != $compareValue;
83 16
            case '!==':
84 4
                return $value !== $compareValue;
85 12
            case '>':
86 3
                return $value > $compareValue;
87 9
            case '>=':
88 3
                return $value >= $compareValue;
89 6
            case '<':
90 3
                return $value < $compareValue;
91 3
            case '<=':
92 3
                return $value <= $compareValue;
93
            default:
94
                return false;
95
        }
96
    }
97
}
98