Test Failed
Pull Request — master (#175)
by
unknown
12:57
created

HasLength::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 10
dl 0
loc 37
ccs 2
cts 2
cp 1
crap 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 Yiisoft\Validator\FormatterInterface;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\Rule;
10
use Yiisoft\Validator\ValidationContext;
11
12
use function is_string;
13
14
/**
15
 * StringValidator validates that a value is of certain length.
16
 *
17
 * Note, this validator should only be used with strings.
18
 */
19
final class HasLength extends Rule
20
{
21
    public function __construct(
22
        /**
23
         * @var int|null minimum length. null means no minimum length limit.
24
         *
25
         * @see $tooShortMessage for the customized message for a too short string.
26
         */
27
        private ?int $min = null,
28
        /**
29
         * @var int|null maximum length. null means no maximum length limit.
30
         *
31
         * @see $tooLongMessage for the customized message for a too long string.
32
         */
33
        private ?int $max = null,
34
        /**
35
         * @var string user-defined error message used when the value is not a string.
36
         */
37
        private string $message = 'This value must be a string.',
38
        /**
39
         * @var string user-defined error message used when the length of the value is smaller than {@see $min}.
40
         */
41
        private string $tooShortMessage = 'This value should contain at least {min, number} {min, plural, one{character} other{characters}}.',
42
        /**
43
         * @var string user-defined error message used when the length of the value is greater than {@see $max}.
44
         */
45
        private string $tooLongMessage = 'This value should contain at most {max, number} {max, plural, one{character} other{characters}}.',
46
        /**
47
         * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
48
         * If this property is not set, application wide encoding will be used.
49
         */
50
        protected string $encoding = 'UTF-8',
51
52
        ?FormatterInterface $formatter = null,
53 7
        bool $skipOnEmpty = false,
54
        bool $skipOnError = false,
55 7
        $when = null
56
    ) {
57
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
58 10
    }
59
60 10
    protected function validateValue($value, ?ValidationContext $context = null): Result
61
    {
62 10
        $result = new Result();
63 3
64 3
        if (!is_string($value)) {
65
            $result->addError($this->formatMessage($this->message));
66
            return $result;
67 10
        }
68
69 10
        $length = mb_strlen($value, $this->encoding);
70 4
71
        if ($this->min !== null && $length < $this->min) {
72 10
            $result->addError($this->formatMessage($this->tooShortMessage, ['min' => $this->min]));
73 4
        }
74
        if ($this->max !== null && $length > $this->max) {
75
            $result->addError($this->formatMessage($this->tooLongMessage, ['max' => $this->max]));
76 10
        }
77
78
        return $result;
79 4
    }
80
81 4
    public function getOptions(): array
82 4
    {
83 4
        return array_merge(parent::getOptions(), [
84
            'min' => $this->min,
85
            'max' => $this->max,
86 8
            'message' => $this->formatMessage($this->message),
87
            'tooShortMessage' => $this->formatMessage($this->tooShortMessage, ['min' => $this->min]),
88 8
            'tooLongMessage' => $this->formatMessage($this->tooLongMessage, ['max' => $this->max]),
89 8
            'encoding' => $this->encoding,
90 8
        ]);
91
    }
92
}
93