Passed
Pull Request — master (#519)
by
unknown
02:43
created

Length::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
nc 1
nop 11
dl 0
loc 23
cc 1
rs 10

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\LimitInterface;
10
use Yiisoft\Validator\Rule\Trait\LimitTrait;
11
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
12
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
13
use Yiisoft\Validator\Rule\Trait\WhenTrait;
14
use Yiisoft\Validator\RuleWithOptionsInterface;
15
use Yiisoft\Validator\SkipOnEmptyInterface;
16
use Yiisoft\Validator\SkipOnErrorInterface;
17
use Yiisoft\Validator\WhenInterface;
18
19
/**
20
 * Defines validation options to check that the value is a string of a certain length.
21
 *
22
 * @see LengthHandler
23
 *
24
 * @psalm-import-type WhenType from WhenInterface
25
 */
26
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
27
final class Length implements
28
    RuleWithOptionsInterface,
29
    SkipOnErrorInterface,
30
    WhenInterface,
31
    SkipOnEmptyInterface,
32
    LimitInterface
33
{
34
    use LimitTrait;
35
    use SkipOnEmptyTrait;
36
    use SkipOnErrorTrait;
37
    use WhenTrait;
38
39
    /**
40
     * @param int|null $min Minimum length. `null` means no minimum length limit. Can't be combined with
41
     * {@see $exactly}. See {@see $lessThanMinMessage} for the customized message for a too short string.
42
     * @param int|null $max maximum length. `null` means no maximum length limit. Can't be combined with
43
     * {@see $exactly}. See {@see $greaterThanMaxMessage} for the customized message for a too long string.
44
     * @param int|null $exactly Exact length. `null` means no strict comparison. Mutually exclusive with
45
     * {@see $min} and {@see $max}.
46
     * @param string $incorrectInputMessage Error message used when the value is not a string.
47
     *
48
     * You may use the following placeholders in the message:
49
     *
50
     * - `{attribute}`: the translated label of the attribute being validated.
51
     * - `{type}`: the type of the value being validated.
52
     * @param string $lessThanMinMessage Error message used when the length of the value is smaller than {@see $min}.
53
     *
54
     * You may use the following placeholders in the message:
55
     *
56
     * - `{attribute}`: the translated label of the attribute being validated.
57
     * - `{min}`: minimum number of items required.
58
     * - `{number}`: actual number of items.
59
     * @param string $greaterThanMaxMessage Error message used when the length of the value is greater than {@see $max}.
60
     *
61
     * You may use the following placeholders in the message:
62
     *
63
     * - `{attribute}`: the translated label of the attribute being validated.
64
     * - `{max}`: maximum number of items required.
65
     * - `{number}`: actual number of items.
66
     * @param string $notExactlyMessage Error message used when the number of items does not equal {@see $exactly}.
67
     *
68
     * You may use the following placeholders in the message:
69
     *
70
     * - `{attribute}`: the translated label of the attribute being validated.
71
     * - `{exactly}`: exact number of items required.
72
     * - `{number}`: actual number of items.
73
     * @param string $encoding The encoding of the string value to be validated (e.g. 'UTF-8').
74
     * If this property is not set, application wide encoding will be used.
75
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty.
76
     * See {@see SkipOnEmptyInterface}.
77
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error.
78
     * See {@see SkipOnErrorInterface}.
79
     * @param Closure|null $when A callable to define a condition for applying the rule.
80
     * See {@see WhenInterface}.
81
     * @psalm-param WhenType $when
82
     */
83
    public function __construct(
84
        int|null $min = null,
85
        int|null $max = null,
86
        int|null $exactly = null,
87
        private string $incorrectInputMessage = 'This value must be a string.',
88
        string $lessThanMinMessage = 'This value must contain at least {min, number} {min, plural, one{character} ' .
89
        'other{characters}}.',
90
        string $greaterThanMaxMessage = 'This value must contain at most {max, number} {max, plural, one{character} ' .
91
        'other{characters}}.',
92
        string $notExactlyMessage = 'This value must contain exactly {exactly, number} {exactly, plural, ' .
93
        'one{character} other{characters}}.',
94
        private string $encoding = 'UTF-8',
95
        private mixed $skipOnEmpty = null,
96
        private bool $skipOnError = false,
97
        private Closure|null $when = null
98
    ) {
99
        $this->initLimitProperties(
100
            $min,
101
            $max,
102
            $exactly,
103
            $lessThanMinMessage,
104
            $greaterThanMaxMessage,
105
            $notExactlyMessage
106
        );
107
    }
108
109
    public function getName(): string
110
    {
111
        return 'length';
112
    }
113
114
    /**
115
     * Get error message used when the value is neither an array nor implementing {@see \Countable} interface.
116
     *
117
     * @return string Error message.
118
     *
119
     * @see $incorrectInputMessage
120
     */
121
    public function getIncorrectInputMessage(): string
122
    {
123
        return $this->incorrectInputMessage;
124
    }
125
126
    /**
127
     * Get the encoding of the string value to be validated (e.g. 'UTF-8').
128
     * If this property is not set, application wide encoding will be used.
129
     *
130
     * @return string Encoding of the string value to be validated.
131
     *
132
     * @see $encoding
133
     */
134
    public function getEncoding(): string
135
    {
136
        return $this->encoding;
137
    }
138
139
    public function getOptions(): array
140
    {
141
        return array_merge($this->getLimitOptions(), [
142
            'incorrectInputMessage' => [
143
                'template' => $this->incorrectInputMessage,
144
                'parameters' => [],
145
            ],
146
            'encoding' => $this->encoding,
147
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
148
            'skipOnError' => $this->skipOnError,
149
        ]);
150
    }
151
152
    public function getHandler(): string
153
    {
154
        return LengthHandler::class;
155
    }
156
}
157