Conditions | 1 |
Paths | 1 |
Total Lines | 68 |
Code Lines | 9 |
Lines | 0 |
Ratio | 0 % |
Tests | 12 |
CRAP Score | 1 |
Changes | 0 |
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:
If many parameters/temporary variables are present:
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 |
||
41 | public function __construct( |
||
42 | /** |
||
43 | * @var bool whether the value can only be an integer. Defaults to `false`. |
||
44 | */ |
||
45 | private bool $asInteger = false, |
||
46 | /** |
||
47 | * @var float|int|null lower limit of the number. Defaults to `null`, meaning no lower limit. Can't be combined |
||
48 | * with {@see $exactly}. |
||
49 | * |
||
50 | * @see $lessThanMin for the message used when the number is too small. |
||
51 | */ |
||
52 | int|float|null $min = null, |
||
53 | /** |
||
54 | * @var float|int|null upper limit of the number. Defaults to `null`, meaning no upper limit. Can't be combined |
||
55 | * with {@see $exactly}. |
||
56 | * |
||
57 | * @see $greaterThanMax for the message used when the number is too big. |
||
58 | */ |
||
59 | int|float|null $max = null, |
||
60 | /** |
||
61 | * @var float|int|null exact number. `null` means no strict comparison. Mutually exclusive with {@see $min} |
||
62 | * and {@see $max}. |
||
63 | * |
||
64 | * @see $notExactlyMessage for the message used when the number does not equal to the set one. |
||
65 | */ |
||
66 | int|float|null $exactly = null, |
||
67 | private string $incorrectInputMessage = 'The allowed types are integer, float and string.', |
||
68 | /** |
||
69 | * @var string error message used when the value is smaller than {@link $min}. |
||
70 | */ |
||
71 | private string $lessThanMinMessage = 'Value must be no less than {min}.', |
||
72 | /** |
||
73 | * @var string error message used when the value is bigger than {@link $max}. |
||
74 | */ |
||
75 | private string $greaterThanMaxMessage = 'Value must be no greater than {max}.', |
||
76 | /** |
||
77 | * @var string error message used when the value does not equal {@see $exactly}. |
||
78 | */ |
||
79 | private string $notExactlyMessage = 'Value must be equal to {exactly}.', |
||
80 | 10 | /** |
|
81 | * @var string the regular expression for matching integers. |
||
82 | 10 | */ |
|
83 | private string $integerPattern = '/^\s*[+-]?\d+\s*$/', |
||
84 | /** |
||
85 | 106 | * @var string the regular expression for matching numbers. It defaults to a pattern |
|
86 | * that matches floating numbers with optional exponential part (e.g. -1.23e-10). |
||
87 | 106 | */ |
|
88 | private string $numberPattern = '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/', |
||
89 | |||
|
|||
90 | 83 | /** |
|
91 | * @var bool|callable|null |
||
92 | 83 | */ |
|
93 | private $skipOnEmpty = null, |
||
94 | private bool $skipOnError = false, |
||
95 | 57 | /** |
|
96 | * @var WhenType |
||
97 | 57 | */ |
|
98 | private Closure|null $when = null, |
||
99 | ) { |
||
100 | 14 | $this->initLimitProperties( |
|
101 | $min, |
||
102 | 14 | $max, |
|
103 | $exactly, |
||
104 | $lessThanMinMessage, |
||
105 | 39 | $greaterThanMaxMessage, |
|
106 | $notExactlyMessage, |
||
107 | 39 | requireLimits: false, |
|
108 | allowNegativeLimits: true, |
||
109 | ); |
||
166 |