Passed
Pull Request — master (#300)
by Alexander
06:13 queued 03:06
created

Compare::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 50
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 50
ccs 2
cts 3
cp 0.6667
rs 10
cc 2
nc 2
nop 8
crap 2.1481

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Closure;
8
use InvalidArgumentException;
9
use RuntimeException;
10
use Yiisoft\Validator\BeforeValidationInterface;
11
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
12
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
13
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
14
use Yiisoft\Validator\SerializableRuleInterface;
15
use Yiisoft\Validator\SkipOnEmptyInterface;
16
use Yiisoft\Validator\ValidationContext;
17
18
abstract class Compare implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface
19
{
20
    use BeforeValidationTrait;
21
    use RuleNameTrait;
22
    use SkipOnEmptyTrait;
23
24
    /**
25
     * Constant for specifying the comparison as string values.
26
     * No conversion will be done before comparison.
27
     *
28
     * @see $type
29
     */
30
    public const TYPE_STRING = 'string';
31
    /**
32
     * Constant for specifying the comparison as numeric values.
33
     * String values will be converted into numbers before comparison.
34
     *
35
     * @see $type
36
     */
37
    public const TYPE_NUMBER = 'number';
38
39
    private array $validOperators = [
40
        '==' => 1,
41
        '===' => 1,
42
        '!=' => 1,
43
        '!==' => 1,
44
        '>' => 1,
45
        '>=' => 1,
46
        '<' => 1,
47
        '<=' => 1,
48
    ];
49
50 22
    public function __construct(
51
        /**
52
         * @var mixed The constant value to be compared with. When both this property
53
         * and {@see $targetAttribute} are set, this property takes precedence.
54
         */
55
        private $targetValue = null,
56
        /**
57
         * @var string|null The name of the attribute to be compared with. When both this property
58
         * and {@see $targetValue} are set, the {@see $targetValue} takes precedence.
59
         *
60
         * @see $targetValue
61
         */
62
        private ?string $targetAttribute = null,
63
        /**
64
         * @var string|null User-defined error message.
65
         */
66
        private ?string $message = null,
67
        /**
68
         * @var string The type of the values being compared.
69
         */
70
        private string $type = self::TYPE_STRING,
71
        /**
72
         * @var string The operator for comparison. The following operators are supported:
73
         *
74
         * - `==`: check if two values are equal. The comparison is done is non-strict mode.
75
         * - `===`: check if two values are equal. The comparison is done is strict mode.
76
         * - `!=`: check if two values are NOT equal. The comparison is done is non-strict mode.
77
         * - `!==`: check if two values are NOT equal. The comparison is done is strict mode.
78
         * - `>`: check if value being validated is greater than the value being compared with.
79
         * - `>=`: check if value being validated is greater than or equal to the value being compared with.
80
         * - `<`: check if value being validated is less than the value being compared with.
81
         * - `<=`: check if value being validated is less than or equal to the value being compared with.
82
         *
83
         * When you want to compare numbers, make sure to also change @see $type} to
84
         * {@see TYPE_NUMBER}.
85
         */
86
        private string $operator = '==',
87
88
        /**
89
         * @var bool|callable|null
90
         */
91
        private $skipOnEmpty = null,
92
        private bool $skipOnError = false,
93
        /**
94
         * @var Closure(mixed, ValidationContext):bool|null
95
         */
96
        private ?Closure $when = null,
97
    ) {
98 22
        if (!isset($this->validOperators[$operator])) {
99
            throw new InvalidArgumentException("Operator \"$operator\" is not supported.");
100
        }
101
    }
102
103 68
    public function getTargetValue(): mixed
104
    {
105 68
        return $this->targetValue;
106
    }
107
108 68
    public function getTargetAttribute(): ?string
109
    {
110 68
        return $this->targetAttribute;
111
    }
112
113 68
    public function getType(): string
114
    {
115 68
        return $this->type;
116
    }
117
118 68
    public function getOperator(): string
119
    {
120 68
        return $this->operator;
121
    }
122
123 83
    public function getMessage(): string
124
    {
125 83
        return $this->message ?? match ($this->operator) {
126 20
            '==', '===' => 'Value must be equal to "{targetValueOrAttribute}".',
127 16
            '!=', '!==' => 'Value must not be equal to "{targetValueOrAttribute}".',
128 8
            '>' => 'Value must be greater than "{targetValueOrAttribute}".',
129 8
            '>=' => 'Value must be greater than or equal to "{targetValueOrAttribute}".',
130 8
            '<' => 'Value must be less than "{targetValueOrAttribute}".',
131 8
            '<=' => 'Value must be less than or equal to "{targetValueOrAttribute}".',
132 83
            default => throw new RuntimeException("Unknown operator: $this->operator."),
133
        };
134
    }
135
136 45
    public function getOptions(): array
137
    {
138
        return [
139 45
            'targetValue' => $this->targetValue,
140 45
            'targetAttribute' => $this->targetAttribute,
141
            'message' => [
142 45
                'message' => $this->getMessage(),
143
                'parameters' => [
144 45
                    'targetValue' => $this->targetValue,
145 45
                    'targetAttribute' => $this->targetAttribute,
146 45
                    'targetValueOrAttribute' => $this->targetValue ?? $this->targetAttribute,
147
                ],
148
            ],
149 45
            'type' => $this->type,
150 45
            'operator' => $this->operator,
151 45
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
152 45
            'skipOnError' => $this->skipOnError,
153
        ];
154
    }
155
156 9
    public function getHandlerClassName(): string
157
    {
158 9
        return CompareHandler::class;
159
    }
160
}
161