These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 minimum length of a string. |
||
13 | */ |
||
14 | View Code Duplication | class CheckMinLength implements ValidatorInterface |
|
0 ignored issues
–
show
|
|||
15 | { |
||
16 | /** |
||
17 | * @var string|null |
||
18 | */ |
||
19 | private $error; |
||
20 | |||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | private $min; |
||
25 | |||
26 | /** |
||
27 | * @param int|null $min min. length |
||
28 | * @param string|null $error optional custom error message |
||
29 | */ |
||
30 | 8 | public function __construct($min, $error = null) |
|
31 | { |
||
32 | 8 | $this->error = $error; |
|
33 | 8 | $this->min = $min; |
|
34 | 8 | } |
|
35 | |||
36 | 1 | public function validate(FieldInterface $field, InputModel $model, InputValidation $validation) |
|
37 | { |
||
38 | 1 | $input = $model->getInput($field); |
|
39 | |||
40 | 1 | if ($input === null) { |
|
41 | 1 | return; // no input |
|
42 | } |
||
43 | |||
44 | 1 | $length = mb_strlen($input); |
|
45 | |||
46 | 1 | if ($length < $this->min) { |
|
47 | 1 | $model->setError( |
|
48 | 1 | $field, |
|
49 | 1 | $this->error ?: lang::text("mindplay/kissform", "minLength", ["field" => $validation->getLabel($field), "min" => $this->min]) |
|
50 | ); |
||
51 | } |
||
52 | 1 | } |
|
53 | } |
||
54 |
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.