Test Failed
Pull Request — master (#219)
by
unknown
02:42
created

HasLength::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 10
dl 0
loc 36
ccs 2
cts 2
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 Yiisoft\Validator\FormatterInterface;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\Rule;
11
use Yiisoft\Validator\ValidationContext;
12
13
use function is_string;
14
15
/**
16
 * Validates that the value is of certain length.
17
 *
18
 * Note, this rule should only be used with strings.
19
 */
20
#[Attribute(Attribute::TARGET_PROPERTY)]
21
final class HasLength extends Rule
22
{
23 12
    public function __construct(
24
        /**
25
         * @var int|null minimum length. null means no minimum length limit.
26
         *
27
         * @see $tooShortMessage for the customized message for a too short string.
28
         */
29
        private ?int $min = null,
30
        /**
31
         * @var int|null maximum length. null means no maximum length limit.
32
         *
33
         * @see $tooLongMessage for the customized message for a too long string.
34
         */
35
        private ?int $max = null,
36
        /**
37
         * @var string user-defined error message used when the value is not a string.
38
         */
39
        private string $message = 'This value must be a string.',
40
        /**
41
         * @var string user-defined error message used when the length of the value is smaller than {@see $min}.
42
         */
43
        private string $tooShortMessage = 'This value should contain at least {min, number} {min, plural, one{character} other{characters}}.',
44
        /**
45
         * @var string user-defined error message used when the length of the value is greater than {@see $max}.
46
         */
47
        private string $tooLongMessage = 'This value should contain at most {max, number} {max, plural, one{character} other{characters}}.',
48
        /**
49
         * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
50
         * If this property is not set, application wide encoding will be used.
51
         */
52
        protected string $encoding = 'UTF-8',
53
        ?FormatterInterface $formatter = null,
54
        bool $skipOnEmpty = false,
55
        bool $skipOnError = false,
56
        $when = null
57
    ) {
58 12
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
59
    }
60
61 37
    /**
62
     * @see $min
63 37
     */
64
    public function min(?int $value): self
65 37
    {
66 6
        $new = clone $this;
67 6
        $new->min = $value;
68
69
        return $new;
70 31
    }
71
72 31
    /**
73 8
     * @see $max
74
     */
75 31
    public function max(?int $value): self
76 5
    {
77
        $new = clone $this;
78
        $new->max = $value;
79 31
80
        return $new;
81
    }
82 6
83
    /**
84 6
     * @see $message
85 6
     */
86 6
    public function message(string $value): self
87 6
    {
88 6
        $new = clone $this;
89 6
        $new->message = $value;
90 6
91
        return $new;
92
    }
93
94
    /**
95
     * @see $tooShortMessage
96
     */
97
    public function tooShortMessage(string $value): self
98
    {
99
        $new = clone $this;
100
        $new->tooShortMessage = $value;
101
102
        return $new;
103
    }
104
105
    /**
106
     * @see $tooLongMessage
107
     */
108
    public function tooLongMessage(string $value): self
109
    {
110
        $new = clone $this;
111
        $new->tooLongMessage = $value;
112
113
        return $new;
114
    }
115
116
    /**
117
     * @see $encoding
118
     */
119
    public function encoding(string $value): self
120
    {
121
        $new = clone $this;
122
        $new->encoding = $value;
123
124
        return $new;
125
    }
126
127
    protected function validateValue($value, ?ValidationContext $context = null): Result
128
    {
129
        $result = new Result();
130
131
        if (!is_string($value)) {
132
            $result->addError($this->formatMessage($this->message));
133
            return $result;
134
        }
135
136
        $length = mb_strlen($value, $this->encoding);
137
138
        if ($this->min !== null && $length < $this->min) {
139
            $result->addError($this->formatMessage($this->tooShortMessage, ['min' => $this->min]));
140
        }
141
        if ($this->max !== null && $length > $this->max) {
142
            $result->addError($this->formatMessage($this->tooLongMessage, ['max' => $this->max]));
143
        }
144
145
        return $result;
146
    }
147
148
    public function getOptions(): array
149
    {
150
        return array_merge(parent::getOptions(), [
151
            'min' => $this->min,
152
            'max' => $this->max,
153
            'message' => $this->formatMessage($this->message),
154
            'tooShortMessage' => $this->formatMessage($this->tooShortMessage, ['min' => $this->min]),
155
            'tooLongMessage' => $this->formatMessage($this->tooLongMessage, ['max' => $this->max]),
156
            'encoding' => $this->encoding,
157
        ]);
158
    }
159
}
160