Passed
Pull Request — master (#274)
by
unknown
02:40
created

LimitTrait::initLimitProperties()   B

Complexity

Conditions 10
Paths 4

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 10

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 27
ccs 13
cts 13
cp 1
rs 7.6666
cc 10
nc 4
nop 6
crap 10

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
     * @var int|null Lower limit.
18
     * @see $lessThanMinMessage for according error message. Can't be combined with {@see $exactly}.
19
     */
20
    private ?int $min = null;
21
    /**
22
     * @var int|null Upper limit.
23
     * @see $greaterThanMaxMessage for according error message. Can't be combined with {@see $exactly}.
24
     */
25
    private ?int $max = null;
26
    /**
27
     * @var int|null Exact number (lower and upper limit match).
28
     * @see $notExactlyMessage for according error message. `null` means no strict comparison. Mutually exclusive with
29
     * {@see $min} and {@see $max}.
30
     */
31
    private ?int $exactly = null;
32
    /**
33
     * @var string Message used when validated value is lower than {@see $min}.
34
     */
35
    private string $lessThanMinMessage;
36
    /**
37
     * @var string Message used when validated value is greater than {@see $max}.
38
     */
39
    private string $greaterThanMaxMessage;
40
    /**
41
     * @var string Message used when validated value doesn't exactly match {@see $exactly}.
42
     */
43
    private string $notExactlyMessage;
44
45
    /**
46
     * Initializes limit related properties and runs checks for required and mutually exclusive properties.
47
     *
48
     * @param int|null $min {@see $min}
49
     * @param int|null $max {@see $max}
50
     * @param int|null $exactly {@see $exactly}
51
     * @param string $lessThanMinMessage {@see $lessThanMinMessage}
52
     * @param string $greaterThanMinMessage {@see $greaterThanMinMessage}
53
     * @param string $notExactlyMessage {@see $notExactlyMessage}
54
     * @return void
55
     */
56 12
    private function initLimitProperties(
57
        ?int $min,
58
        ?int $max,
59
        ?int $exactly,
60
        string $lessThanMinMessage,
61
        string $greaterThanMinMessage,
62
        string $notExactlyMessage
63
    ): void {
64 12
        $this->min = $min;
65 12
        $this->max = $max;
66 12
        $this->exactly = $exactly;
67 12
        $this->lessThanMinMessage = $lessThanMinMessage;
68 12
        $this->greaterThanMaxMessage = $greaterThanMinMessage;
69 12
        $this->notExactlyMessage = $notExactlyMessage;
70
71 12
        if (!$this->min && !$this->max && !$this->exactly) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->exactly of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $this->max of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $this->min of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
72 1
            throw new InvalidArgumentException(
73
                'At least one of these attributes must be specified: $min, $max, $exactly.'
74
            );
75
        }
76
77 11
        if ($this->exactly && ($this->min || $this->max)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->min of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $this->exactly of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $this->max of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
78 3
            throw new InvalidArgumentException('$exactly is mutually exclusive with $min and $max.');
79
        }
80
81 8
        if ($this->min && $this->max && $this->min === $this->max) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->min of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $this->max of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
82 1
            throw new InvalidArgumentException('Use $exactly instead.');
83
        }
84
    }
85
86
    /**
87
     * Gets lower limit.
88
     *
89
     * @return int|null {@see $min}
90
     */
91 40
    public function getMin(): ?int
92
    {
93 40
        return $this->min;
94
    }
95
96
    /**
97
     * Gets upper limit.
98
     *
99
     * @return int|null {@see $max}
100
     */
101 40
    public function getMax(): ?int
102
    {
103 40
        return $this->max;
104
    }
105
106
    /**
107
     * Gets "exactly" value.
108
     *
109
     * @return int|null {@see $exactly}
110
     */
111 43
    public function getExactly(): ?int
112
    {
113 43
        return $this->exactly;
114
    }
115
116
    /**
117
     * Gets "less than lower limit" error message.
118
     *
119
     * @return string {@see $lessThanMinMessage}
120
     */
121 13
    public function getLessThanMinMessage(): string
122
    {
123 13
        return $this->lessThanMinMessage;
124
    }
125
126
    /**
127
     * Gets "greater than upper limit" error message.
128
     *
129
     * @return string {@see $greaterThanMaxMessage}
130
     */
131 4
    public function getGreaterThanMaxMessage(): string
132
    {
133 4
        return $this->greaterThanMaxMessage;
134
    }
135
136
    /**
137
     * Gets "does not match exactly" error message.
138
     *
139
     * @return string {@see notExactlyMessage}
140
     */
141 3
    public function getNotExactlyMessage(): string
142
    {
143 3
        return $this->notExactlyMessage;
144
    }
145
146
    /**
147
     * Limit related options intended to be merged with other rule options.
148
     *
149
     * @return array
150
     */
151 4
    #[ArrayShape([
152
        'min' => 'int|null',
153
        'max' => 'int|null',
154
        'exactly' => 'int|null',
155
        'lessThanMinMessage' => 'array',
156
        'greaterThanMaxMessage' => 'array',
157
        'notExactlyMessage' => 'array',
158
    ])]
159
    private function getLimitOptions(): array
160
    {
161
        return [
162 4
            'min' => $this->min,
163 4
            'max' => $this->max,
164 4
            'exactly' => $this->exactly,
165
            'lessThanMinMessage' => [
166 4
                'message' => $this->lessThanMinMessage,
167 4
                'parameters' => ['min' => $this->min],
168
            ],
169
            'greaterThanMaxMessage' => [
170 4
                'message' => $this->greaterThanMaxMessage,
171 4
                'parameters' => ['max' => $this->max],
172
            ],
173
            'notExactlyMessage' => [
174 4
                'message' => $this->notExactlyMessage,
175 4
                'parameters' => ['exactly' => $this->exactly],
176
            ],
177
        ];
178
    }
179
}
180