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 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 | View Code Duplication | class CheckSameValue implements ValidatorInterface |
|
0 ignored issues
–
show
|
|||
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 | public function validate(FieldInterface $field, InputModel $model, InputValidation $validation) |
|
44 | { |
||
45 | 1 | if ($model->getInput($field) !== $model->getInput($this->primary_field)) { |
|
46 | 1 | $model->setError( |
|
47 | 1 | $field, |
|
48 | 1 | $this->error ?: lang::text("mindplay/kissform", "confirm", ["field" => $validation->getLabel($field)]) |
|
49 | ); |
||
50 | } |
||
51 | 1 | } |
|
52 | } |
||
53 |
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.