Complex classes like Give_Donor 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_Donor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Give_Donor { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The donor ID |
||
| 28 | * |
||
| 29 | * @since 1.0 |
||
| 30 | * @access public |
||
| 31 | * |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | public $id = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The donor's donation count. |
||
| 38 | * |
||
| 39 | * @since 1.0 |
||
| 40 | * @access public |
||
| 41 | * |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | public $purchase_count = 0; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The donor's lifetime value. |
||
| 48 | * |
||
| 49 | * @since 1.0 |
||
| 50 | * @access public |
||
| 51 | * |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | public $purchase_value = 0; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The donor's email. |
||
| 58 | * |
||
| 59 | * @since 1.0 |
||
| 60 | * @access public |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | public $email; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The donor's emails. |
||
| 68 | * |
||
| 69 | * @since 1.7 |
||
| 70 | * @access public |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | public $emails; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The donor's name. |
||
| 78 | * |
||
| 79 | * @since 1.0 |
||
| 80 | * @access public |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | public $name; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The donor creation date. |
||
| 88 | * |
||
| 89 | * @since 1.0 |
||
| 90 | * @access public |
||
| 91 | * |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | public $date_created; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The payment IDs associated with the donor. |
||
| 98 | * |
||
| 99 | * @since 1.0 |
||
| 100 | * @access public |
||
| 101 | * |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | public $payment_ids; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The user ID associated with the donor. |
||
| 108 | * |
||
| 109 | * @since 1.0 |
||
| 110 | * @access public |
||
| 111 | * |
||
| 112 | * @var int |
||
| 113 | */ |
||
| 114 | public $user_id; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Donor notes saved by admins. |
||
| 118 | * |
||
| 119 | * @since 1.0 |
||
| 120 | * @access public |
||
| 121 | * |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | public $notes; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * The Database Abstraction |
||
| 128 | * |
||
| 129 | * @since 1.0 |
||
| 130 | * @access protected |
||
| 131 | * |
||
| 132 | * @var Give_DB_Donors |
||
| 133 | */ |
||
| 134 | protected $db; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Give_Donor constructor. |
||
| 138 | * |
||
| 139 | * @param bool $_id_or_email |
||
| 140 | * @param bool $by_user_id |
||
| 141 | */ |
||
| 142 | public function __construct( $_id_or_email = false, $by_user_id = false ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Setup Donor |
||
| 173 | * |
||
| 174 | * Set donor variables. |
||
| 175 | * |
||
| 176 | * @since 1.0 |
||
| 177 | * @access private |
||
| 178 | * |
||
| 179 | * @param object $donor The Donor Object. |
||
| 180 | * |
||
| 181 | * @return bool If the setup was successful or not. |
||
| 182 | */ |
||
| 183 | private function setup_donor( $donor ) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Magic __get function to dispatch a call to retrieve a private property. |
||
| 219 | * |
||
| 220 | * @since 1.0 |
||
| 221 | * @access public |
||
| 222 | * @param $key |
||
| 223 | * |
||
| 224 | * @return mixed|\WP_Error |
||
| 225 | */ |
||
| 226 | public function __get( $key ) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Creates a donor. |
||
| 243 | * |
||
| 244 | * @since 1.0 |
||
| 245 | * @access public |
||
| 246 | * |
||
| 247 | * @param array $data Array of attributes for a donor. |
||
| 248 | * |
||
| 249 | * @return bool|int False if not a valid creation, donor ID if user is found or valid creation. |
||
| 250 | */ |
||
| 251 | public function create( $data = array() ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Updates a donor record. |
||
| 311 | * |
||
| 312 | * @since 1.0 |
||
| 313 | * @access public |
||
| 314 | * |
||
| 315 | * @param array $data Array of data attributes for a donor (checked via whitelist). |
||
| 316 | * |
||
| 317 | * @return bool If the update was successful or not. |
||
| 318 | */ |
||
| 319 | public function update( $data = array() ) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Attach Payment |
||
| 363 | * |
||
| 364 | * Attach payment to the donor then triggers increasing stats. |
||
| 365 | * |
||
| 366 | * @since 1.0 |
||
| 367 | * @access public |
||
| 368 | * |
||
| 369 | * @param int $payment_id The payment ID to attach to the donor. |
||
| 370 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
| 371 | * |
||
| 372 | * @return bool If the attachment was successfully. |
||
| 373 | */ |
||
| 374 | public function attach_payment( $payment_id = 0, $update_stats = true ) { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Remove Payment |
||
| 442 | * |
||
| 443 | * Remove a payment from this donor, then triggers reducing stats. |
||
| 444 | * |
||
| 445 | * @since 1.0 |
||
| 446 | * @access public |
||
| 447 | * |
||
| 448 | * @param int $payment_id The Payment ID to remove. |
||
| 449 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
| 450 | * |
||
| 451 | * @return boolean If the removal was successful. |
||
| 452 | */ |
||
| 453 | public function remove_payment( $payment_id = 0, $update_stats = true ) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Increase the donation count of a donor. |
||
| 528 | * |
||
| 529 | * @since 1.0 |
||
| 530 | * @access public |
||
| 531 | * |
||
| 532 | * @param int $count The number to increase by. |
||
| 533 | * |
||
| 534 | * @return int The donation count. |
||
| 535 | */ |
||
| 536 | public function increase_purchase_count( $count = 1 ) { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Decrease the donor donation count. |
||
| 575 | * |
||
| 576 | * @since 1.0 |
||
| 577 | * @access public |
||
| 578 | * |
||
| 579 | * @param int $count The amount to decrease by. |
||
| 580 | * |
||
| 581 | * @return mixed If successful, the new count, otherwise false. |
||
| 582 | */ |
||
| 583 | public function decrease_donation_count( $count = 1 ) { |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Increase the donor's lifetime value. |
||
| 626 | * |
||
| 627 | * @since 1.0 |
||
| 628 | * @access public |
||
| 629 | * |
||
| 630 | * @param float $value The value to increase by. |
||
| 631 | * |
||
| 632 | * @return mixed If successful, the new value, otherwise false. |
||
| 633 | */ |
||
| 634 | public function increase_value( $value = 0.00 ) { |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Decrease a donor's lifetime value. |
||
| 668 | * |
||
| 669 | * @since 1.0 |
||
| 670 | * @access public |
||
| 671 | * |
||
| 672 | * @param float $value The value to decrease by. |
||
| 673 | * |
||
| 674 | * @return mixed If successful, the new value, otherwise false. |
||
| 675 | */ |
||
| 676 | public function decrease_value( $value = 0.00 ) { |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Decrease/Increase a donor's lifetime value. |
||
| 714 | * |
||
| 715 | * This function will update donation stat on basis of current amount and new amount donation difference. |
||
| 716 | * Difference value can positive or negative. Negative value will decrease user donation stat while positive value increase donation stat. |
||
| 717 | * |
||
| 718 | * @since 1.0 |
||
| 719 | * @access public |
||
| 720 | * |
||
| 721 | * @param float $curr_amount Current Donation amount. |
||
| 722 | * @param float $new_amount New (changed) Donation amount. |
||
| 723 | * |
||
| 724 | * @return mixed If successful, the new donation stat value, otherwise false. |
||
| 725 | */ |
||
| 726 | public function update_donation_value( $curr_amount, $new_amount ) { |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Get the parsed notes for a donor as an array. |
||
| 752 | * |
||
| 753 | * @since 1.0 |
||
| 754 | * @access public |
||
| 755 | * |
||
| 756 | * @param int $length The number of notes to get. |
||
| 757 | * @param int $paged What note to start at. |
||
| 758 | * |
||
| 759 | * @return array The notes requested. |
||
| 760 | */ |
||
| 761 | public function get_notes( $length = 20, $paged = 1 ) { |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Get the total number of notes we have after parsing. |
||
| 777 | * |
||
| 778 | * @since 1.0 |
||
| 779 | * @access public |
||
| 780 | * |
||
| 781 | * @return int The number of notes for the donor. |
||
| 782 | */ |
||
| 783 | public function get_notes_count() { |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Add a note for the donor. |
||
| 794 | * |
||
| 795 | * @since 1.0 |
||
| 796 | * @access public |
||
| 797 | * |
||
| 798 | * @param string $note The note to add. Default is empty. |
||
| 799 | * |
||
| 800 | * @return string|boolean The new note if added successfully, false otherwise. |
||
| 801 | */ |
||
| 802 | public function add_note( $note = '' ) { |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Get the notes column for the donor |
||
| 853 | * |
||
| 854 | * @since 1.0 |
||
| 855 | * @access private |
||
| 856 | * |
||
| 857 | * @return string The Notes for the donor, non-parsed. |
||
| 858 | */ |
||
| 859 | private function get_raw_notes() { |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Retrieve a meta field for a donor. |
||
| 869 | * |
||
| 870 | * @since 1.6 |
||
| 871 | * @access public |
||
| 872 | * |
||
| 873 | * @param string $meta_key The meta key to retrieve. Default is empty. |
||
| 874 | * @param bool $single Whether to return a single value. Default is true. |
||
| 875 | * |
||
| 876 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. |
||
| 877 | */ |
||
| 878 | public function get_meta( $meta_key = '', $single = true ) { |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Add a meta data field to a donor. |
||
| 884 | * |
||
| 885 | * @since 1.6 |
||
| 886 | * @access public |
||
| 887 | * |
||
| 888 | * @param string $meta_key Metadata name. Default is empty. |
||
| 889 | * @param mixed $meta_value Metadata value. |
||
| 890 | * @param bool $unique Optional. Whether the same key should not be added. Default is false. |
||
| 891 | * |
||
| 892 | * @return bool False for failure. True for success. |
||
| 893 | */ |
||
| 894 | public function add_meta( $meta_key = '', $meta_value, $unique = false ) { |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Update a meta field based on donor ID. |
||
| 900 | * |
||
| 901 | * @since 1.6 |
||
| 902 | * @access public |
||
| 903 | * |
||
| 904 | * @param string $meta_key Metadata key. Default is empty. |
||
| 905 | * @param mixed $meta_value Metadata value. |
||
| 906 | * @param mixed $prev_value Optional. Previous value to check before removing. Default is empty. |
||
| 907 | * |
||
| 908 | * @return bool False on failure, true if success. |
||
| 909 | */ |
||
| 910 | public function update_meta( $meta_key = '', $meta_value, $prev_value = '' ) { |
||
| 913 | |||
| 914 | /** |
||
| 915 | * Remove metadata matching criteria from a donor. |
||
| 916 | * |
||
| 917 | * @since 1.6 |
||
| 918 | * @access public |
||
| 919 | * |
||
| 920 | * @param string $meta_key Metadata name. Default is empty. |
||
| 921 | * @param mixed $meta_value Optional. Metadata value. Default is empty. |
||
| 922 | * |
||
| 923 | * @return bool False for failure. True for success. |
||
| 924 | */ |
||
| 925 | public function delete_meta( $meta_key = '', $meta_value = '' ) { |
||
| 928 | |||
| 929 | /** |
||
| 930 | * Sanitize the data for update/create |
||
| 931 | * |
||
| 932 | * @since 1.0 |
||
| 933 | * @access private |
||
| 934 | * |
||
| 935 | * @param array $data The data to sanitize. |
||
| 936 | * |
||
| 937 | * @return array The sanitized data, based off column defaults. |
||
| 938 | */ |
||
| 939 | private function sanitize_columns( $data ) { |
||
| 991 | |||
| 992 | /** |
||
| 993 | * Attach an email to the donor |
||
| 994 | * |
||
| 995 | * @since 1.7 |
||
| 996 | * @access public |
||
| 997 | * |
||
| 998 | * @param string $email The email address to attach to the donor |
||
| 999 | * @param bool $primary Allows setting the email added as the primary |
||
| 1000 | * |
||
| 1001 | * @return bool If the email was added successfully |
||
| 1002 | */ |
||
| 1003 | public function add_email( $email = '', $primary = false ) { |
||
| 1034 | |||
| 1035 | /** |
||
| 1036 | * Remove an email from the donor. |
||
| 1037 | * |
||
| 1038 | * @since 1.7 |
||
| 1039 | * @access public |
||
| 1040 | * |
||
| 1041 | * @param string $email The email address to remove from the donor. |
||
| 1042 | * |
||
| 1043 | * @return bool If the email was removed successfully. |
||
| 1044 | */ |
||
| 1045 | public function remove_email( $email = '' ) { |
||
| 1058 | |||
| 1059 | /** |
||
| 1060 | * Set an email address as the donor's primary email. |
||
| 1061 | * |
||
| 1062 | * This will move the donor's previous primary email to an additional email. |
||
| 1063 | * |
||
| 1064 | * @since 1.7 |
||
| 1065 | * @access public |
||
| 1066 | * |
||
| 1067 | * @param string $new_primary_email The email address to remove from the donor. |
||
| 1068 | * |
||
| 1069 | * @return bool If the email was set as primary successfully. |
||
| 1070 | */ |
||
| 1071 | public function set_primary_email( $new_primary_email = '' ) { |
||
| 1106 | } |
||
| 1107 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.