CheckMinValue::validate()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 0
Metric Value
dl 21
loc 21
ccs 11
cts 12
cp 0.9167
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 11
nc 4
nop 3
crap 5.0144
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 with a minimum.
12
 */
13 View Code Duplication
class CheckMinValue extends CheckFloat
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
14
{
15
    /**
16
     * @var int|float
17
     */
18
    private $min;
19
20
    /**
21
     * @param int|float   $min   min value
22
     * @param string|null $error optional custom error message
23
     */
24 3
    public function __construct($min, $error = null)
25
    {
26 3
        parent::__construct($error);
27
28 3
        $this->min = $min;
29 3
    }
30
31 1
    public function validate(FieldInterface $field, InputModel $model, InputValidation $validation)
32
    {
33 1
        parent::validate($field, $model, $validation);
34
35 1
        if ($model->hasError($field)) {
36
            return; // parent validation (IsNumber) failed
37
        }
38
39 1
        $input = $model->getInput($field);
40
41 1
        if ($input === null) {
42 1
            return; // no value given
43
        }
44
45 1
        if ($input < $this->min) {
46 1
            $model->setError(
47 1
                $field,
48 1
                $this->error ?: lang::text("mindplay/kissform", "minValue", ["field" => $validation->getLabel($field), "min" => $this->min])
49
            );
50
        }
51 1
    }
52
}
53