CheckMinValue   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 40
loc 40
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0

2 Methods

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

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 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