Completed
Push — master ( 6f059f...dc27cd )
by Rasmus
05:49
created

CheckSameValue   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 28.13 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 9
loc 32
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A validate() 9 9 3

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 repeated input (e.g. confirming e-mail address or password, etc.)
13
 *
14
 * This validation should be applied to a secondary input confirmation field - it's value
15
 * will be compared against the value of a specified primary input field.
16
 *
17
 * In a scenario where the user is required to confirm e.g. an e-mail address or password,
18
 * apply the e-mail or password validation to the primary field - then apply this validation
19
 * to the secondary field.
20
 */
21
class CheckSameValue implements ValidatorInterface
22
{
23
    /**
24
     * @var FieldInterface
25
     */
26
    private $primary_field;
27
28
    /**
29
     * @var string|null
30
     */
31
    private $error;
32
33
    /**
34
     * @param FieldInterface $primary_field primary input field
35
     * @param string|null    $error         optional custom error message
36
     */
37 1
    public function __construct(FieldInterface $primary_field, $error = null)
38
    {
39 1
        $this->primary_field = $primary_field;
40 1
        $this->error = $error;
41 1
    }
42
43 1 View Code Duplication
    public function validate(FieldInterface $field, InputModel $model, InputValidation $validation)
0 ignored issues
show
Duplication introduced by
This method 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...
44
    {
45 1
        if ($model->getInput($field) !== $model->getInput($this->primary_field)) {
46 1
            $model->setError(
47
                $field,
48 1
                $this->error ?: lang::text("mindplay/kissform", "confirm", ["field" => $validation->getLabel($field)])
49
            );
50
        }
51 1
    }
52
}
53