Conditions | 2 |
Paths | 2 |
Total Lines | 52 |
Code Lines | 3 |
Lines | 0 |
Ratio | 0 % |
Tests | 3 |
CRAP Score | 2.0625 |
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 |
||
47 | 28 | public function __construct( |
|
48 | /** |
||
49 | * @var mixed The constant value to be compared with. When both this property |
||
50 | * and {@see $targetAttribute} are set, this property takes precedence. |
||
51 | */ |
||
52 | private $targetValue = null, |
||
53 | /** |
||
54 | * @var string|null The name of the attribute to be compared with. When both this property |
||
55 | * and {@see $targetValue} are set, the {@see $targetValue} takes precedence. |
||
56 | * |
||
57 | * @see $targetValue |
||
58 | */ |
||
59 | private ?string $targetAttribute = null, |
||
60 | /** |
||
61 | * @var string|null User-defined error message. |
||
62 | */ |
||
63 | private ?string $message = null, |
||
64 | /** |
||
65 | * @var string The type of the values being compared. |
||
66 | */ |
||
67 | private string $type = self::TYPE_STRING, |
||
68 | /** |
||
69 | * @var string The operator for comparison. The following operators are supported: |
||
70 | * |
||
71 | * - `==`: check if two values are equal. The comparison is done is non-strict mode. |
||
72 | * - `===`: check if two values are equal. The comparison is done is strict mode. |
||
73 | * - `!=`: check if two values are NOT equal. The comparison is done is non-strict mode. |
||
74 | * - `!==`: check if two values are NOT equal. The comparison is done is strict mode. |
||
75 | * - `>`: check if value being validated is greater than the value being compared with. |
||
76 | * - `>=`: check if value being validated is greater than or equal to the value being compared with. |
||
77 | * - `<`: check if value being validated is less than the value being compared with. |
||
78 | * - `<=`: check if value being validated is less than or equal to the value being compared with. |
||
79 | * |
||
80 | * When you want to compare numbers, make sure to also change @see $type} to |
||
81 | * {@see TYPE_NUMBER}. |
||
82 | */ |
||
83 | private string $operator = '==', |
||
84 | private bool $skipOnEmpty = false, |
||
85 | /** |
||
86 | * @var callable |
||
87 | */ |
||
88 | private $skipOnEmptyCallback = null, |
||
89 | private bool $skipOnError = false, |
||
90 | /** |
||
91 | * @var Closure(mixed, ValidationContext):bool|null |
||
92 | */ |
||
93 | private ?Closure $when = null, |
||
94 | ) { |
||
95 | 28 | $this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback); |
|
96 | |||
97 | 28 | if (!isset($this->validOperators[$operator])) { |
|
98 | throw new InvalidArgumentException("Operator \"$operator\" is not supported."); |
||
99 | } |
||
160 |