Test Failed
Pull Request — master (#222)
by Rustam
14:22 queued 33s
created

CompareToHandler::compareValues()   B

Complexity

Conditions 10
Paths 18

Size

Total Lines 28
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 28
rs 7.6666
c 0
b 0
f 0
cc 10
nc 18
nop 4

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
    public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
42
    {
43
        if (!$rule instanceof CompareTo) {
44
            throw new UnexpectedRuleException(CompareTo::class, $rule);
45
        }
46
47
        $result = new Result();
48
49
        if (!$this->compareValues($rule->operator, $rule->type, $value, $rule->compareValue)) {
50
            $result->addError($rule->getMessage(), ['value' => $rule->compareValue]);
51
        }
52
53
        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
    private function compareValues(string $operator, string $type, $value, $compareValue): bool
67
    {
68
        if ($type === self::TYPE_NUMBER) {
69
            $value = (float) $value;
70
            $compareValue = (float)$compareValue;
71
        } else {
72
            $value = (string) $value;
73
            $compareValue = (string) $compareValue;
74
        }
75
        switch ($operator) {
76
            case '==':
77
                return $value == $compareValue;
78
            case '===':
79
                return $value === $compareValue;
80
            case '!=':
81
                return $value != $compareValue;
82
            case '!==':
83
                return $value !== $compareValue;
84
            case '>':
85
                return $value > $compareValue;
86
            case '>=':
87
                return $value >= $compareValue;
88
            case '<':
89
                return $value < $compareValue;
90
            case '<=':
91
                return $value <= $compareValue;
92
            default:
93
                return false;
94
        }
95
    }
96
}
97