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 Give_DB_Donors 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 Give_DB_Donors, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Give_DB_Donors extends Give_DB { |
||
25 | |||
26 | /** |
||
27 | * Give_DB_Donors constructor. |
||
28 | * |
||
29 | * Set up the Give DB Donor class. |
||
30 | * |
||
31 | * @since 1.0 |
||
32 | * @access public |
||
33 | */ |
||
34 | public function __construct() { |
||
52 | |||
53 | /** |
||
54 | * Get columns and formats |
||
55 | * |
||
56 | * @since 1.0 |
||
57 | * @access public |
||
58 | * |
||
59 | * @return array Columns and formats. |
||
60 | */ |
||
61 | View Code Duplication | public function get_columns() { |
|
77 | |||
78 | /** |
||
79 | * Get default column values |
||
80 | * |
||
81 | * @since 1.0 |
||
82 | * @access public |
||
83 | * |
||
84 | * @return array Default column values. |
||
85 | */ |
||
86 | View Code Duplication | public function get_column_defaults() { |
|
101 | |||
102 | /** |
||
103 | * Add a donor |
||
104 | * |
||
105 | * @param array $data List of donor data to add. |
||
106 | * |
||
107 | * @since 1.0 |
||
108 | * @access public |
||
109 | * |
||
110 | * @return int|bool |
||
111 | */ |
||
112 | public function add( $data = array() ) { |
||
164 | |||
165 | |||
166 | /** |
||
167 | * Update a donor. |
||
168 | * |
||
169 | * |
||
170 | * @param int $row_id |
||
171 | * @param array $data |
||
172 | * @param string $where |
||
173 | * |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function update( $row_id, $data = array(), $where = '' ) { |
||
186 | |||
187 | /** |
||
188 | * Insert a donor. |
||
189 | * |
||
190 | * @param array $data |
||
191 | * @param string $type |
||
192 | * |
||
193 | * @return int |
||
194 | */ |
||
195 | public function insert( $data, $type = '' ) { |
||
204 | |||
205 | /** |
||
206 | * Delete a donor. |
||
207 | * |
||
208 | * NOTE: This should not be called directly as it does not make necessary changes to |
||
209 | * the payment meta and logs. Use give_donor_delete() instead. |
||
210 | * |
||
211 | * @param bool|string|int $_id_or_email ID or Email of Donor. |
||
212 | * |
||
213 | * @since 1.0 |
||
214 | * @access public |
||
215 | * |
||
216 | * @return bool|int |
||
217 | */ |
||
218 | public function delete( $_id_or_email = false ) { |
||
247 | |||
248 | /** |
||
249 | * Delete a donor by user ID. |
||
250 | * |
||
251 | * NOTE: This should not be called directly as it does not make necessary changes to |
||
252 | * the payment meta and logs. Use give_donor_delete() instead. |
||
253 | * |
||
254 | * @since 1.0 |
||
255 | * @access public |
||
256 | * |
||
257 | * @param int|bool $user_id |
||
258 | * |
||
259 | * @return bool|int |
||
260 | */ |
||
261 | public function delete_by_user_id( $user_id = false ) { |
||
282 | |||
283 | /** |
||
284 | * Checks if a donor exists |
||
285 | * |
||
286 | * @param string $value The value to search for. Default is empty. |
||
287 | * @param string $field The Donor ID or email to search in. Default is 'email'. |
||
288 | * |
||
289 | * @since 1.0 |
||
290 | * @access public |
||
291 | * |
||
292 | * @return bool True is exists, false otherwise. |
||
293 | */ |
||
294 | public function exists( $value = '', $field = 'email' ) { |
||
304 | |||
305 | /** |
||
306 | * Attaches a payment ID to a donor |
||
307 | * |
||
308 | * @since 1.0 |
||
309 | * @access public |
||
310 | * |
||
311 | * @param int $donor_id Donor ID. |
||
312 | * @param int $payment_id Payment ID. |
||
313 | * |
||
314 | * @return bool |
||
315 | */ |
||
316 | public function attach_payment( $donor_id = 0, $payment_id = 0 ) { |
||
328 | |||
329 | /** |
||
330 | * Removes a payment ID from a donor. |
||
331 | * |
||
332 | * @since 1.0 |
||
333 | * @access public |
||
334 | * |
||
335 | * @param int $donor_id Donor ID. |
||
336 | * @param int $payment_id Payment ID. |
||
337 | * |
||
338 | * @return bool |
||
339 | */ |
||
340 | public function remove_payment( $donor_id = 0, $payment_id = 0 ) { |
||
352 | |||
353 | /** |
||
354 | * Increments donor's donation stats. |
||
355 | * |
||
356 | * @access public |
||
357 | * |
||
358 | * @param int $donor_id Donor ID. |
||
359 | * @param float $amount THe amount to increase. |
||
360 | * |
||
361 | * @return bool |
||
362 | */ |
||
363 | View Code Duplication | public function increment_stats( $donor_id = 0, $amount = 0.00 ) { |
|
377 | |||
378 | /** |
||
379 | * Decrements donor's donation stats. |
||
380 | * |
||
381 | * @since 1.0 |
||
382 | * @access public |
||
383 | * |
||
384 | * @param int $donor_id Donor ID. |
||
385 | * @param float $amount Amount. |
||
386 | * |
||
387 | * @return bool |
||
388 | */ |
||
389 | View Code Duplication | public function decrement_stats( $donor_id = 0, $amount = 0.00 ) { |
|
403 | |||
404 | /** |
||
405 | * Retrieves a single donor from the database |
||
406 | * |
||
407 | * @since 1.0 |
||
408 | * @access public |
||
409 | * |
||
410 | * @param string $field ID or email. Default is 'id'. |
||
411 | * @param mixed $value The Customer ID or email to search. Default is 0. |
||
412 | * |
||
413 | * @return mixed Upon success, an object of the donor. Upon failure, NULL |
||
414 | */ |
||
415 | public function get_donor_by( $field = 'id', $value = 0 ) { |
||
490 | |||
491 | /** |
||
492 | * Retrieve donors from the database. |
||
493 | * |
||
494 | * @since 1.0 |
||
495 | * @access public |
||
496 | * |
||
497 | * @param array $args |
||
498 | * |
||
499 | * @return array|object|null Donors array or object. Null if not found. |
||
500 | */ |
||
501 | public function get_donors( $args = array() ) { |
||
509 | |||
510 | |||
511 | /** |
||
512 | * Count the total number of donors in the database |
||
513 | * |
||
514 | * @since 1.0 |
||
515 | * @access public |
||
516 | * |
||
517 | * @param array $args |
||
518 | * |
||
519 | * @return int Total number of donors. |
||
520 | */ |
||
521 | public function count( $args = array() ) { |
||
538 | |||
539 | /** |
||
540 | * Create the table |
||
541 | * |
||
542 | * @since 1.0 |
||
543 | * @access public |
||
544 | * |
||
545 | * @return void |
||
546 | */ |
||
547 | public function create_table() { |
||
573 | |||
574 | /** |
||
575 | * Add backward compatibility for old table name |
||
576 | * |
||
577 | * @since 2.0 |
||
578 | * @access private |
||
579 | * @global wpdb $wpdb |
||
580 | */ |
||
581 | private function bc_200_params() { |
||
592 | |||
593 | /** |
||
594 | * Add backward compatibility for deprecated param |
||
595 | * |
||
596 | * @since 1.8.14 |
||
597 | * @access private |
||
598 | * |
||
599 | * @param $args |
||
600 | */ |
||
601 | private function bc_1814_params( &$args ) { |
||
639 | } |
||
640 |
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.