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 |
||
25 | class BaseRecord |
||
26 | { |
||
27 | /** |
||
28 | * Database por defecto usa default. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected static $database = 'default'; |
||
33 | |||
34 | /** |
||
35 | * PK por defecto, si no existe mira en metadata. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected static $pk = 'id'; |
||
40 | |||
41 | /** |
||
42 | * Constructor. |
||
43 | * |
||
44 | * @param array $data |
||
45 | */ |
||
46 | public function __construct(array $data = []) |
||
50 | |||
51 | /** |
||
52 | * Get the Primary Key value for the object |
||
53 | * @return mixed |
||
54 | */ |
||
55 | public function pk(){ |
||
59 | |||
60 | /** |
||
61 | * Cargar datos al objeto. |
||
62 | * |
||
63 | * @param array $data |
||
64 | */ |
||
65 | public function dump(array $data = []) |
||
71 | |||
72 | /** |
||
73 | * Listado de los campos. |
||
74 | * |
||
75 | * @return array |
||
76 | */ |
||
77 | public function getFields() |
||
82 | |||
83 | /** |
||
84 | * Alias de los campos. |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | public static function alias() |
||
92 | |||
93 | /** |
||
94 | * Verifica que PK este seteado. |
||
95 | * |
||
96 | * @throw \KumbiaException |
||
97 | */ |
||
98 | protected function hasPK() |
||
105 | |||
106 | /** |
||
107 | * Get the name of the primary key |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | public static function getPK() |
||
119 | |||
120 | /** |
||
121 | * Obtiene nombre de tabla en la bd. |
||
122 | * |
||
123 | * @return string smallcase del nombre de la clase |
||
124 | */ |
||
125 | public static function getTable() |
||
131 | |||
132 | /** |
||
133 | * Obtiene el schema al que pertenece. |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | public static function getSchema() |
||
141 | |||
142 | /** |
||
143 | * Obtiene la combinación de esquema y tabla. |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | public static function getSource() |
||
156 | |||
157 | /** |
||
158 | * Obtiene la conexión que se utilizará (contenidas en databases.php). |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | public static function getDatabase() |
||
166 | |||
167 | /** |
||
168 | * Obtiene metadatos. |
||
169 | * |
||
170 | * @return Metadata\Metadata |
||
171 | */ |
||
172 | public static function metadata() |
||
181 | |||
182 | /** |
||
183 | * Obtiene manejador de conexion a la base de datos. |
||
184 | * |
||
185 | * @param bool $force forzar nueva conexion PDO |
||
186 | * |
||
187 | * @return \PDO |
||
188 | */ |
||
189 | protected static function dbh($force = false) |
||
193 | |||
194 | /** |
||
195 | * Consulta sql preparada. |
||
196 | * |
||
197 | * @param string $sql |
||
198 | * |
||
199 | * @return \PDOStatement |
||
200 | * @throw \PDOException |
||
201 | */ |
||
202 | public static function prepare($sql) |
||
209 | |||
210 | /** |
||
211 | * Retorna el último ID insertado. |
||
212 | * |
||
213 | * @return ID |
||
214 | */ |
||
215 | public static function lastInsertId() |
||
219 | |||
220 | /** |
||
221 | * Consulta sql. |
||
222 | * |
||
223 | * @param string $sql |
||
224 | * |
||
225 | * @return \PDOStatement |
||
226 | * @throw PDOException |
||
227 | */ |
||
228 | public static function sql($sql) |
||
235 | |||
236 | /** |
||
237 | * Ejecuta consulta sql. |
||
238 | * |
||
239 | * @param string $sql |
||
240 | * @param array | string $values valores |
||
241 | * |
||
242 | * @return PDOStatement |
||
243 | */ |
||
244 | public static function query($sql, $values = null) |
||
257 | |||
258 | /** |
||
259 | * Verifica si existe el registro. |
||
260 | * |
||
261 | * @param string $pk valor para clave primaria |
||
262 | * |
||
263 | * @return bool |
||
264 | */ |
||
265 | View Code Duplication | public static function exists($pk) |
|
272 | |||
273 | /** |
||
274 | * Paginar consulta sql. |
||
275 | * |
||
276 | * @param string $sql consulta select sql |
||
277 | * @param int $page numero de pagina |
||
278 | * @param int $perPage cantidad de items por pagina |
||
279 | * @param array $values valores |
||
280 | * |
||
281 | * @return Paginator |
||
282 | */ |
||
283 | public static function paginateQuery($sql, $page, $perPage, $values = []) |
||
287 | |||
288 | /** |
||
289 | * Devuelve el nombre del drive PDO utilizado. |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | public static function getDriver() |
||
297 | |||
298 | /** |
||
299 | * Comienza una trasacción. |
||
300 | * |
||
301 | * @return bool |
||
302 | */ |
||
303 | public static function begin() |
||
307 | |||
308 | /** |
||
309 | * Da marcha atrás a una trasacción. |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | public static function rollback() |
||
317 | |||
318 | /** |
||
319 | * Realiza el commit de una trasacción. |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | public static function commit() |
||
327 | } |
||
328 |
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.