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 ActiveRecord extends LiteRecord implements \JsonSerializable |
||
| 26 | { |
||
| 27 | |||
| 28 | const BELONG_TO = 1; |
||
| 29 | const HAS_MANY = 2; |
||
| 30 | const HAS_ONE = 3; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Describe the relationships |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | static protected $_rs = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Store all information about relationship for populate methods |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $_populate = []; |
||
| 43 | |||
| 44 | static function resolver($rs, $obj){ |
||
| 50 | |||
| 51 | static public function hasMany($name, $class, $via = NULL){ |
||
| 60 | |||
| 61 | public function jsonSerialize(){ |
||
| 66 | |||
| 67 | |||
| 68 | public function __call($name, $arguments){ |
||
| 75 | |||
| 76 | static protected function getRelationship($rel){ |
||
| 81 | |||
| 82 | public function populate($rel){ |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Actualizar registros. |
||
| 89 | * |
||
| 90 | * @param array $fields |
||
| 91 | * @param string $where condiciones |
||
| 92 | * @param array $values valores para condiciones |
||
| 93 | * |
||
| 94 | * @return int numero de registros actualizados |
||
| 95 | */ |
||
| 96 | View Code Duplication | public static function updateAll(array $fields, $where = null, array $values = []) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Eliminar registro. |
||
| 110 | * |
||
| 111 | * @param string $where condiciones |
||
| 112 | * @param array |string $values valores |
||
| 113 | * |
||
| 114 | * @return int numero de registros eliminados |
||
| 115 | */ |
||
| 116 | public static function deleteAll($where = null, $values = null) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Elimina caracteres que podrian ayudar a ejecutar |
||
| 127 | * un ataque de Inyeccion SQL. |
||
| 128 | * |
||
| 129 | * @param string $sqlItem |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | * @throw KumbiaException |
||
| 133 | */ |
||
| 134 | public static function sqlItemSanitize($sqlItem) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Obtener la primera coincidencia por el campo indicado. |
||
| 149 | * |
||
| 150 | * @param string $field campo |
||
| 151 | * @param string $value valor |
||
| 152 | * @param array $params parametros adicionales |
||
| 153 | * order: criterio de ordenamiento |
||
| 154 | * fields: lista de campos |
||
| 155 | * join: joins de tablas |
||
| 156 | * group: agrupar campos |
||
| 157 | * having: condiciones de grupo |
||
| 158 | * offset: valor offset |
||
| 159 | * |
||
| 160 | * @return ActiveRecord |
||
| 161 | */ |
||
| 162 | View Code Duplication | public static function firstBy($field, $value, $params = []) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Obtener la primera coincidencia de las condiciones indicadas. |
||
| 172 | * |
||
| 173 | * @param array $params parametros de bus |
||
| 174 | * @param string $field campo |
||
| 175 | * @param string $value valor |
||
| 176 | * @param array $params parametros adicionales |
||
| 177 | * order: criterio de ordenamiento |
||
| 178 | * fields: lista de campos |
||
| 179 | * group: agrupar campos |
||
| 180 | * join: joins de tablas |
||
| 181 | * having: condiciones de grupo |
||
| 182 | * offset: valor offset queda |
||
| 183 | * @param array $values valores de busqueda |
||
| 184 | * |
||
| 185 | * @return ActiveRecord |
||
| 186 | */ |
||
| 187 | public static function first($params = [], $values = []) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Obtener todos los registros. |
||
| 199 | * |
||
| 200 | * @param array $params |
||
| 201 | * where: condiciones where |
||
| 202 | * order: criterio de ordenamiento |
||
| 203 | * fields: lista de campos |
||
| 204 | * join: joins de tablas |
||
| 205 | * group: agrupar campos |
||
| 206 | * having: condiciones de grupo |
||
| 207 | * limit: valor limit |
||
| 208 | * offset: valor offset |
||
| 209 | * @param array $values valores de busqueda |
||
| 210 | * |
||
| 211 | * @return \PDOStatement |
||
| 212 | */ |
||
| 213 | public static function all($params = [], $values = []) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Do a query. |
||
| 222 | * |
||
| 223 | * @param array $array params of query |
||
| 224 | * |
||
| 225 | * @return \PDOStatement|false |
||
| 226 | */ |
||
| 227 | protected static function doQuery(array $array) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Retorna los parametros para el doQuery. |
||
| 239 | * |
||
| 240 | * @param array $array |
||
| 241 | * |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | protected static function getParam(array &$array) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Retorna los values para el doQuery. |
||
| 253 | * |
||
| 254 | * @param array $array |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | protected static function getValues(array $array) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Obtener todas las coincidencias por el campo indicado. |
||
| 266 | * |
||
| 267 | * @param string $field campo |
||
| 268 | * @param string $value valor |
||
| 269 | * @param array $params |
||
| 270 | * order: criterio de ordenamiento |
||
| 271 | * fields: lista de campos |
||
| 272 | * join: joins de tablas |
||
| 273 | * group: agrupar campos |
||
| 274 | * having: condiciones de grupo |
||
| 275 | * limit: valor limit |
||
| 276 | * offset: valor offset |
||
| 277 | * |
||
| 278 | * @return \PDOStatement |
||
| 279 | */ |
||
| 280 | View Code Duplication | public static function allBy($field, $value, $params = []) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Cuenta los registros que coincidan con las condiciones indicadas. |
||
| 290 | * |
||
| 291 | * @param string $where condiciones |
||
| 292 | * @param array $values valores |
||
| 293 | * |
||
| 294 | * @return int |
||
| 295 | */ |
||
| 296 | View Code Duplication | public static function count($where = null, $values = null) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Paginar. |
||
| 310 | * |
||
| 311 | * @param array $params |
||
| 312 | * @param int $page numero de pagina |
||
| 313 | * @param int $perPage cantidad de items por pagina |
||
| 314 | * @param array $values valores |
||
| 315 | * |
||
| 316 | * @return Paginator |
||
| 317 | */ |
||
| 318 | public static function paginate(array $params, $page, $perPage, $values = null) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Obtiene todos los registros de la consulta sql. |
||
| 333 | * |
||
| 334 | * @param string $sql |
||
| 335 | * @param string | array $values |
||
| 336 | * |
||
| 337 | * @return array |
||
| 338 | */ |
||
| 339 | View Code Duplication | public static function allBySql($sql, $values = null) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Obtiene el primer registro de la consulta sql. |
||
| 350 | * |
||
| 351 | * @param string $sql |
||
| 352 | * @param string | array $values |
||
| 353 | * |
||
| 354 | * @return array |
||
| 355 | */ |
||
| 356 | View Code Duplication | public static function firstBySql($sql, $values = null) |
|
| 364 | } |
||
| 365 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.