Passed
Pull Request — master (#274)
by Alexander
05:12 queued 02:35
created

HasLength::__construct()   A

Complexity

Conditions 1
Paths 1

Size

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