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 | * @since 1.0 |
||
106 | * @access public |
||
107 | * |
||
108 | * @param array $data |
||
109 | * |
||
110 | * @return int|bool |
||
111 | */ |
||
112 | public function add( $data = array() ) { |
||
164 | |||
165 | /** |
||
166 | * Delete a donor. |
||
167 | * |
||
168 | * NOTE: This should not be called directly as it does not make necessary changes to |
||
169 | * the payment meta and logs. Use give_donor_delete() instead. |
||
170 | * |
||
171 | * @since 1.0 |
||
172 | * @access public |
||
173 | * |
||
174 | * @param bool|string|int $_id_or_email |
||
175 | * |
||
176 | * @return bool|int |
||
177 | */ |
||
178 | public function delete( $_id_or_email = false ) { |
||
205 | |||
206 | /** |
||
207 | * Delete a donor by user ID. |
||
208 | * |
||
209 | * NOTE: This should not be called directly as it does not make necessary changes to |
||
210 | * the payment meta and logs. Use give_donor_delete() instead. |
||
211 | * |
||
212 | * @since 1.0 |
||
213 | * @access public |
||
214 | * |
||
215 | * @param int|bool $user_id |
||
216 | * |
||
217 | * @return bool|int |
||
218 | */ |
||
219 | public function delete_by_user_id( $user_id = false ) { |
||
239 | |||
240 | /** |
||
241 | * Checks if a donor exists |
||
242 | * |
||
243 | * @since 1.0 |
||
244 | * @access public |
||
245 | * |
||
246 | * @param string $value The value to search for. Default is empty. |
||
247 | * @param string $field The Donor ID or email to search in. Default is 'email'. |
||
248 | * |
||
249 | * @return bool True is exists, false otherwise. |
||
250 | */ |
||
251 | public function exists( $value = '', $field = 'email' ) { |
||
261 | |||
262 | /** |
||
263 | * Attaches a payment ID to a donor |
||
264 | * |
||
265 | * @since 1.0 |
||
266 | * @access public |
||
267 | * |
||
268 | * @param int $donor_id Donor ID. |
||
269 | * @param int $payment_id Payment ID. |
||
270 | * |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function attach_payment( $donor_id = 0, $payment_id = 0 ) { |
||
285 | |||
286 | /** |
||
287 | * Removes a payment ID from a donor. |
||
288 | * |
||
289 | * @since 1.0 |
||
290 | * @access public |
||
291 | * |
||
292 | * @param int $donor_id Donor ID. |
||
293 | * @param int $payment_id Payment ID. |
||
294 | * |
||
295 | * @return bool |
||
296 | */ |
||
297 | public function remove_payment( $donor_id = 0, $payment_id = 0 ) { |
||
309 | |||
310 | /** |
||
311 | * Increments donor's donation stats. |
||
312 | * |
||
313 | * @access public |
||
314 | * |
||
315 | * @param int $donor_id Donor ID. |
||
316 | * @param float $amount THe amount to increase. |
||
317 | * |
||
318 | * @return bool |
||
319 | */ |
||
320 | View Code Duplication | public function increment_stats( $donor_id = 0, $amount = 0.00 ) { |
|
334 | |||
335 | /** |
||
336 | * Decrements donor's donation stats. |
||
337 | * |
||
338 | * @since 1.0 |
||
339 | * @access public |
||
340 | * |
||
341 | * @param int $donor_id Donor ID. |
||
342 | * @param float $amount Amount. |
||
343 | * |
||
344 | * @return bool |
||
345 | */ |
||
346 | View Code Duplication | public function decrement_stats( $donor_id = 0, $amount = 0.00 ) { |
|
360 | |||
361 | /** |
||
362 | * Updates the email address of a donor record when the email on a user is updated |
||
363 | * |
||
364 | * @since 1.4.3 |
||
365 | * @access public |
||
366 | * |
||
367 | * @param int $user_id User ID. |
||
368 | * @param WP_User|bool $old_user_data User data. |
||
369 | * |
||
370 | * @return bool |
||
371 | */ |
||
372 | public function update_donor_email_on_user_update( $user_id = 0, $old_user_data = false ) { |
||
419 | |||
420 | /** |
||
421 | * Retrieves a single donor from the database |
||
422 | * |
||
423 | * @since 1.0 |
||
424 | * @access public |
||
425 | * |
||
426 | * @param string $field ID or email. Default is 'id'. |
||
427 | * @param mixed $value The Customer ID or email to search. Default is 0. |
||
428 | * |
||
429 | * @return mixed Upon success, an object of the donor. Upon failure, NULL |
||
430 | */ |
||
431 | public function get_donor_by( $field = 'id', $value = 0 ) { |
||
506 | |||
507 | /** |
||
508 | * Retrieve donors from the database. |
||
509 | * |
||
510 | * @since 1.0 |
||
511 | * @access public |
||
512 | * |
||
513 | * @param array $args |
||
514 | * |
||
515 | * @return array|object|null Donors array or object. Null if not found. |
||
516 | */ |
||
517 | public function get_donors( $args = array() ) { |
||
534 | |||
535 | |||
536 | /** |
||
537 | * Count the total number of donors in the database |
||
538 | * |
||
539 | * @since 1.0 |
||
540 | * @access public |
||
541 | * |
||
542 | * @param array $args |
||
543 | * |
||
544 | * @return int Total number of donors. |
||
545 | */ |
||
546 | public function count( $args = array() ) { |
||
563 | |||
564 | /** |
||
565 | * Create the table |
||
566 | * |
||
567 | * @since 1.0 |
||
568 | * @access public |
||
569 | * |
||
570 | * @return void |
||
571 | */ |
||
572 | View Code Duplication | public function create_table() { |
|
598 | |||
599 | /** |
||
600 | * Check if the Customers table was ever installed |
||
601 | * |
||
602 | * @since 1.4.3 |
||
603 | * @access public |
||
604 | * |
||
605 | * @return bool Returns if the donors table was installed and upgrade routine run. |
||
606 | */ |
||
607 | public function installed() { |
||
610 | |||
611 | /** |
||
612 | * Add backward compatibility for deprecated param |
||
613 | * |
||
614 | * @since 1.8.14 |
||
615 | * @access private |
||
616 | * |
||
617 | * @param $args |
||
618 | */ |
||
619 | private function bc_1814_params( &$args ) { |
||
658 | } |
||
659 |
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.