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

LimitTrait::getLessThanMinMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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
trait LimitTrait
11
{
12
    private ?int $min = null;
13
    private ?int $max = null;
14
    private ?int $exactly = null;
15
    private string $lessThanMinMessage;
16
    private string $greaterThanMaxMessage;
17
    private string $notExactlyMessage;
18
19 12
    private function initLimitProperties(
20
        ?int $min,
21
        ?int $max,
22
        ?int $exactly,
23
        string $lessThanMinMessage,
24
        string $greaterThanMinMessage,
25
        string $notExactlyMessage
26
    ): void {
27 12
        $this->min = $min;
28 12
        $this->max = $max;
29 12
        $this->exactly = $exactly;
30 12
        $this->lessThanMinMessage = $lessThanMinMessage;
31 12
        $this->greaterThanMaxMessage = $greaterThanMinMessage;
32 12
        $this->notExactlyMessage = $notExactlyMessage;
33
34 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->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...
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...
35 1
            throw new InvalidArgumentException(
36
                'At least one of these attributes must be specified: $min, $max, $exactly.'
37
            );
38
        }
39
40 11
        if ($this->exactly && ($this->min || $this->max)) {
0 ignored issues
show
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...
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...
41 3
            throw new InvalidArgumentException('$exactly is mutually exclusive with $min and $max.');
42
        }
43
44 8
        if ($this->min && $this->max && $this->min === $this->max) {
0 ignored issues
show
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...
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...
45 1
            throw new InvalidArgumentException('Use $exactly instead.');
46
        }
47
    }
48
49
    /**
50
     * @return int|null
51
     */
52 40
    public function getMin(): ?int
53
    {
54 40
        return $this->min;
55
    }
56
57
    /**
58
     * @return int|null
59
     */
60 40
    public function getMax(): ?int
61
    {
62 40
        return $this->max;
63
    }
64
65
    /**
66
     * @return int|null
67
     */
68 43
    public function getExactly(): ?int
69
    {
70 43
        return $this->exactly;
71
    }
72
73
    /**
74
     * @return string
75
     */
76 13
    public function getLessThanMinMessage(): string
77
    {
78 13
        return $this->lessThanMinMessage;
79
    }
80
81
    /**
82
     * @return string
83
     */
84 4
    public function getGreaterThanMaxMessage(): string
85
    {
86 4
        return $this->greaterThanMaxMessage;
87
    }
88
89
    /**
90
     * @return string
91
     */
92 3
    public function getNotExactlyMessage(): string
93
    {
94 3
        return $this->notExactlyMessage;
95
    }
96
97 4
    #[ArrayShape([
98
        'min' => 'int|null',
99
        'max' => 'int|null',
100
        'exactly' => 'int|null',
101
        'lessThanMinMessage' => 'array',
102
        'greaterThanMaxMessage' => 'array',
103
        'notExactlyMessage' => 'array',
104
    ])]
105
    private function getLimitOptions(): array
106
    {
107
        return [
108 4
            'min' => $this->min,
109 4
            'max' => $this->max,
110 4
            'exactly' => $this->exactly,
111
            'lessThanMinMessage' => [
112 4
                'message' => $this->lessThanMinMessage,
113 4
                'parameters' => ['min' => $this->min],
114
            ],
115
            'greaterThanMaxMessage' => [
116 4
                'message' => $this->greaterThanMaxMessage,
117 4
                'parameters' => ['max' => $this->max],
118
            ],
119
            'notExactlyMessage' => [
120 4
                'message' => $this->notExactlyMessage,
121 4
                'parameters' => ['exactly' => $this->exactly],
122
            ],
123
        ];
124
    }
125
}
126