Passed
Pull Request — master (#468)
by Sergei
03:08
created

HasLength::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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