Conditions | 1 |
Paths | 1 |
Total Lines | 59 |
Code Lines | 7 |
Lines | 0 |
Ratio | 0 % |
Tests | 2 |
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 |
||
38 | 32 | public function __construct( |
|
39 | /** |
||
40 | * @var int|null minimum length. null means no minimum length limit. Can't be combined with |
||
41 | * {@see $exactly}. |
||
42 | * |
||
43 | * @see $lessThanMinMessage for the customized message for a too short string. |
||
44 | */ |
||
45 | ?int $min = null, |
||
46 | /** |
||
47 | * @var int|null maximum length. null means no maximum length limit. Can't be combined with |
||
48 | * {@see $exactly}. |
||
49 | * |
||
50 | * @see $greaterThanMaxMessage for the customized message for a too long string. |
||
51 | */ |
||
52 | ?int $max = null, |
||
53 | /** |
||
54 | * @var int|null exact length. `null` means no strict comparison. Mutually exclusive with {@see $min} and |
||
55 | * {@see $max}. |
||
56 | */ |
||
57 | ?int $exactly = null, |
||
58 | /** |
||
59 | * @var string user-defined error message used when the value is not a string. |
||
60 | */ |
||
61 | private string $incorrectInputMessage = 'This value must be a string.', |
||
62 | /** |
||
63 | * @var string user-defined error message used when the length of the value is smaller than {@see $min}. |
||
64 | */ |
||
65 | string $lessThanMinMessage = 'This value must contain at least {min, number} {min, plural, one{character} ' . |
||
66 | 'other{characters}}.', |
||
67 | /** |
||
68 | * @var string user-defined error message used when the length of the value is greater than {@see $max}. |
||
69 | */ |
||
70 | string $greaterThanMaxMessage = 'This value must contain at most {max, number} {max, plural, one{character} ' . |
||
71 | 'other{characters}}.', |
||
72 | string $notExactlyMessage = 'This value must contain exactly {exactly, number} {exactly, plural, ' . |
||
73 | 'one{character} other{characters}}.', |
||
74 | /** |
||
75 | * @var string the encoding of the string value to be validated (e.g. 'UTF-8'). |
||
76 | * If this property is not set, application wide encoding will be used. |
||
77 | */ |
||
78 | private string $encoding = 'UTF-8', |
||
79 | |||
80 | /** |
||
81 | * @var bool|callable|null |
||
82 | */ |
||
83 | private $skipOnEmpty = null, |
||
84 | private bool $skipOnError = false, |
||
85 | /** |
||
86 | * @var Closure(mixed, ValidationContext):bool|null |
||
87 | */ |
||
88 | private ?Closure $when = null |
||
89 | ) { |
||
90 | 32 | $this->initLimitProperties( |
|
91 | $min, |
||
92 | $max, |
||
93 | $exactly, |
||
94 | $lessThanMinMessage, |
||
95 | $greaterThanMaxMessage, |
||
96 | $notExactlyMessage |
||
97 | ); |
||
132 |