Passed
Pull Request — master (#352)
by
unknown
02:45
created

LimitTrait::initLimitProperties()   D

Complexity

Conditions 18
Paths 6

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 18

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 39
ccs 19
cts 19
cp 1
rs 4.8666
cc 18
nc 6
nop 6
crap 18

How to fix   Complexity   

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:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\Trait;
6
7
use InvalidArgumentException;
8
use JetBrains\PhpStorm\ArrayShape;
9
10
/**
11
 * A trait attachable to a rule with limits.
12
 *
13
 * @see LimitHandlerTrait
14
 */
15
trait LimitTrait
16
{
17
    /**
18
     * @var int|null Lower limit.
19
     *
20
     * @see $lessThanMinMessage for according error message. Can't be combined with {@see $exactly}.
21
     */
22
    private ?int $min = null;
23
    /**
24
     * @var int|null Upper limit.
25
     *
26
     * @see $greaterThanMaxMessage for according error message. Can't be combined with {@see $exactly}.
27
     */
28
    private ?int $max = null;
29
    /**
30
     * @var int|null Exact number (lower and upper limit match).
31
     *
32
     * @see $notExactlyMessage for according error message. `null` means no strict comparison. Mutually exclusive with
33
     * {@see $min} and {@see $max}.
34
     */
35
    private ?int $exactly = null;
36
    /**
37
     * @var string Message used when validated value is lower than {@see $min}.
38
     */
39
    private string $lessThanMinMessage;
40
    /**
41
     * @var string Message used when validated value is greater than {@see $max}.
42
     */
43
    private string $greaterThanMaxMessage;
44
    /**
45
     * @var string Message used when validated value doesn't exactly match {@see $exactly}.
46
     */
47
    private string $notExactlyMessage;
48
49
    /**
50
     * Initializes limit related properties and runs checks for required and mutually exclusive properties.
51
     *
52
     * @param int|null $min {@see $min}
53
     * @param int|null $max {@see $max}
54
     * @param int|null $exactly {@see $exactly}
55
     * @param string $lessThanMinMessage {@see $lessThanMinMessage}
56
     * @param string $greaterThanMinMessage {@see $greaterThanMinMessage}
57
     * @param string $notExactlyMessage {@see $notExactlyMessage}
58
     */
59 35
    private function initLimitProperties(
60
        ?int $min,
61
        ?int $max,
62
        ?int $exactly,
63
        string $lessThanMinMessage,
64
        string $greaterThanMinMessage,
65
        string $notExactlyMessage
66
    ): void {
67 35
        $this->min = $min;
68 35
        $this->max = $max;
69 35
        $this->exactly = $exactly;
70 35
        $this->lessThanMinMessage = $lessThanMinMessage;
71 35
        $this->greaterThanMaxMessage = $greaterThanMinMessage;
72 35
        $this->notExactlyMessage = $notExactlyMessage;
73
74 35
        if ($this->min === null && $this->max === null && $this->exactly === null) {
75 2
            throw new InvalidArgumentException(
76
                'At least one of these attributes must be specified: $min, $max, $exactly.'
77
            );
78
        }
79
80 33
        if ($this->exactly !== null && ($this->min !== null || $this->max !== null)) {
81 6
            throw new InvalidArgumentException('$exactly is mutually exclusive with $min and $max.');
82
        }
83
84 27
        if ($this->min === $this->max && $this->min !== null) {
85 2
            throw new InvalidArgumentException('Use $exactly instead.');
86
        }
87
88
        if (
89 25
            ($this->min !== null && $this->min <= 0) ||
90 21
            ($this->max !== null && $this->max <= 0) ||
91 25
            ($this->exactly !== null && $this->exactly <= 0)
92
        ) {
93 8
            throw new InvalidArgumentException('Only positive values are allowed.');
94
        }
95
96 17
        if ($this->min !== null && $this->max !== null && $this->min > $this->max) {
97 1
            throw new InvalidArgumentException('$min must be lower than $max.');
98
        }
99
    }
100
101
    /**
102
     * Gets lower limit.
103
     *
104
     * @return int|null {@see $min}
105
     */
106 78
    public function getMin(): ?int
107
    {
108 78
        return $this->min;
109
    }
110
111
    /**
112
     * Gets upper limit.
113
     *
114
     * @return int|null {@see $max}
115
     */
116 78
    public function getMax(): ?int
117
    {
118 78
        return $this->max;
119
    }
120
121
    /**
122
     * Gets "exactly" value.
123
     *
124
     * @return int|null {@see $exactly}
125
     */
126 81
    public function getExactly(): ?int
127
    {
128 81
        return $this->exactly;
129
    }
130
131
    /**
132
     * Gets "less than lower limit" error message.
133
     *
134
     * @return string {@see $lessThanMinMessage}
135
     */
136 42
    public function getLessThanMinMessage(): string
137
    {
138 42
        return $this->lessThanMinMessage;
139
    }
140
141
    /**
142
     * Gets "greater than upper limit" error message.
143
     *
144
     * @return string {@see $greaterThanMaxMessage}
145
     */
146 6
    public function getGreaterThanMaxMessage(): string
147
    {
148 6
        return $this->greaterThanMaxMessage;
149
    }
150
151
    /**
152
     * Gets "does not match exactly" error message.
153
     *
154
     * @return string {@see notExactlyMessage}
155
     */
156 3
    public function getNotExactlyMessage(): string
157
    {
158 3
        return $this->notExactlyMessage;
159
    }
160
161
    /**
162
     * Limit related options intended to be merged with other rule options.
163
     */
164 5
    #[ArrayShape([
165
        'min' => 'int|null',
166
        'max' => 'int|null',
167
        'exactly' => 'int|null',
168
        'lessThanMinMessage' => 'array',
169
        'greaterThanMaxMessage' => 'array',
170
        'notExactlyMessage' => 'array',
171
    ])]
172
    private function getLimitOptions(): array
173
    {
174
        return [
175 5
            'min' => $this->min,
176 5
            'max' => $this->max,
177 5
            'exactly' => $this->exactly,
178
            'lessThanMinMessage' => [
179 5
                'message' => $this->lessThanMinMessage,
180 5
                'parameters' => ['min' => $this->min],
181
            ],
182
            'greaterThanMaxMessage' => [
183 5
                'message' => $this->greaterThanMaxMessage,
184 5
                'parameters' => ['max' => $this->max],
185
            ],
186
            'notExactlyMessage' => [
187 5
                'message' => $this->notExactlyMessage,
188 5
                'parameters' => ['exactly' => $this->exactly],
189
            ],
190
        ];
191
    }
192
}
193