CheckAccept   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A validate() 11 11 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 an acceptance checkbox (for confirmations, e.g. accepted privacy policy or terms of service)
13
 */
14 View Code Duplication
class CheckAccept 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 expected checkbox value
18
     */
19
    private $checked_value;
20
21
    /**
22
     * @var string|null
23
     */
24
    private $error;
25
26
    /**
27
     * @param string      $checked_value expected checkbox value
28
     * @param string|null $error         optional custom error message
29
     */
30 2
    public function __construct($checked_value, $error = null)
31
    {
32 2
        $this->checked_value = $checked_value;
33 2
        $this->error = $error;
34 2
    }
35
36 1
    public function validate(FieldInterface $field, InputModel $model, InputValidation $validation)
37
    {
38 1
        $value = $model->getInput($field);
39
40 1
        if ($value != $this->checked_value) {
41 1
            $model->setError(
42 1
                $field,
43 1
                $this->error ?: lang::text("mindplay/kissform", "checked", ["field" => $validation->getLabel($field)])
44
            );
45
        }
46 1
    }
47
}
48