Conditions | 10 |
Paths | 4 |
Total Lines | 56 |
Code Lines | 7 |
Lines | 0 |
Ratio | 0 % |
Tests | 7 |
CRAP Score | 10 |
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 |
||
25 | 6 | public function __construct( |
|
26 | /** |
||
27 | * @var int|null minimum number of items. null means no minimum number limit. |
||
28 | * |
||
29 | * @see $tooFewItemsMessage for the customized message for a value with too few items. |
||
30 | */ |
||
31 | public ?int $min = null, |
||
32 | /** |
||
33 | * @var int|null maximum number of items. null means no maximum number limit. |
||
34 | * |
||
35 | * @see $tooManyItemsMessage for the customized message for a value wuth too many items. |
||
36 | */ |
||
37 | public ?int $max = null, |
||
38 | /** |
||
39 | * @var int|null exact number of items. null means no strict comparison. Mutually exclusive with {@see $min} and |
||
40 | * {@see $max}. |
||
41 | */ |
||
42 | public ?int $exactly = null, |
||
43 | /** |
||
44 | * @var string user-defined error message used when the value is neither an array nor implementing |
||
45 | * {@see \Countable} interface. |
||
46 | * |
||
47 | * @see Countable |
||
48 | */ |
||
49 | public string $message = 'This value must be an array or implement \Countable interface.', |
||
50 | /** |
||
51 | * @var string user-defined error message used when the number of items is smaller than {@see $min}. |
||
52 | */ |
||
53 | public string $tooFewItemsMessage = 'This value must contain at least {min, number} ' . |
||
54 | '{min, plural, one{item} other{items}}.', |
||
55 | /** |
||
56 | * @var string user-defined error message used when the number of items is greater than {@see $max}. |
||
57 | */ |
||
58 | public string $tooManyItemsMessage = 'This value must contain at most {max, number} ' . |
||
59 | '{max, plural, one{item} other{items}}.', |
||
60 | /** |
||
61 | * @var string user-defined error message used when the number of items does not equal {@see $exactly}. |
||
62 | */ |
||
63 | public string $notExactlyMessage = 'This value must contain exactly {max, number} ' . |
||
64 | '{max, plural, one{item} other{items}}.', |
||
65 | public bool $skipOnEmpty = false, |
||
66 | public bool $skipOnError = false, |
||
67 | public ?Closure $when = null, |
||
68 | ) { |
||
69 | 6 | if (!$this->min && !$this->max && !$this->exactly) { |
|
70 | 1 | throw new InvalidArgumentException( |
|
71 | 'At least one of these attributes must be specified: $min, $max, $exactly.' |
||
72 | ); |
||
73 | } |
||
74 | |||
75 | 5 | if ($this->exactly && ($this->min || $this->max)) { |
|
76 | 3 | throw new InvalidArgumentException('$exactly is mutually exclusive with $min and $max.'); |
|
77 | } |
||
78 | |||
79 | 2 | if ($this->min && $this->max && $this->min === $this->max) { |
|
80 | 1 | throw new InvalidArgumentException('Use $exactly instead.'); |
|
81 | } |
||
110 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: