Completed
Push — master ( dc27cd...c0eb3b )
by Rasmus
07:46
created

CheckRequired   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 36 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 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 required input
13
 */
14
class CheckRequired implements ValidatorInterface
15
{
16
    /**
17
     * @var string|null
18
     */
19
    private $error;
20
21
    /**
22
     * @param string $error optional custom error message
0 ignored issues
show
Documentation introduced by
Should the type for parameter $error not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
23
     */
24 10
    public function __construct($error = null)
25
    {
26 10
        $this->error = $error;
27 10
    }
28
29 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...
30
    {
31 1
        if ($model->getInput($field) === null) {
32 1
            $model->setError(
33 1
                $field,
34 1
                $this->error ?: lang::text("mindplay/kissform", "required", ["field" => $validation->getLabel($field)])
35
            );
36
        }
37 1
    }
38
}
39