Passed
Pull Request — master (#364)
by Alexander
05:26 queued 02:39
created

HasLength::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 59
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 11
crap 1

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\SerializableRuleInterface;
15
use Yiisoft\Validator\SkipOnEmptyInterface;
16
use Yiisoft\Validator\SkipOnErrorInterface;
17
use Yiisoft\Validator\ValidationContext;
18
use Yiisoft\Validator\WhenInterface;
19
20
/**
21
 * Validates that the value is of certain length.
22
 *
23
 * Note, this rule should only be used with strings.
24
 */
25
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
26
final class HasLength implements
27
    SerializableRuleInterface,
28
    SkipOnErrorInterface,
29
    WhenInterface,
30
    SkipOnEmptyInterface,
31
    LimitInterface
32
{
33
    use LimitTrait;
34
    use SkipOnEmptyTrait;
35
    use SkipOnErrorTrait;
36
    use WhenTrait;
37
38 32
    public function __construct(
39
        /**
40
         * @var int|null minimum length. null means no minimum length limit. Can't be combined with
41
         * {@see $exactly}.
42
         *
43
         * @see $lessThanMinMessage for the customized message for a too short string.
44
         */
45
        ?int $min = null,
46
        /**
47
         * @var int|null maximum length. null means no maximum length limit. Can't be combined with
48
         * {@see $exactly}.
49
         *
50
         * @see $greaterThanMaxMessage for the customized message for a too long string.
51
         */
52
        ?int $max = null,
53
        /**
54
         * @var int|null exact length. `null` means no strict comparison. Mutually exclusive with {@see $min} and
55
         * {@see $max}.
56
         */
57
        ?int $exactly = null,
58
        /**
59
         * @var string user-defined error message used when the value is not a string.
60
         */
61
        private string $incorrectInputMessage = 'This value must be a string.',
62
        /**
63
         * @var string user-defined error message used when the length of the value is smaller than {@see $min}.
64
         */
65
        string $lessThanMinMessage = 'This value must contain at least {min, number} {min, plural, one{character} ' .
66
        'other{characters}}.',
67
        /**
68
         * @var string user-defined error message used when the length of the value is greater than {@see $max}.
69
         */
70
        string $greaterThanMaxMessage = 'This value must contain at most {max, number} {max, plural, one{character} ' .
71
        'other{characters}}.',
72
        string $notExactlyMessage = 'This value must contain exactly {exactly, number} {exactly, plural, ' .
73
        'one{character} other{characters}}.',
74
        /**
75
         * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
76
         * If this property is not set, application wide encoding will be used.
77
         */
78
        private string $encoding = 'UTF-8',
79
80
        /**
81
         * @var bool|callable|null
82
         */
83
        private $skipOnEmpty = null,
84
        private bool $skipOnError = false,
85
        /**
86
         * @var Closure(mixed, ValidationContext):bool|null
87
         */
88
        private ?Closure $when = null
89
    ) {
90 32
        $this->initLimitProperties(
91
            $min,
92
            $max,
93
            $exactly,
94
            $lessThanMinMessage,
95
            $greaterThanMaxMessage,
96
            $notExactlyMessage
97
        );
98
    }
99
100 3
    public function getName(): string
101
    {
102 3
        return 'hasLength';
103
    }
104
105 5
    public function getIncorrectInputMessage(): string
106
    {
107 5
        return $this->incorrectInputMessage;
108
    }
109
110 60
    public function getEncoding(): string
111
    {
112 60
        return $this->encoding;
113
    }
114
115 5
    public function getOptions(): array
116
    {
117 5
        return array_merge($this->getLimitOptions(), [
118
            'incorrectInputMessage' => [
119 5
                'message' => $this->incorrectInputMessage,
120
            ],
121 5
            'encoding' => $this->encoding,
122 5
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
123 5
            'skipOnError' => $this->skipOnError,
124
        ]);
125
    }
126
127 65
    public function getHandlerClassName(): string
128
    {
129 65
        return HasLengthHandler::class;
130
    }
131
}
132