Passed
Pull Request — master (#222)
by Alexander
04:47 queued 02:23
created

Number::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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

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\Number;
6
7
use Attribute;
8
use Closure;
9
use Yiisoft\Validator\Rule\RuleNameTrait;
10
use Yiisoft\Validator\Rule\ValidatorClassNameTrait;
11
use Yiisoft\Validator\RuleInterface;
12
13
/**
14
 * Validates that the value is a number.
15
 *
16
 * The format of the number must match the regular expression specified in {@see Number::$integerPattern}
17
 * or {@see Number::$numberPattern}. Optionally, you may configure the {@see Number::min()} and {@see Number::max()}
18
 * to ensure the number is within certain range.
19
 */
20
#[Attribute(Attribute::TARGET_PROPERTY)]
21
final class Number implements RuleInterface
22
{
23
    use RuleNameTrait;
24
    use ValidatorClassNameTrait;
25
26 2
    public function __construct(
27
        /**
28
         * @var bool whether the value can only be an integer. Defaults to false.
29
         */
30
        public bool $asInteger = false,
31
        /**
32
         * @var float|int lower limit of the number. Defaults to null, meaning no lower limit.
33
         *
34
         * @see tooSmallMessage for the customized message used when the number is too small.
35
         */
36
        public           $min = null,
37
        /**
38
         * @var float|int upper limit of the number. Defaults to null, meaning no upper limit.
39
         *
40
         * @see tooBigMessage for the customized message used when the number is too big.
41
         */
42
        public           $max = null,
43
        /**
44
         * @var string user-defined error message used when the value is smaller than {@link $min}.
45
         */
46
        public string $tooSmallMessage = 'Value must be no less than {min}.',
47
        /**
48
         * @var string user-defined error message used when the value is bigger than {@link $max}.
49
         */
50
        public string $tooBigMessage = 'Value must be no greater than {max}.',
51
        /**
52
         * @var string the regular expression for matching integers.
53
         */
54
        public string $integerPattern = '/^\s*[+-]?\d+\s*$/',
55
        /**
56
         * @var string the regular expression for matching numbers. It defaults to a pattern
57
         * that matches floating numbers with optional exponential part (e.g. -1.23e-10).
58
         */
59
        public string $numberPattern = '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/',
60
        public bool $skipOnEmpty = false,
61
        public bool $skipOnError = false,
62
        public ?Closure $when = null,
63
    ) {
64
    }
65
66 9
    public function getNotANumberMessage(): string
67
    {
68 9
        return $this->asInteger ? 'Value must be an integer.' : 'Value must be a number.';
69
    }
70
71 9
    public function getOptions(): array
72
    {
73
        return [
74 9
            'asInteger' => $this->asInteger,
75 9
            'min' => $this->min,
76 9
            'max' => $this->max,
77
            'notANumberMessage' => [
78 9
                'message' => $this->getNotANumberMessage(),
79
            ],
80
            'tooSmallMessage' => [
81 9
                'message' => $this->tooSmallMessage,
82 9
                'parameters' => ['min' => $this->min],
83
            ],
84
            'tooBigMessage' => [
85 9
                'message' => $this->tooBigMessage,
86 9
                'parameters' => ['max' => $this->max],
87
            ],
88 9
            'skipOnEmpty' => $this->skipOnEmpty,
89 9
            'skipOnError' => $this->skipOnError,
90 9
            'integerPattern' => $this->integerPattern,
91 9
            'numberPattern' => $this->numberPattern,
92
        ];
93
    }
94
}
95