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 |
||
26 | class ActiveRecord extends LiteRecord implements \JsonSerializable |
||
27 | { |
||
28 | const BELONG_TO = 1; |
||
29 | const HAS_MANY = 2; |
||
30 | const HAS_ONE = 3; |
||
31 | |||
32 | /** |
||
33 | * Describe the relationships. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected static $relations = []; |
||
38 | |||
39 | public static function resolver($relations, $obj) |
||
48 | |||
49 | View Code Duplication | public static function hasMany($name, $class, $via = null) |
|
59 | |||
60 | View Code Duplication | public static function hasOne($name, $class, $via = null) |
|
70 | |||
71 | /** |
||
72 | * json_encode() method. |
||
73 | */ |
||
74 | public function jsonSerialize() |
||
78 | |||
79 | public function __get($key) |
||
93 | |||
94 | protected static function getRelationship($rel) |
||
102 | |||
103 | public function populate($rel) |
||
108 | |||
109 | /** |
||
110 | * Pagination of Results. |
||
111 | * |
||
112 | * @param array $params [description] |
||
113 | * @param array $values [description] |
||
114 | * @param int $page [description] |
||
115 | * @param int $per_page [description] |
||
116 | * |
||
117 | * @return Paginator [description] |
||
118 | */ |
||
119 | public static function pagination($params = [], $values = [], $page = 1, $per_page = 10) |
||
127 | |||
128 | /** |
||
129 | * Actualizar registros. |
||
130 | * |
||
131 | * @param array $fields |
||
132 | * @param string $where condiciones |
||
133 | * @param array $values valores para condiciones |
||
134 | * |
||
135 | * @return int numero de registros actualizados |
||
136 | */ |
||
137 | public static function updateAll(array $fields, string $where = '', array $values = []) |
||
145 | |||
146 | /** |
||
147 | * Eliminar registro. |
||
148 | * |
||
149 | * @param string $where condiciones |
||
150 | * @param array |string $values valores |
||
151 | * |
||
152 | * @return int numero de registros eliminados |
||
153 | */ |
||
154 | public static function deleteAll($where = null, $values = null) |
||
162 | |||
163 | /** |
||
164 | * Elimina caracteres que podrian ayudar a ejecutar |
||
165 | * un ataque de Inyeccion SQL. |
||
166 | * |
||
167 | * @param string $sqlItem |
||
168 | * |
||
169 | * @return string |
||
170 | * @throws \RuntimeException |
||
171 | */ |
||
172 | public static function sqlItemSanitize($sqlItem) |
||
184 | |||
185 | /** |
||
186 | * Obtener la primera coincidencia por el campo indicado. |
||
187 | * |
||
188 | * @param string $field campo |
||
189 | * @param string $value valor |
||
190 | * @param array $params parametros adicionales |
||
191 | * order: criterio de ordenamiento |
||
192 | * fields: lista de campos |
||
193 | * join: joins de tablas |
||
194 | * group: agrupar campos |
||
195 | * having: condiciones de grupo |
||
196 | * offset: valor offset |
||
197 | * |
||
198 | * @return ActiveRecord |
||
199 | */ |
||
200 | View Code Duplication | public static function firstBy($field, $value, $params = []) |
|
207 | |||
208 | /** |
||
209 | * Obtener la primera coincidencia de las condiciones indicadas. |
||
210 | * |
||
211 | * @param array $params parametros adicionales |
||
212 | * order: criterio de ordenamiento |
||
213 | * fields: lista de campos |
||
214 | * group: agrupar campos |
||
215 | * join: joins de tablas |
||
216 | * having: condiciones de grupo |
||
217 | * offset: valor offset queda |
||
218 | * @param array $values valores de busqueda |
||
219 | * |
||
220 | * @return ActiveRecord |
||
221 | */ |
||
222 | public static function first($params = [], $values = []) |
||
231 | |||
232 | /** |
||
233 | * Obtener todos los registros. |
||
234 | * |
||
235 | * @param array $params |
||
236 | * where: condiciones where |
||
237 | * order: criterio de ordenamiento |
||
238 | * fields: lista de campos |
||
239 | * join: joins de tablas |
||
240 | * group: agrupar campos |
||
241 | * having: condiciones de grupo |
||
242 | * limit: valor limit |
||
243 | * offset: valor offset |
||
244 | * @param array $values valores de busqueda |
||
245 | * |
||
246 | * @return \PDOStatement |
||
247 | */ |
||
248 | public static function all($params = [], $values = []) |
||
254 | |||
255 | /** |
||
256 | * Do a query. |
||
257 | * |
||
258 | * @param array $array params of query |
||
259 | * |
||
260 | * @return \PDOStatement|false |
||
261 | */ |
||
262 | protected static function doQuery(array $array) |
||
271 | |||
272 | /** |
||
273 | * Retorna los parametros para el doQuery. |
||
274 | * |
||
275 | * @param array $array |
||
276 | * |
||
277 | * @return array |
||
278 | */ |
||
279 | protected static function getParam(array &$array) |
||
285 | |||
286 | /** |
||
287 | * Retorna los values para el doQuery. |
||
288 | * |
||
289 | * @param array $array |
||
290 | * |
||
291 | * @return array |
||
292 | */ |
||
293 | protected static function getValues(array $array) |
||
298 | |||
299 | /** |
||
300 | * Obtener todas las coincidencias por el campo indicado. |
||
301 | * |
||
302 | * @param string $field campo |
||
303 | * @param string $value valor |
||
304 | * @param array $params |
||
305 | * order: criterio de ordenamiento |
||
306 | * fields: lista de campos |
||
307 | * join: joins de tablas |
||
308 | * group: agrupar campos |
||
309 | * having: condiciones de grupo |
||
310 | * limit: valor limit |
||
311 | * offset: valor offset |
||
312 | * |
||
313 | * @return \PDOStatement |
||
314 | */ |
||
315 | View Code Duplication | public static function allBy($field, $value, $params = []) |
|
322 | |||
323 | /** |
||
324 | * Cuenta los registros que coincidan con las condiciones indicadas. |
||
325 | * |
||
326 | * @param string $where condiciones |
||
327 | * @param array $values valores |
||
328 | * |
||
329 | * @return int |
||
330 | */ |
||
331 | public static function count(string $where = '', array $values = []) |
||
340 | |||
341 | /** |
||
342 | * Paginar. |
||
343 | * |
||
344 | * @param array $params |
||
345 | * @param int $page numero de pagina |
||
346 | * @param int $perPage cantidad de items por pagina |
||
347 | * @param array $values valores |
||
348 | * |
||
349 | * @return Paginator |
||
350 | */ |
||
351 | public static function paginate(array $params, int $page, int $perPage, $values = null) |
||
363 | |||
364 | /** |
||
365 | * Obtiene todos los registros de la consulta sql. |
||
366 | * |
||
367 | * @param string $sql |
||
368 | * @param string | array $values |
||
369 | * |
||
370 | * @return array |
||
371 | */ |
||
372 | View Code Duplication | public static function allBySql($sql, $values = null) |
|
380 | |||
381 | /** |
||
382 | * Obtiene el primer registro de la consulta sql. |
||
383 | * |
||
384 | * @param string $sql |
||
385 | * @param string | array $values |
||
386 | * |
||
387 | * @return array |
||
388 | */ |
||
389 | View Code Duplication | public static function firstBySql($sql, $values = null) |
|
397 | } |
||
398 |
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.