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 |
||
10 | abstract class BaseService |
||
11 | { |
||
12 | const USER_NOT_FOUND = 'El usuario solicitado no existe.'; |
||
13 | const USER_NAME_NOT_FOUND = 'No se encontraron usuarios con ese nombre.'; |
||
14 | const USER_NAME_REQUIRED = 'Ingrese el nombre del usuario.'; |
||
15 | const USER_INFO_REQUIRED = 'Ingrese los datos a actualizar del usuario.'; |
||
16 | const USER_DELETED = 'El usuario fue eliminado correctamente.'; |
||
17 | const USER_NAME_INVALID = 'El nombre ingresado es incorrecto.'; |
||
18 | const USER_EMAIL_INVALID = 'El email ingresado es incorrecto.'; |
||
19 | |||
20 | const TASK_NOT_FOUND = 'La tarea solicitada no existe.'; |
||
21 | const TASK_NAME_NOT_FOUND = 'No se encontraron tareas con ese nombre.'; |
||
22 | const TASK_NAME_REQUIRED = 'Ingrese el nombre de la tarea.'; |
||
23 | const TASK_INFO_REQUIRED = 'Ingrese los datos a actualizar de la tarea.'; |
||
24 | const TASK_DELETED = 'La tarea fue eliminada correctamente.'; |
||
25 | const TASK_NAME_INVALID = 'La tarea ingresada es incorrecta.'; |
||
26 | const TASK_STATUS_INVALID = 'El estado ingresado es incorrecto.'; |
||
27 | |||
28 | protected $database; |
||
29 | protected $request; |
||
30 | protected $response; |
||
31 | protected $args; |
||
32 | |||
33 | /** |
||
34 | * Validate and sanitize input data when create new user. |
||
35 | * |
||
36 | * @param array $input |
||
37 | * @return string |
||
38 | * @throws \Exception |
||
39 | */ |
||
40 | protected function validateInputOnCreateUser($input) |
||
53 | |||
54 | /** |
||
55 | * Validate and sanitize input data when update a user. |
||
56 | * |
||
57 | * @param array $input |
||
58 | * @param object $user |
||
59 | * @return string |
||
60 | * @throws \Exception |
||
61 | */ |
||
62 | protected function validateInputOnUpdateUser($input, $user) |
||
78 | |||
79 | /** |
||
80 | * Validate and sanitize input data when create new user. |
||
81 | * |
||
82 | * @param array $input |
||
83 | * @return string |
||
84 | * @throws \Exception |
||
85 | */ |
||
86 | protected function validateInputOnCreateTask($input) |
||
99 | |||
100 | /** |
||
101 | * Validate and sanitize a username. |
||
102 | * |
||
103 | * @param string $name |
||
104 | * @return string |
||
105 | * @throws \Exception |
||
106 | */ |
||
107 | View Code Duplication | protected function validateName($name) |
|
115 | |||
116 | /** |
||
117 | * Validate and sanitize a email address. |
||
118 | * |
||
119 | * @param string $emailValue |
||
120 | * @return string |
||
121 | * @throws \Exception |
||
122 | */ |
||
123 | protected function validateEmail($emailValue) |
||
132 | |||
133 | View Code Duplication | protected function validateTaskName($name) |
|
141 | } |
||
142 |
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.