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