CheckMaxValue::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 6
loc 6
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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 maximum.
12
 */
13 View Code Duplication
class CheckMaxValue 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|null
17
     */
18
    private $max;
19
20
    /**
21
     * @param int|float   $max   max value
22
     * @param string|null $error optional custom error message
23
     */
24 3
    public function __construct($max, $error = null)
25
    {
26 3
        parent::__construct($error);
27
28 3
        $this->max = $max;
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 > $this->max) {
42 1
            $model->setError(
43 1
                $field,
44 1
                $this->error ?: lang::text("mindplay/kissform", "maxValue", ["field" => $validation->getLabel($field), "max" => $this->max])
45
            );
46
        }
47 1
    }
48
}
49