Range::getMessage()   B
last analyzed

Complexity

Conditions 11
Paths 6

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 11

Importance

Changes 0
Metric Value
cc 11
eloc 11
nc 6
nop 2
dl 0
loc 23
ccs 12
cts 12
cp 1
crap 11
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Hop\Validator\Validator;
6
7
class Range implements RuleValidator
8
{
9
    use IsValidTrait;
10
11
    /**
12
     * @inheritdoc
13
     */
14 6
    public function getMessage($value, ?array $options)
15
    {
16 6
        if (!\is_scalar($value)) {
17 1
            return 'Param must be a scalar value';
18
        }
19
20 5
        if ($options === null || (!isset($options['min']) && !isset($options['max']))) {
21 1
            throw new \InvalidArgumentException('Options must include at least one, min or max param');
22
        }
23
24 4
        if (isset($options['max'], $options['min']) && (float)$options['max'] < (float)$options['min']) {
25 1
            throw new \InvalidArgumentException('max param must be greater than min param');
26
        }
27
28 3
        if (isset($options['min']) && (float)$value < (float)$options['min']) {
29 1
            return sprintf('The minimum value is %s', (float)$options['min']);
30
        }
31
32 2
        if (isset($options['max']) && (float)$value > (float)$options['max']) {
33 1
            return sprintf('The maximum value is %s', (float)$options['max']);
34
        }
35
36 1
        return null;
37
    }
38
}
39