Complex classes like Give_Donate_Form 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_Donate_Form, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Give_Donate_Form { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The donation ID |
||
| 21 | * |
||
| 22 | * @since 1.0 |
||
| 23 | */ |
||
| 24 | public $ID = 0; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The donation price |
||
| 28 | * |
||
| 29 | * @since 1.0 |
||
| 30 | */ |
||
| 31 | private $price; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The minimum donation price |
||
| 35 | * |
||
| 36 | * @since 1.3.6 |
||
| 37 | */ |
||
| 38 | private $minimum_price; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The donation goal |
||
| 42 | * |
||
| 43 | * @since 1.0 |
||
| 44 | */ |
||
| 45 | private $goal; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The donation prices, if Price Levels are enabled |
||
| 49 | * |
||
| 50 | * @since 1.0 |
||
| 51 | */ |
||
| 52 | private $prices; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The form's sale count |
||
| 56 | * |
||
| 57 | * @since 1.0 |
||
| 58 | */ |
||
| 59 | private $sales; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The form's total earnings |
||
| 63 | * |
||
| 64 | * @since 1.0 |
||
| 65 | */ |
||
| 66 | private $earnings; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Declare the default properties in WP_Post as we can't extend it |
||
| 70 | * Anything we've declared above has been removed. |
||
| 71 | */ |
||
| 72 | public $post_author = 0; |
||
| 73 | public $post_date = '0000-00-00 00:00:00'; |
||
| 74 | public $post_date_gmt = '0000-00-00 00:00:00'; |
||
| 75 | public $post_content = ''; |
||
| 76 | public $post_title = ''; |
||
| 77 | public $post_excerpt = ''; |
||
| 78 | public $post_status = 'publish'; |
||
| 79 | public $comment_status = 'open'; |
||
| 80 | public $ping_status = 'open'; |
||
| 81 | public $post_password = ''; |
||
| 82 | public $post_name = ''; |
||
| 83 | public $to_ping = ''; |
||
| 84 | public $pinged = ''; |
||
| 85 | public $post_modified = '0000-00-00 00:00:00'; |
||
| 86 | public $post_modified_gmt = '0000-00-00 00:00:00'; |
||
| 87 | public $post_content_filtered = ''; |
||
| 88 | public $post_parent = 0; |
||
| 89 | public $guid = ''; |
||
| 90 | public $menu_order = 0; |
||
| 91 | public $post_mime_type = ''; |
||
| 92 | public $comment_count = 0; |
||
| 93 | public $filter; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Give_Donate_Form constructor. |
||
| 97 | * |
||
| 98 | * @since 1.0 |
||
| 99 | * |
||
| 100 | * @param bool $_id |
||
| 101 | * @param array $_args |
||
| 102 | */ |
||
| 103 | 57 | public function __construct( $_id = false, $_args = array() ) { |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Given the donation form data, let's set the variables |
||
| 113 | * |
||
| 114 | * @since 1.5 |
||
| 115 | * |
||
| 116 | * @param object $donation_form The Donation Form Object |
||
| 117 | * |
||
| 118 | * @return bool If the setup was successful or not |
||
| 119 | */ |
||
| 120 | 57 | private function setup_donation_form( $donation_form ) { |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Magic __get function to dispatch a call to retrieve a private property |
||
| 152 | * |
||
| 153 | * @since 1.0 |
||
| 154 | * |
||
| 155 | * @param $key |
||
| 156 | * |
||
| 157 | * @return mixed |
||
| 158 | * @throws Exception |
||
| 159 | */ |
||
| 160 | 53 | public function __get( $key ) { |
|
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * Creates a donation form |
||
| 177 | * |
||
| 178 | * @since 1.5 |
||
| 179 | * |
||
| 180 | * @param array $data Array of attributes for a donation form |
||
| 181 | * |
||
| 182 | * @return mixed false if data isn't passed and class not instantiated for creation, or New Form ID |
||
| 183 | */ |
||
| 184 | public function create( $data = array() ) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Retrieve the ID |
||
| 223 | * |
||
| 224 | * @since 1.0 |
||
| 225 | * @return int |
||
| 226 | */ |
||
| 227 | 1 | public function get_ID() { |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Retrieve the donation form name |
||
| 235 | * |
||
| 236 | * @since 1.5 |
||
| 237 | * @return string Name of the donation form |
||
| 238 | */ |
||
| 239 | public function get_name() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Retrieve the price |
||
| 245 | * |
||
| 246 | * @since 1.0 |
||
| 247 | * @return float |
||
| 248 | */ |
||
| 249 | 15 | public function get_price() { |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Retrieve the minimum price |
||
| 280 | * |
||
| 281 | * @since 1.3.6 |
||
| 282 | * @return float |
||
| 283 | */ |
||
| 284 | 2 | public function get_minimum_price() { |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Retrieve the variable prices |
||
| 308 | * |
||
| 309 | * @since 1.0 |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | 13 | public function get_prices() { |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Retrieve the goal |
||
| 334 | * |
||
| 335 | * @since 1.0 |
||
| 336 | * @return float |
||
| 337 | */ |
||
| 338 | 2 | public function get_goal() { |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Determine if single price mode is enabled or disabled |
||
| 362 | * |
||
| 363 | * @since 1.0 |
||
| 364 | * @return bool |
||
| 365 | */ |
||
| 366 | public function is_single_price_mode() { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Has Variable Prices |
||
| 389 | * |
||
| 390 | * @description Determine if the donation form has variable prices enabled |
||
| 391 | * |
||
| 392 | * @since 1.0 |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | 53 | public function has_variable_prices() { |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Retrieve the donation form type, set or multi-level |
||
| 416 | * |
||
| 417 | * @since 1.5 |
||
| 418 | * @return string Type of donation form, either 'set' or 'multi' |
||
| 419 | */ |
||
| 420 | public function get_type() { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Retrieve the sale count for the donation form |
||
| 438 | * |
||
| 439 | * @since 1.0 |
||
| 440 | * @return int |
||
| 441 | */ |
||
| 442 | 52 | public function get_sales() { |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Increment the sale count by one |
||
| 465 | * |
||
| 466 | * @since 1.0 |
||
| 467 | * |
||
| 468 | * @param int $quantity The quantity to increase the donations by |
||
| 469 | * |
||
| 470 | * @return int|false New number of total sales |
||
| 471 | */ |
||
| 472 | 42 | public function increase_sales( $quantity = 1 ) { |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Decrement the sale count by one |
||
| 491 | * |
||
| 492 | * @since 1.0 |
||
| 493 | * |
||
| 494 | * @param int $quantity The quantity to decrease by |
||
| 495 | * |
||
| 496 | * @return int|false New number of total sales |
||
| 497 | */ |
||
| 498 | 31 | public function decrease_sales( $quantity = 1 ) { |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Retrieve the total earnings for the form |
||
| 524 | * |
||
| 525 | * @since 1.0 |
||
| 526 | * @return float |
||
| 527 | */ |
||
| 528 | 53 | public function get_earnings() { |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Increase the earnings by the given amount |
||
| 551 | * |
||
| 552 | * @since 1.0 |
||
| 553 | * @return float|false |
||
| 554 | */ |
||
| 555 | 42 | public function increase_earnings( $amount = 0 ) { |
|
| 571 | |||
| 572 | /** |
||
| 573 | * Decrease the earnings by the given amount |
||
| 574 | * |
||
| 575 | * @since 1.0 |
||
| 576 | * @return float|false |
||
| 577 | */ |
||
| 578 | 31 | public function decrease_earnings( $amount ) { |
|
| 600 | |||
| 601 | /** |
||
| 602 | * Determine if the donation is free or if the given price ID is free |
||
| 603 | * |
||
| 604 | * @since 1.0 |
||
| 605 | * @return bool |
||
| 606 | */ |
||
| 607 | public function is_free( $price_id = false ) { |
||
| 625 | |||
| 626 | |||
| 627 | /** |
||
| 628 | * Updates a single meta entry for the donation form |
||
| 629 | * |
||
| 630 | * @since 1.5 |
||
| 631 | * @access private |
||
| 632 | * |
||
| 633 | * @param string $meta_key The meta_key to update |
||
| 634 | * @param string|array|object $meta_value The value to put into the meta |
||
| 635 | * |
||
| 636 | * @return bool The result of the update query |
||
| 637 | */ |
||
| 638 | 51 | private function update_meta( $meta_key = '', $meta_value = '' ) { |
|
| 667 | |||
| 668 | |||
| 669 | } |
||
| 670 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.