Passed
Pull Request — master (#485)
by
unknown
02:44
created

Boolean::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 8
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0

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 Attribute;
8
use Closure;
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\RuleWithOptionsInterface;
13
use Yiisoft\Validator\SkipOnEmptyInterface;
14
use Yiisoft\Validator\SkipOnErrorInterface;
15
use Yiisoft\Validator\WhenInterface;
16
17
/**
18
 * Contains a set of options to determine if the value is "true" or "false", not limited to boolean type only. What
19
 * values exactly are considered "true" and "false" can be configured via {@see $trueValue} and {@see $falseValue}
20
 * settings accordingly. There is also an option to choose between strict and non-strict mode of comparison
21
 * (see {@see $strict}).
22
 *
23
 * If the purpose is to check the truthiness only, use {@see IsTrue} rule instead.
24
 *
25
 * @see BooleanHandler Corresponding handler performing the actual validation.
26
 *
27
 * @psalm-import-type WhenType from WhenInterface
28 11
 */
29
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
30
final class Boolean implements RuleWithOptionsInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface
31
{
32
    use SkipOnEmptyTrait;
33
    use SkipOnErrorTrait;
34
    use WhenTrait;
35
36
    /**
37
     * @const Default message used for all cases.
38
     */
39
    private const DEFAULT_MESSAGE = 'Value must be either "{true}" or "{false}".';
40
41
    /**
42
     * @param scalar $trueValue The value that is considered to be "true". Only scalar values (either int, float, string
43
     * or bool) are allowed. Defaults to `1` string.
44
     * @param scalar $falseValue The value that is considered to be "false". Only scalar values (either int, float,
45
     * string or bool) are allowed. Defaults to `0` string.
46
     * @param bool $strict Whether the comparison to {@see $trueValue} and {@see $falseValue} is strict:
47
     *
48
     * - Strict mode uses `===` operator meaning the type and the value must both match to those set in
49
     * {@see $trueValue} or {@see $falseValue}.
50
     * - Non-strict mode uses `==` operator meaning that type juggling is performed first before the comparison. You can
51
     * read more in the PHP docs:
52
     *
53
     * - {@link https://www.php.net/manual/en/language.operators.comparison.php}
54
     * - {@link https://www.php.net/manual/en/types.comparisons.php}
55
     * - {@link https://www.php.net/manual/en/language.types.type-juggling.php}
56
     *
57
     * Defaults to `false` meaning non-strict mode is used.
58 2
     * @param string $messageWithType Error message used when validation fails and neither non-scalar value (int,
59
     * float, string, bool) nor null was provided as input. The type is used instead of value here for more predictable
60 2
     * formatting.
61
     *
62
     * You may use the following placeholders in the message:
63 31
     *
64
     * - `{attribute}`: the label of the attribute being validated.
65 31
     * - `{true}`: the value set in {@see $trueValue} option.
66
     * - `{false}`: the value set in {@see $falseValue} option.
67
     * - `{type}`: the type of the value being validated.
68 21
     * @param string $messageWithValue Error message used when validation fails and either scalar value (int, float,
69
     * string, bool) or null was provided as input.
70 21
     *
71
     * You may use the following placeholders in the message:
72
     *
73 22
     * - `{attribute}`: the label of the attribute being validated.
74
     * - `{true}`: the value set in {@see $trueValue} option.
75 22
     * - `{false}`: the value set in {@see $falseValue} option.
76
     * - `{value}`: the value being validated.
77
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the validated value is empty / not passed.
78 9
     * See {@see SkipOnEmptyInterface}.
79
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See
80 9
     * {@see SkipOnErrorInterface}.
81
     * @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}.
82
     * @psalm-param WhenType $when
83 8
     */
84
    public function __construct(
85 8
        private int|float|string|bool $trueValue = '1',
86
        private int|float|string|bool $falseValue = '0',
87
        private bool $strict = false,
88 4
        private string $messageWithType = self::DEFAULT_MESSAGE,
89
        private string $messageWithValue = self::DEFAULT_MESSAGE,
90 4
        private mixed $skipOnEmpty = null,
91 4
        private bool $skipOnError = false,
92 4
        private Closure|null $when = null,
93
    ) {
94
    }
95
96 4
    public function getName(): string
97 4
    {
98 4
        return 'boolean';
99
    }
100 4
101
    /**
102
     * Gets the value that is considered to be "true".
103
     *
104 4
     * @return scalar A scalar value.
105
     *
106
     * @see $trueValue
107 4
     */
108 4
    public function getTrueValue(): int|float|string|bool
109
    {
110
        return $this->trueValue;
111
    }
112 31
113
    /**
114 31
     * Gets the value that is considered to be "false".
115
     *
116
     * @return scalar A scalar value.
117
     *
118
     * @see $falseValue
119
     */
120
    public function getFalseValue(): int|float|string|bool
121
    {
122
        return $this->falseValue;
123
    }
124
125
    /**
126
     * Whether the comparison to {@see $trueValue} and {@see $falseValue} is strict.
127
     *
128
     * @return bool `true` - strict, `false` - non-strict.
129
     *
130
     * @see $strict
131
     */
132
    public function isStrict(): bool
133
    {
134
        return $this->strict;
135
    }
136
137
    /**
138
     * Gets error message used when validation fails and value is complex to format, so its type is used instead.
139
     *
140
     * @return string Error message / template.
141
     *
142
     * @see $messageWithType
143
     */
144
    public function getMessageWithType(): string
145
    {
146
        return $this->messageWithType;
147
    }
148
149
    /**
150
     * Gets error message used when validation fails and value can be formatted.
151
     *
152
     * @return string Error message / template.
153
     *
154
     * @see $messageWithValue
155
     */
156
    public function getMessageWithValue(): string
157
    {
158
        return $this->messageWithValue;
159
    }
160
161
    public function getOptions(): array
162
    {
163
        $messageParameters = [
164
            'true' => $this->trueValue === true ? 'true' : $this->trueValue,
165
            'false' => $this->falseValue === false ? 'false' : $this->falseValue,
166
        ];
167
168
        return [
169
            'trueValue' => $this->trueValue,
170
            'falseValue' => $this->falseValue,
171
            'strict' => $this->strict,
172
            'messageWithType' => [
173
                'template' => $this->messageWithType,
174
                'parameters' => $messageParameters,
175
            ],
176
            'messageWithValue' => [
177
                'template' => $this->messageWithValue,
178
                'parameters' => $messageParameters,
179
            ],
180
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
181
            'skipOnError' => $this->skipOnError,
182
        ];
183
    }
184
185
    public function getHandler(): string
186
    {
187
        return BooleanHandler::class;
188
    }
189
}
190