Conditions | 2 |
Paths | 2 |
Total Lines | 55 |
Code Lines | 6 |
Lines | 0 |
Ratio | 0 % |
Tests | 6 |
CRAP Score | 2 |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
52 | public function __construct( |
||
53 | /** |
||
54 | * @var scalar|null The constant value to be compared with. When both this property and {@see $targetAttribute} |
||
55 | * are set, this property takes precedence. |
||
56 | */ |
||
57 | private int|float|string|bool|null $targetValue = null, |
||
58 | /** |
||
59 | * @var string|null The name of the attribute to be compared with. When both this property and |
||
60 | * {@see $targetValue} are set, the {@see $targetValue} takes precedence. |
||
61 | */ |
||
62 | private string|null $targetAttribute = null, |
||
63 | private string $incorrectInputMessage = 'The allowed types are integer, float, string, boolean and null.', |
||
64 | private string $incorrectDataSetTypeMessage = 'The attribute value returned from a custom data set must have ' . |
||
65 | 'a scalar type.', |
||
66 | /** |
||
67 | * @var string|null User-defined error message. |
||
68 | */ |
||
69 | private string|null $message = null, |
||
70 | /** |
||
71 | * @var string The type of the values being compared. |
||
72 | */ |
||
73 | private string $type = self::TYPE_STRING, |
||
74 | /** |
||
75 | * @var string The operator for comparison. The following operators are supported: |
||
76 | * |
||
77 | * - `==`: check if two values are equal. The comparison is done in non-strict mode. |
||
78 | * - `===`: check if two values are equal. The comparison is done in strict mode. |
||
79 | * - `!=`: check if two values are NOT equal. The comparison is done in non-strict mode. |
||
80 | * - `!==`: check if two values are NOT equal. The comparison is done in strict mode. |
||
81 | * - `>`: check if value being validated is greater than the value being compared with. |
||
82 | * - `>=`: check if value being validated is greater than or equal to the value being compared with. |
||
83 | * - `<`: check if value being validated is less than the value being compared with. |
||
84 | * - `<=`: check if value being validated is less than or equal to the value being compared with. |
||
85 | * |
||
86 | * When you want to compare numbers, make sure to also change {@see $type} to {@see TYPE_NUMBER}. |
||
87 | */ |
||
88 | private string $operator = '==', |
||
89 | /** |
||
90 | * @var bool|callable|null |
||
91 | */ |
||
92 | private mixed $skipOnEmpty = null, |
||
93 | private bool $skipOnError = false, |
||
94 | /** |
||
95 | * @var WhenType |
||
96 | */ |
||
97 | 34 | private Closure|null $when = null, |
|
98 | 1 | ) { |
|
99 | if (!isset($this->validOperatorsMap[$this->operator])) { |
||
100 | 1 | $wrapInQuotesCallable = static fn (string $operator): string => '"' . $operator . '"'; |
|
101 | 1 | /** @var string[] $validOperators */ |
|
102 | 1 | $validOperators = array_keys($this->validOperatorsMap); |
|
103 | $validOperatorsString = implode(', ', array_map($wrapInQuotesCallable, $validOperators)); |
||
104 | 1 | $message = "Operator \"$operator\" is not supported. The valid operators are: $validOperatorsString."; |
|
105 | |||
106 | throw new InvalidArgumentException($message); |
||
107 | } |
||
187 |