Passed
Push — master ( c9e253...fac319 )
by
unknown
09:09 queued 06:24
created

Number::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

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