Completed
Pull Request — master (#175)
by Alexander
02:53 queued 02:53
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 10
    protected function validateValue($value, ?ValidationContext $context = null): Result
62
    {
63 10
        $result = new Result();
64
65 10
        if (!is_string($value)) {
66 3
            $result->addError($this->formatMessage($this->message));
67 3
            return $result;
68
        }
69
70 10
        $length = mb_strlen($value, $this->encoding);
71
72 10
        if ($this->min !== null && $length < $this->min) {
73 4
            $result->addError($this->formatMessage($this->tooShortMessage, ['min' => $this->min]));
74
        }
75 10
        if ($this->max !== null && $length > $this->max) {
76 4
            $result->addError($this->formatMessage($this->tooLongMessage, ['max' => $this->max]));
77
        }
78
79 10
        return $result;
80
    }
81
82 6
    public function getOptions(): array
83
    {
84 6
        return array_merge(parent::getOptions(), [
85 6
            'min' => $this->min,
86 6
            'max' => $this->max,
87 6
            'message' => $this->formatMessage($this->message),
88 6
            'tooShortMessage' => $this->formatMessage($this->tooShortMessage, ['min' => $this->min]),
89 6
            'tooLongMessage' => $this->formatMessage($this->tooLongMessage, ['max' => $this->max]),
90 6
            'encoding' => $this->encoding,
91
        ]);
92
    }
93
}
94