Complex classes like Give_Customer 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_Customer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Give_Customer { |
||
25 | |||
26 | /** |
||
27 | * The customer ID |
||
28 | * |
||
29 | * @since 1.0 |
||
30 | * @access public |
||
31 | * |
||
32 | * @var int |
||
33 | */ |
||
34 | public $id = 0; |
||
35 | |||
36 | /** |
||
37 | * The customer's purchase count |
||
38 | * |
||
39 | * @since 1.0 |
||
40 | * @access public |
||
41 | * |
||
42 | * @var int |
||
43 | */ |
||
44 | public $purchase_count = 0; |
||
45 | |||
46 | /** |
||
47 | * The customer'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 customer's email |
||
58 | * |
||
59 | * @since 1.0 |
||
60 | * @access public |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | public $email; |
||
65 | |||
66 | /** |
||
67 | * The customer's name |
||
68 | * |
||
69 | * @since 1.0 |
||
70 | * @access public |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | public $name; |
||
75 | |||
76 | /** |
||
77 | * The customer's creation date |
||
78 | * |
||
79 | * @since 1.0 |
||
80 | * @access public |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | public $date_created; |
||
85 | |||
86 | /** |
||
87 | * The payment IDs associated with the customer |
||
88 | * |
||
89 | * @since 1.0 |
||
90 | * @access public |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | public $payment_ids; |
||
95 | |||
96 | /** |
||
97 | * The user ID associated with the customer |
||
98 | * |
||
99 | * @since 1.0 |
||
100 | 52 | * @access public |
|
101 | * |
||
102 | 52 | * @var int |
|
103 | */ |
||
104 | 52 | public $user_id; |
|
105 | |||
106 | /** |
||
107 | * Customer Notes |
||
108 | 52 | * |
|
109 | * @since 1.0 |
||
110 | 52 | * @access public |
|
111 | 52 | * |
|
112 | 52 | * @var string |
|
113 | 52 | */ |
|
114 | public $notes; |
||
115 | |||
116 | 52 | /** |
|
117 | * The Database Abstraction |
||
118 | 52 | * |
|
119 | 52 | * @since 1.0 |
|
120 | * @access protected |
||
121 | * |
||
122 | 52 | * @var Give_DB_Customers |
|
123 | */ |
||
124 | 52 | protected $db; |
|
125 | |||
126 | /** |
||
127 | * Class Constructor |
||
128 | * |
||
129 | * Set up the Give Customer Class. |
||
130 | * |
||
131 | * @since 1.0 |
||
132 | * @access public |
||
133 | * |
||
134 | * @param bool $_id_or_email |
||
135 | 52 | * @param bool $by_user_id |
|
136 | */ |
||
137 | 52 | public function __construct( $_id_or_email = false, $by_user_id = false ) { |
|
162 | |||
163 | /** |
||
164 | * Setup Customer |
||
165 | * |
||
166 | * Given the customer data, let's set the variables. |
||
167 | * |
||
168 | * @since 1.0 |
||
169 | * @access private |
||
170 | * |
||
171 | 1 | * @param object $customer The Customer Object. |
|
172 | * |
||
173 | 1 | * @return bool If the setup was successful or not. |
|
174 | */ |
||
175 | private function setup_customer( $customer ) { |
||
205 | 53 | ||
206 | 53 | /** |
|
207 | * Magic __get function to dispatch a call to retrieve a private property. |
||
208 | 53 | * |
|
209 | 1 | * @since 1.0 |
|
210 | * @access public |
||
211 | */ |
||
212 | 53 | public function __get( $key ) { |
|
226 | |||
227 | 53 | /** |
|
228 | * Creates a customer |
||
229 | 53 | * |
|
230 | 53 | * @since 1.0 |
|
231 | * @access public |
||
232 | 53 | * |
|
233 | * @param array $data Array of attributes for a customer. |
||
234 | 53 | * |
|
235 | * @return mixed False if not a valid creation, Customer ID if user is found or valid creation. |
||
236 | */ |
||
237 | public function create( $data = array() ) { |
||
279 | |||
280 | /** |
||
281 | * Update a customer record |
||
282 | * |
||
283 | 52 | * @since 1.0 |
|
284 | * @access public |
||
285 | 52 | * |
|
286 | 1 | * @param array $data Array of data attributes for a customer (checked via whitelist). |
|
287 | * |
||
288 | * @return bool If the update was successful or not. |
||
289 | 52 | */ |
|
290 | public function update( $data = array() ) { |
||
314 | |||
315 | /** |
||
316 | 52 | * Attach Payment |
|
317 | 1 | * |
|
318 | * Attach payment to the customer then triggers increasing stats. |
||
319 | 1 | * |
|
320 | * @since 1.0 |
||
321 | * @access public |
||
322 | * |
||
323 | 1 | * @param int $payment_id The payment ID to attach to the customer. |
|
324 | 1 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
|
325 | * |
||
326 | 52 | * @return bool If the attachment was successfuly. |
|
327 | */ |
||
328 | 52 | public function attach_payment( $payment_id = 0, $update_stats = true ) { |
|
377 | |||
378 | 3 | /** |
|
379 | * Remove Payment |
||
380 | 3 | * |
|
381 | * Remove a payment from this customer, then triggers reducing stats. |
||
382 | 3 | * |
|
383 | * @since 1.0 |
||
384 | * @access public |
||
385 | * |
||
386 | * @param integer $payment_id The Payment ID to remove. |
||
387 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
388 | * |
||
389 | * @return boolean If the removal was successful. |
||
390 | */ |
||
391 | public function remove_payment( $payment_id = 0, $update_stats = true ) { |
||
447 | |||
448 | 6 | /** |
|
449 | 1 | * Increase the purchase count of a customer. |
|
450 | 1 | * |
|
451 | * @since 1.0 |
||
452 | 6 | * @access public |
|
453 | * |
||
454 | 6 | * @param integer $count The number to increment by. |
|
455 | 6 | * |
|
456 | 6 | * @return int The purchase count. |
|
457 | */ |
||
458 | 6 | public function increase_purchase_count( $count = 1 ) { |
|
477 | |||
478 | 42 | /** |
|
479 | 41 | * Decrease the customer purchase count. |
|
480 | 41 | * |
|
481 | * @since 1.0 |
||
482 | 42 | * @access public |
|
483 | * |
||
484 | 42 | * @param integer $count The amount to decrease by. |
|
485 | * |
||
486 | * @return mixed If successful, the new count, otherwise false. |
||
487 | */ |
||
488 | public function decrease_purchase_count( $count = 1 ) { |
||
511 | |||
512 | 7 | /** |
|
513 | * Increase the customer's lifetime value. |
||
514 | * |
||
515 | * @since 1.0 |
||
516 | * @access public |
||
517 | * |
||
518 | * @param float $value The value to increase by. |
||
519 | * |
||
520 | * @return mixed If successful, the new value, otherwise false. |
||
521 | */ |
||
522 | public function increase_value( $value = 0.00 ) { |
||
536 | |||
537 | /** |
||
538 | * Decrease a customer's lifetime value. |
||
539 | * |
||
540 | * @since 1.0 |
||
541 | * @access public |
||
542 | * |
||
543 | * @param float $value The value to decrease by. |
||
544 | * |
||
545 | 1 | * @return mixed If successful, the new value, otherwise false. |
|
546 | */ |
||
547 | 1 | public function decrease_value( $value = 0.00 ) { |
|
565 | 1 | ||
566 | 1 | /** |
|
567 | * Decrease/Increase a customer's lifetime value. |
||
568 | * |
||
569 | * This function will update donation stat on basis of current amount and new amount donation difference. |
||
570 | 1 | * Difference value can positive or negative. Negative value will decrease user donation stat while positive value increase donation stat. |
|
571 | * |
||
572 | 1 | * @since 1.0 |
|
573 | 1 | * @access public |
|
574 | 1 | * |
|
575 | * @param float $curr_amount Current Donation amount. |
||
576 | 1 | * @param float $new_amount New (changed) Donation amount. |
|
577 | 1 | * |
|
578 | 1 | * @return mixed If successful, the new donation stat value, otherwise false. |
|
579 | */ |
||
580 | 1 | public function update_donation_value( $curr_amount, $new_amount ) { |
|
604 | |||
605 | 52 | /** |
|
606 | * Get the parsed notes for a customer as an array. |
||
607 | * |
||
608 | * @since 1.0 |
||
609 | * @access public |
||
610 | * |
||
611 | * @param integer $length The number of notes to get. |
||
612 | * @param integer $paged What note to start at. |
||
613 | * |
||
614 | * @return array The notes requested. |
||
615 | */ |
||
616 | public function get_notes( $length = 20, $paged = 1 ) { |
||
629 | |||
630 | /** |
||
631 | * Get the total number of notes we have after parsing. |
||
632 | 52 | * |
|
633 | 52 | * @since 1.0 |
|
634 | 52 | * @access public |
|
635 | 52 | * |
|
636 | 1 | * @return int The number of notes for the customer. |
|
637 | 1 | */ |
|
638 | 52 | public function get_notes_count() { |
|
646 | 52 | ||
647 | /** |
||
648 | 52 | * Add a note for the customer. |
|
649 | * |
||
650 | 42 | * @since 1.0 |
|
651 | * @access public |
||
652 | 42 | * |
|
653 | * @param string $note The note to add. Default is empty. |
||
654 | 42 | * |
|
655 | * @return string|boolean The new note if added successfully, false otherwise. |
||
656 | */ |
||
657 | 42 | public function add_note( $note = '' ) { |
|
688 | |||
689 | /** |
||
690 | * Get the notes column for the customer |
||
691 | * |
||
692 | * @since 1.0 |
||
693 | * @access private |
||
694 | * |
||
695 | * @return string The Notes for the customer, non-parsed. |
||
696 | */ |
||
697 | private function get_raw_notes() { |
||
704 | |||
705 | /** |
||
706 | * Retrieve customer meta field for a customer. |
||
707 | * |
||
708 | * @since 1.6 |
||
709 | * @access public |
||
710 | * |
||
711 | * @param string $meta_key The meta key to retrieve. Default is empty. |
||
712 | * @param bool $single Whether to return a single value. Default is true. |
||
713 | * |
||
714 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. |
||
715 | */ |
||
716 | public function get_meta( $meta_key = '', $single = true ) { |
||
719 | |||
720 | /** |
||
721 | * Add meta data field to a customer. |
||
722 | * |
||
723 | * @since 1.6 |
||
724 | * @access public |
||
725 | * |
||
726 | * @param string $meta_key Metadata name. Default is empty. |
||
727 | * @param mixed $meta_value Metadata value. |
||
728 | * @param bool $unique Optional. Whether the same key should not be added. Default is false. |
||
729 | * |
||
730 | * @return bool False for failure. True for success. |
||
731 | */ |
||
732 | public function add_meta( $meta_key = '', $meta_value, $unique = false ) { |
||
735 | |||
736 | /** |
||
737 | * Update customer meta field based on customer ID. |
||
738 | * |
||
739 | * @since 1.6 |
||
740 | * @access public |
||
741 | * |
||
742 | * @param string $meta_key Metadata key. Default is empty. |
||
743 | * @param mixed $meta_value Metadata value. |
||
744 | * @param mixed $prev_value Optional. Previous value to check before removing. Default is empty. |
||
745 | * |
||
746 | * @return bool False on failure, true if success. |
||
747 | */ |
||
748 | public function update_meta( $meta_key = '', $meta_value, $prev_value = '' ) { |
||
751 | |||
752 | /** |
||
753 | * Remove metadata matching criteria from a customer. |
||
754 | * |
||
755 | * @since 1.6 |
||
756 | * @access public |
||
757 | * |
||
758 | * @param string $meta_key Metadata name. Default is empty. |
||
759 | * @param mixed $meta_value Optional. Metadata value. Default is empty. |
||
760 | * |
||
761 | * @return bool False for failure. True for success. |
||
762 | */ |
||
763 | public function delete_meta( $meta_key = '', $meta_value = '' ) { |
||
766 | |||
767 | /** |
||
768 | * Sanitize the data for update/create |
||
769 | * |
||
770 | * @since 1.0 |
||
771 | * @access private |
||
772 | * |
||
773 | * @param array $data The data to sanitize. |
||
774 | * |
||
775 | * @return array The sanitized data, based off column defaults. |
||
776 | */ |
||
777 | private function sanitize_columns( $data ) { |
||
830 | |||
831 | } |
||
832 |
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.