Passed
Pull Request — master (#274)
by
unknown
03:13 queued 22s
created

HasLength::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
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 53
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.
32
         *
33
         * @see $lessThanMinMessage for the customized message for a too short string.
34
         */
35
        ?int $min = null,
36
        /**
37
         * @var int|null maximum length. null means no maximum length limit.
38
         *
39
         * @see $greaterThanMaxMessage for the customized message for a too long string.
40
         */
41
        ?int $max = null,
42
        /**
43
         * @var int|null exact length. null means no strict comparison. Mutually exclusive with {@see $min} and
44
         * {@see $max}.
45
         */
46
        ?int $exactly = null,
47
        /**
48
         * @var string user-defined error message used when the value is not a string.
49
         */
50
        private string $message = 'This value must be a string.',
51
        /**
52
         * @var string user-defined error message used when the length of the value is smaller than {@see $min}.
53
         */
54
        string $lessThanMinMessage = 'This value must contain at least {min, number} {min, plural, one{character} ' .
55
        'other{characters}}.',
56
        /**
57
         * @var string user-defined error message used when the length of the value is greater than {@see $max}.
58
         */
59
        string $greaterThanMaxMessage = 'This value must contain at most {max, number} {max, plural, one{character} ' .
60
        'other{characters}}.',
61
        string $notExactlyMessage = 'This value must contain exactly {exactly, number} {exactly, plural, ' .
62
        'one{character} other{characters}}.',
63
        /**
64
         * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
65
         * If this property is not set, application wide encoding will be used.
66
         */
67
        private string $encoding = 'UTF-8',
68
        private bool $skipOnEmpty = false,
69
        private bool $skipOnError = false,
70
        /**
71
         * @var Closure(mixed, ValidationContext):bool|null
72
         */
73
        private ?Closure $when = null
74
    ) {
75 5
        $this->initLimitProperties(
76
            $min,
77
            $max,
78
            $exactly,
79
            $lessThanMinMessage,
80
            $greaterThanMaxMessage,
81
            $notExactlyMessage
82
        );
83
    }
84
85
    /**
86
     * @return string
87
     */
88 5
    public function getMessage(): string
89
    {
90 5
        return $this->message;
91
    }
92
93
    /**
94
     * @return string
95
     */
96 24
    public function getEncoding(): string
97
    {
98 24
        return $this->encoding;
99
    }
100
101 3
    #[ArrayShape([
102
        'min' => 'int|null',
103
        'max' => 'int|null',
104
        'message' => 'string[]',
105
        'lessThanMinMessage' => 'array',
106
        'greaterThanMaxMessage' => 'array',
107
        'notExactlyMessage' => 'array',
108
        'encoding' => 'string',
109
        'skipOnEmpty' => 'bool',
110
        'skipOnError' => 'bool',
111
    ])]
112
    public function getOptions(): array
113
    {
114 3
        return array_merge($this->getLimitOptions(), [
115
            'message' => [
116 3
                'message' => $this->message,
117
            ],
118 3
            'encoding' => $this->encoding,
119 3
            'skipOnEmpty' => $this->skipOnEmpty,
120 3
            'skipOnError' => $this->skipOnError,
121
        ]);
122
    }
123
124 3
    public function getHandlerClassName(): string
125
    {
126 3
        return HasLengthHandler::class;
127
    }
128
}
129