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:
Complex classes like ActiveRecord often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ActiveRecord, and based on these observations, apply Extract Interface, too.
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 | static public function resolver($rs, $obj){ |
||
46 | |||
47 | View Code Duplication | static public function hasMany($name, $class, $via = NULL){ |
|
56 | |||
57 | View Code Duplication | static public function hasOne($name, $class, $via = NULL){ |
|
66 | |||
67 | public function jsonSerialize(){ |
||
70 | |||
71 | public function __get($name){ |
||
82 | |||
83 | static protected function getRelationship($rel){ |
||
88 | |||
89 | public function populate($rel){ |
||
93 | |||
94 | /** |
||
95 | * Actualizar registros. |
||
96 | * |
||
97 | * @param array $fields |
||
98 | * @param string $where condiciones |
||
99 | * @param array $values valores para condiciones |
||
100 | * |
||
101 | * @return int numero de registros actualizados |
||
102 | */ |
||
103 | View Code Duplication | public static function updateAll(array $fields, $where = null, array $values = []) |
|
114 | |||
115 | /** |
||
116 | * Eliminar registro. |
||
117 | * |
||
118 | * @param string $where condiciones |
||
119 | * @param array |string $values valores |
||
120 | * |
||
121 | * @return int numero de registros eliminados |
||
122 | */ |
||
123 | public static function deleteAll($where = null, $values = null) |
||
131 | |||
132 | /** |
||
133 | * Elimina caracteres que podrian ayudar a ejecutar |
||
134 | * un ataque de Inyeccion SQL. |
||
135 | * |
||
136 | * @param string $sqlItem |
||
137 | * |
||
138 | * @return string |
||
139 | * @throw KumbiaException |
||
140 | */ |
||
141 | public static function sqlItemSanitize($sqlItem) |
||
153 | |||
154 | /** |
||
155 | * Obtener la primera coincidencia por el campo indicado. |
||
156 | * |
||
157 | * @param string $field campo |
||
158 | * @param string $value valor |
||
159 | * @param array $params parametros adicionales |
||
160 | * order: criterio de ordenamiento |
||
161 | * fields: lista de campos |
||
162 | * join: joins de tablas |
||
163 | * group: agrupar campos |
||
164 | * having: condiciones de grupo |
||
165 | * offset: valor offset |
||
166 | * |
||
167 | * @return ActiveRecord |
||
168 | */ |
||
169 | View Code Duplication | public static function firstBy($field, $value, $params = []) |
|
176 | |||
177 | /** |
||
178 | * Obtener la primera coincidencia de las condiciones indicadas. |
||
179 | * |
||
180 | * @param array $params parametros de bus |
||
181 | * @param string $field campo |
||
182 | * @param string $value valor |
||
183 | * @param array $params parametros adicionales |
||
184 | * order: criterio de ordenamiento |
||
185 | * fields: lista de campos |
||
186 | * group: agrupar campos |
||
187 | * join: joins de tablas |
||
188 | * having: condiciones de grupo |
||
189 | * offset: valor offset queda |
||
190 | * @param array $values valores de busqueda |
||
191 | * |
||
192 | * @return ActiveRecord |
||
193 | */ |
||
194 | public static function first($params = [], $values = []) |
||
203 | |||
204 | /** |
||
205 | * Obtener todos los registros. |
||
206 | * |
||
207 | * @param array $params |
||
208 | * where: condiciones where |
||
209 | * order: criterio de ordenamiento |
||
210 | * fields: lista de campos |
||
211 | * join: joins de tablas |
||
212 | * group: agrupar campos |
||
213 | * having: condiciones de grupo |
||
214 | * limit: valor limit |
||
215 | * offset: valor offset |
||
216 | * @param array $values valores de busqueda |
||
217 | * |
||
218 | * @return \PDOStatement |
||
219 | */ |
||
220 | public static function all($params = [], $values = []) |
||
226 | |||
227 | /** |
||
228 | * Do a query. |
||
229 | * |
||
230 | * @param array $array params of query |
||
231 | * |
||
232 | * @return \PDOStatement|false |
||
233 | */ |
||
234 | protected static function doQuery(array $array) |
||
243 | |||
244 | /** |
||
245 | * Retorna los parametros para el doQuery. |
||
246 | * |
||
247 | * @param array $array |
||
248 | * |
||
249 | * @return array |
||
250 | */ |
||
251 | protected static function getParam(array &$array) |
||
257 | |||
258 | /** |
||
259 | * Retorna los values para el doQuery. |
||
260 | * |
||
261 | * @param array $array |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | protected static function getValues(array $array) |
||
270 | |||
271 | /** |
||
272 | * Obtener todas las coincidencias por el campo indicado. |
||
273 | * |
||
274 | * @param string $field campo |
||
275 | * @param string $value valor |
||
276 | * @param array $params |
||
277 | * order: criterio de ordenamiento |
||
278 | * fields: lista de campos |
||
279 | * join: joins de tablas |
||
280 | * group: agrupar campos |
||
281 | * having: condiciones de grupo |
||
282 | * limit: valor limit |
||
283 | * offset: valor offset |
||
284 | * |
||
285 | * @return \PDOStatement |
||
286 | */ |
||
287 | View Code Duplication | public static function allBy($field, $value, $params = []) |
|
294 | |||
295 | /** |
||
296 | * Cuenta los registros que coincidan con las condiciones indicadas. |
||
297 | * |
||
298 | * @param string $where condiciones |
||
299 | * @param array $values valores |
||
300 | * |
||
301 | * @return int |
||
302 | */ |
||
303 | View Code Duplication | public static function count($where = null, $values = null) |
|
314 | |||
315 | /** |
||
316 | * Paginar. |
||
317 | * |
||
318 | * @param array $params |
||
319 | * @param int $page numero de pagina |
||
320 | * @param int $perPage cantidad de items por pagina |
||
321 | * @param array $values valores |
||
322 | * |
||
323 | * @return Paginator |
||
324 | */ |
||
325 | public static function paginate(array $params, $page, $perPage, $values = null) |
||
337 | |||
338 | /** |
||
339 | * Obtiene todos los registros de la consulta sql. |
||
340 | * |
||
341 | * @param string $sql |
||
342 | * @param string | array $values |
||
343 | * |
||
344 | * @return array |
||
345 | */ |
||
346 | View Code Duplication | public static function allBySql($sql, $values = null) |
|
354 | |||
355 | /** |
||
356 | * Obtiene el primer registro de la consulta sql. |
||
357 | * |
||
358 | * @param string $sql |
||
359 | * @param string | array $values |
||
360 | * |
||
361 | * @return array |
||
362 | */ |
||
363 | View Code Duplication | public static function firstBySql($sql, $values = null) |
|
371 | } |
||
372 |
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.