Passed
Pull Request — master (#222)
by Alexander
04:51 queued 02:32
created

CompareToValidator::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 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 25
c 1
b 0
f 0
nc 18
nop 4
dl 0
loc 28
ccs 21
cts 24
cp 0.875
crap 10.1953
rs 7.6666

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