Completed
Push — master ( f7f501...458ded )
by Rasmus
05:42
created

CheckFloat   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 3
dl 31
loc 31
ccs 11
cts 11
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A validate() 15 15 4

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\Facets\ValidatorInterface;
7
use mindplay\kissform\InputModel;
8
use mindplay\kissform\InputValidation;
9
use mindplay\lang;
10
11
/**
12
 * Validate numeric input, allowing floating point values.
13
 */
14 View Code Duplication
class CheckFloat implements ValidatorInterface
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...
15
{
16
    /**
17
     * @var string|null
18
     */
19
    protected $error;
20
21
    /**
22
     * @param string|null $error optional custom error message
23
     */
24 6
    public function __construct($error = null)
25
    {
26 6
        $this->error = $error;
27 6
    }
28
29 4
    public function validate(FieldInterface $field, InputModel $model, InputValidation $validation)
30
    {
31 4
        $input = $model->getInput($field);
32
33 4
        if ($input === null) {
34 4
            return; // no input
35
        }
36
37 4
        if (filter_var($input, FILTER_VALIDATE_FLOAT) === false) {
38 1
            $model->setError(
39
                $field,
40 1
                $this->error ?: lang::text("mindplay/kissform", "float", ["field" => $validation->getLabel($field)])
41
            );
42
        }
43 4
    }
44
}
45