CheckRange::validate()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 6
Ratio 28.57 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 6
loc 21
ccs 11
cts 12
cp 0.9167
rs 8.7624
cc 6
eloc 11
nc 4
nop 3
crap 6.0208
1
<?php
2
3
namespace mindplay\kissform\Validators;
4
5
use mindplay\kissform\Facets\FieldInterface;
6
use mindplay\kissform\InputModel;
7
use mindplay\kissform\InputValidation;
8
use mindplay\lang;
9
10
/**
11
 * Validate numerical value within a min/max range.
12
 */
13
class CheckRange extends CheckFloat
14
{
15
    /**
16
     * @var int|float
17
     */
18
    private $min;
19
20
    /**
21
     * @var int|float
22
     */
23
    private $max;
24
25
    /**
26
     * @param int|float   $min   min value
27
     * @param int|float   $max   max value
28
     * @param string|null $error optional custom error message
29
     */
30 3
    public function __construct($min, $max, $error = null)
31
    {
32 3
        parent::__construct($error);
33
34 3
        $this->min = $min;
35 3
        $this->max = $max;
36 3
    }
37
38 1
    public function validate(FieldInterface $field, InputModel $model, InputValidation $validation)
39
    {
40 1
        parent::validate($field, $model, $validation);
41
42 1
        if ($model->hasError($field)) {
43
            return; // parent validation (IsNumber) failed
44
        }
45
46 1
        $input = $model->getInput($field);
47
48 1
        if ($input === null) {
49 1
            return; // no input, no minimum value
50
        }
51
52 1 View Code Duplication
        if ($input < $this->min || $input > $this->max) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53 1
            $model->setError(
54 1
                $field,
55 1
                $this->error ?: lang::text("mindplay/kissform", "range", ["field" => $validation->getLabel($field), "min" => $this->min, "max" => $this->max])
56
            );
57
        }
58 1
    }
59
}
60