| Conditions | 11 |
| Paths | 6 |
| Total Lines | 23 |
| Code Lines | 11 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 12 |
| CRAP Score | 11 |
| 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:
| 1 | <?php |
||
| 11 | 6 | public function getMessage($value, ?array $options) |
|
| 12 | { |
||
| 13 | 6 | if (!\is_scalar($value)) { |
|
| 14 | 1 | return 'Param must be a scalar value'; |
|
| 15 | } |
||
| 16 | |||
| 17 | 5 | if ($options === null || (!isset($options['min']) && !isset($options['max']))) { |
|
| 18 | 1 | throw new \InvalidArgumentException('Options must include at least one, min or max param'); |
|
| 19 | } |
||
| 20 | |||
| 21 | 4 | if (isset($options['max'], $options['min']) && (int)$options['max'] < (int)$options['min']) { |
|
| 22 | 1 | throw new \InvalidArgumentException('max param must be greater than min param'); |
|
| 23 | } |
||
| 24 | |||
| 25 | 3 | if (isset($options['min']) && \strlen((string)$value) < (int)$options['min']) { |
|
| 26 | 1 | return sprintf('The minimum length is %s', (int)$options['min']); |
|
| 27 | } |
||
| 28 | |||
| 29 | 2 | if (isset($options['max']) && \strlen((string)$value) > (int)$options['max']) { |
|
| 30 | 1 | return sprintf('The maximum value is %s', (int)$options['max']); |
|
| 31 | } |
||
| 32 | |||
| 33 | 1 | return null; |
|
| 34 | } |
||
| 36 |