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 |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Actualizar registros. |
||
| 29 | * |
||
| 30 | * @param array $fields |
||
| 31 | * @param string $where condiciones |
||
| 32 | * @param array $values valores para condiciones |
||
| 33 | * |
||
| 34 | * @return int numero de registros actualizados |
||
| 35 | */ |
||
| 36 | View Code Duplication | public static function updateAll(array $fields, $where = null, array $values = []) |
|
|
|
|||
| 37 | { |
||
| 38 | if ($values !== null && !is_array($values)) { |
||
| 39 | $values = \array_slice(\func_get_args(), 2); |
||
| 40 | } |
||
| 41 | $sql = QueryGenerator::updateAll(\get_called_class(), $fields, $values, $where); |
||
| 42 | $sth = self::prepare($sql); |
||
| 43 | $sth->execute($values); |
||
| 44 | |||
| 45 | return $sth->rowCount(); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Eliminar registro. |
||
| 50 | * |
||
| 51 | * @param string $where condiciones |
||
| 52 | * @param array |string $values valores |
||
| 53 | * |
||
| 54 | * @return int numero de registros eliminados |
||
| 55 | */ |
||
| 56 | public static function deleteAll($where = null, $values = null) |
||
| 57 | { |
||
| 58 | $source = static::getSource(); |
||
| 59 | $sql = QueryGenerator::deleteAll($source, $where); |
||
| 60 | $sth = self::query($sql, $values); |
||
| 61 | |||
| 62 | return $sth->rowCount(); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Elimina caracteres que podrian ayudar a ejecutar |
||
| 67 | * un ataque de Inyeccion SQL. |
||
| 68 | * |
||
| 69 | * @param string $sqlItem |
||
| 70 | * |
||
| 71 | * @return string |
||
| 72 | * @throw KumbiaException |
||
| 73 | */ |
||
| 74 | public static function sqlItemSanitize($sqlItem) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Obtener la primera coincidencia por el campo indicado. |
||
| 89 | * |
||
| 90 | * @param string $field campo |
||
| 91 | * @param string $value valor |
||
| 92 | * @param array $params parametros adicionales |
||
| 93 | * order: criterio de ordenamiento |
||
| 94 | * fields: lista de campos |
||
| 95 | * join: joins de tablas |
||
| 96 | * group: agrupar campos |
||
| 97 | * having: condiciones de grupo |
||
| 98 | * offset: valor offset |
||
| 99 | * |
||
| 100 | * @return ActiveRecord |
||
| 101 | */ |
||
| 102 | View Code Duplication | public static function firstBy($field, $value, $params = []) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Obtener la primera coincidencia de las condiciones indicadas. |
||
| 112 | * |
||
| 113 | * @param array $params parametros de bus |
||
| 114 | * @param string $field campo |
||
| 115 | * @param string $value valor |
||
| 116 | * @param array $params parametros adicionales |
||
| 117 | * order: criterio de ordenamiento |
||
| 118 | * fields: lista de campos |
||
| 119 | * group: agrupar campos |
||
| 120 | * join: joins de tablas |
||
| 121 | * having: condiciones de grupo |
||
| 122 | * offset: valor offset queda |
||
| 123 | * @param array $values valores de busqueda |
||
| 124 | * |
||
| 125 | * @return ActiveRecord |
||
| 126 | */ |
||
| 127 | public static function first($params = [], $values = []) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Obtener todos los registros. |
||
| 139 | * |
||
| 140 | * @param array $params |
||
| 141 | * where: condiciones where |
||
| 142 | * order: criterio de ordenamiento |
||
| 143 | * fields: lista de campos |
||
| 144 | * join: joins de tablas |
||
| 145 | * group: agrupar campos |
||
| 146 | * having: condiciones de grupo |
||
| 147 | * limit: valor limit |
||
| 148 | * offset: valor offset |
||
| 149 | * @param array $values valores de busqueda |
||
| 150 | * |
||
| 151 | * @return \PDOStatement |
||
| 152 | */ |
||
| 153 | public static function all($params = [], $values = []) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Do a query. |
||
| 162 | * |
||
| 163 | * @param array $array params of query |
||
| 164 | * |
||
| 165 | * @return \PDOStatement|false |
||
| 166 | */ |
||
| 167 | protected static function doQuery(array $array) |
||
| 168 | { |
||
| 169 | $params = self::getParam($array); |
||
| 170 | $values = self::getValues($array); |
||
| 171 | $sql = QueryGenerator::select(static::getSource(), static::getDriver(), $params); |
||
| 172 | $sth = static::query($sql, $values); |
||
| 173 | |||
| 174 | return $sth; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Retorna los parametros para el doQuery. |
||
| 179 | * |
||
| 180 | * @param array $array |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | protected static function getParam(array &$array) |
||
| 185 | { |
||
| 186 | $val = array_shift($array); |
||
| 187 | |||
| 188 | return is_null($val) ? [] : $val; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Retorna los values para el doQuery. |
||
| 193 | * |
||
| 194 | * @param array $array |
||
| 195 | * |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | protected static function getValues(array $array) |
||
| 199 | { |
||
| 200 | return isset($array[0]) ? |
||
| 201 | is_array($array[0]) ? $array[0] : [$array[0]]: $array; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Obtener todas las coincidencias por el campo indicado. |
||
| 206 | * |
||
| 207 | * @param string $field campo |
||
| 208 | * @param string $value valor |
||
| 209 | * @param array $params |
||
| 210 | * order: criterio de ordenamiento |
||
| 211 | * fields: lista de campos |
||
| 212 | * join: joins de tablas |
||
| 213 | * group: agrupar campos |
||
| 214 | * having: condiciones de grupo |
||
| 215 | * limit: valor limit |
||
| 216 | * offset: valor offset |
||
| 217 | * |
||
| 218 | * @return \PDOStatement |
||
| 219 | */ |
||
| 220 | View Code Duplication | public static function allBy($field, $value, $params = []) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Cuenta los registros que coincidan con las condiciones indicadas. |
||
| 230 | * |
||
| 231 | * @param string $where condiciones |
||
| 232 | * @param array $values valores |
||
| 233 | * |
||
| 234 | * @return int |
||
| 235 | */ |
||
| 236 | View Code Duplication | public static function count($where = null, $values = null) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Paginar. |
||
| 250 | * |
||
| 251 | * @param array $params |
||
| 252 | * @param int $page numero de pagina |
||
| 253 | * @param int $perPage cantidad de items por pagina |
||
| 254 | * @param array $values valores |
||
| 255 | * |
||
| 256 | * @return Paginator |
||
| 257 | */ |
||
| 258 | public static function paginate(array $params, $page, $perPage, $values = null) |
||
| 259 | { |
||
| 260 | unset($params['limit'], $params['offset']); |
||
| 261 | $sql = QueryGenerator::select(static::getSource(), static::getDriver(), $params); |
||
| 262 | |||
| 263 | // Valores para consulta |
||
| 264 | if ($values !== null && !\is_array($values)) { |
||
| 265 | $values = \array_slice(func_get_args(), 3); |
||
| 266 | } |
||
| 267 | |||
| 268 | return new Paginator(\get_called_class(), $sql, (int) $page, (int) $perPage, $values); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Obtiene todos los registros de la consulta sql. |
||
| 273 | * |
||
| 274 | * @param string $sql |
||
| 275 | * @param string | array $values |
||
| 276 | * |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | View Code Duplication | public static function allBySql($sql, $values = null) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Obtiene el primer registro de la consulta sql. |
||
| 290 | * |
||
| 291 | * @param string $sql |
||
| 292 | * @param string | array $values |
||
| 293 | * |
||
| 294 | * @return array |
||
| 295 | */ |
||
| 296 | View Code Duplication | public static function firstBySql($sql, $values = null) |
|
| 304 | } |
||
| 305 |
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.