CheckRange   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 12.77 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 4
dl 6
loc 47
ccs 16
cts 17
cp 0.9412
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B validate() 6 21 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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