Test Failed
Pull Request — master (#364)
by
unknown
03:02
created

Number::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 46
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 11
crap 1

How to fix   Many Parameters   

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 Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
10
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
11
use Yiisoft\Validator\Rule\Trait\WhenTrait;
12
use Yiisoft\Validator\SerializableRuleInterface;
13
use Yiisoft\Validator\SkipOnEmptyInterface;
14
use Yiisoft\Validator\SkipOnErrorInterface;
15
use Yiisoft\Validator\ValidationContext;
16
use Yiisoft\Validator\WhenInterface;
17
18
/**
19
 * Validates that the value is a number.
20
 *
21
 * The format of the number must match the regular expression specified in {@see Number::$integerPattern}
22
 * or {@see Number::$numberPattern}. Optionally, you may configure the {@see Number::min()} and {@see Number::max()}
23
 * to ensure the number is within certain range.
24
 */
25
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
26
final class Number implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
27
{
28
    use SkipOnEmptyTrait;
29
    use SkipOnErrorTrait;
30
    use WhenTrait;
31
32 33
    public function __construct(
33
        /**
34
         * @var bool whether the value can only be an integer. Defaults to false.
35
         */
36
        private bool $asInteger = false,
37
        /**
38
         * @var float|int lower limit of the number. Defaults to null, meaning no lower limit.
39
         *
40
         * @see tooSmallMessage for the customized message used when the number is too small.
41
         */
42
        private $min = null,
43
        /**
44
         * @var float|int upper limit of the number. Defaults to null, meaning no upper limit.
45
         *
46
         * @see tooBigMessage for the customized message used when the number is too big.
47
         */
48
        private $max = null,
49
        private string $incorrectInputMessage = 'The allowed types are integer, float and string.',
50
        /**
51
         * @var string user-defined error message used when the value is smaller than {@link $min}.
52
         */
53
        private string $tooSmallMessage = 'Value must be no less than {min}.',
54
        /**
55
         * @var string user-defined error message used when the value is bigger than {@link $max}.
56
         */
57
        private string $tooBigMessage = 'Value must be no greater than {max}.',
58
        /**
59
         * @var string the regular expression for matching integers.
60
         */
61
        private string $integerPattern = '/^\s*[+-]?\d+\s*$/',
62
        /**
63
         * @var string the regular expression for matching numbers. It defaults to a pattern
64
         * that matches floating numbers with optional exponential part (e.g. -1.23e-10).
65
         */
66
        private string $numberPattern = '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/',
67
68
        /**
69
         * @var bool|callable|null
70
         */
71
        private $skipOnEmpty = null,
72
        private bool $skipOnError = false,
73
        /**
74
         * @var Closure(mixed, ValidationContext):bool|null
75
         */
76
        private ?Closure $when = null,
77
    ) {
78
    }
79 7
80
    public function getName(): string
81 7
    {
82
        return 'number';
83
    }
84 99
85
    public function isAsInteger(): bool
86 99
    {
87
        return $this->asInteger;
88
    }
89 76
90
    public function getMin(): float|int|null
91 76
    {
92
        return $this->min;
93
    }
94 53
95
    public function getMax(): float|int|null
96 53
    {
97
        return $this->max;
98
    }
99 35
100
    public function getIncorrectInputMessage(): string
101 35
    {
102
        return $this->incorrectInputMessage;
103
    }
104 17
105
    public function getTooSmallMessage(): string
106 17
    {
107
        return $this->tooSmallMessage;
108
    }
109 38
110
    public function getTooBigMessage(): string
111 38
    {
112
        return $this->tooBigMessage;
113
    }
114 61
115
    public function getIntegerPattern(): string
116 61
    {
117
        return $this->integerPattern;
118
    }
119 48
120
    public function getNumberPattern(): string
121 48
    {
122
        return $this->numberPattern;
123
    }
124 11
125
    public function getNotANumberMessage(): string
126
    {
127 11
        return $this->asInteger ? 'Value must be an integer.' : 'Value must be a number.';
128 11
    }
129 11
130
    public function getOptions(): array
131 11
    {
132
        return [
133
            'asInteger' => $this->asInteger,
134 11
            'min' => $this->min,
135 11
            'max' => $this->max,
136
            'incorrectInputMessage' => [
137
                'message' => $this->incorrectInputMessage,
138 11
            ],
139 11
            'notANumberMessage' => [
140
                'message' => $this->getNotANumberMessage(),
141 11
            ],
142 11
            'tooSmallMessage' => [
143 11
                'message' => $this->tooSmallMessage,
144 11
                'parameters' => ['min' => $this->min],
145
            ],
146
            'tooBigMessage' => [
147
                'message' => $this->tooBigMessage,
148 113
                'parameters' => ['max' => $this->max],
149
            ],
150 113
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
151
            'skipOnError' => $this->skipOnError,
152
            'integerPattern' => $this->integerPattern,
153
            'numberPattern' => $this->numberPattern,
154
        ];
155
    }
156
157
    public function getHandlerClassName(): string
158
    {
159
        return NumberHandler::class;
160
    }
161
}
162