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