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_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 |
||
| 33 | class Give_Donate_Form { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The donation ID. |
||
| 37 | * |
||
| 38 | * @since 1.0 |
||
| 39 | * @access public |
||
| 40 | * |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | public $ID = 0; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The donation price. |
||
| 47 | * |
||
| 48 | * @since 1.0 |
||
| 49 | * @access private |
||
| 50 | * |
||
| 51 | * @var float |
||
| 52 | */ |
||
| 53 | private $price; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The minimum donation price. |
||
| 57 | * |
||
| 58 | * @since 1.3.6 |
||
| 59 | * @access private |
||
| 60 | * |
||
| 61 | * @var float |
||
| 62 | */ |
||
| 63 | private $minimum_price; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The maximum donation price. |
||
| 67 | * |
||
| 68 | * @since 2.0 |
||
| 69 | * @access private |
||
| 70 | * |
||
| 71 | * @var float |
||
| 72 | */ |
||
| 73 | private $maximum_price; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The donation prices, if Price Levels are enabled. |
||
| 77 | * |
||
| 78 | * @since 1.0 |
||
| 79 | * @access private |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | private $prices; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The donation goal. |
||
| 87 | * |
||
| 88 | * @since 1.0 |
||
| 89 | * @access private |
||
| 90 | * |
||
| 91 | * @var float |
||
| 92 | */ |
||
| 93 | private $goal; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The form's sale count. |
||
| 97 | * |
||
| 98 | * @since 1.0 |
||
| 99 | * @access private |
||
| 100 | * |
||
| 101 | * @var int |
||
| 102 | */ |
||
| 103 | private $sales; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * The form's total earnings |
||
| 107 | * |
||
| 108 | * @since 1.0 |
||
| 109 | * @access private |
||
| 110 | * |
||
| 111 | * @var float |
||
| 112 | */ |
||
| 113 | private $earnings; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Declare the default properties in WP_Post as we can't extend it |
||
| 117 | * Anything we've declared above has been removed. |
||
| 118 | */ |
||
| 119 | |||
| 120 | /** |
||
| 121 | * The post author |
||
| 122 | * |
||
| 123 | * @since 1.0 |
||
| 124 | * @access public |
||
| 125 | * |
||
| 126 | * @var int |
||
| 127 | */ |
||
| 128 | public $post_author = 0; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * The post date |
||
| 132 | * |
||
| 133 | * @since 1.0 |
||
| 134 | * @access public |
||
| 135 | * |
||
| 136 | * @var string |
||
| 137 | */ |
||
| 138 | public $post_date = '0000-00-00 00:00:00'; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * The post GTM date |
||
| 142 | * |
||
| 143 | * @since 1.0 |
||
| 144 | * @access public |
||
| 145 | * |
||
| 146 | * @var string |
||
| 147 | */ |
||
| 148 | public $post_date_gmt = '0000-00-00 00:00:00'; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * The post content |
||
| 152 | * |
||
| 153 | * @since 1.0 |
||
| 154 | * @access public |
||
| 155 | * |
||
| 156 | * @var string |
||
| 157 | */ |
||
| 158 | public $post_content = ''; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * The post title |
||
| 162 | * |
||
| 163 | * @since 1.0 |
||
| 164 | * @access public |
||
| 165 | * |
||
| 166 | * @var string |
||
| 167 | */ |
||
| 168 | public $post_title = ''; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * The post excerpt |
||
| 172 | * |
||
| 173 | * @since 1.0 |
||
| 174 | * @access public |
||
| 175 | * |
||
| 176 | * @var string |
||
| 177 | */ |
||
| 178 | public $post_excerpt = ''; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * The post status |
||
| 182 | * |
||
| 183 | * @since 1.0 |
||
| 184 | * @access public |
||
| 185 | * |
||
| 186 | * @var string |
||
| 187 | */ |
||
| 188 | public $post_status = 'publish'; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * The comment status |
||
| 192 | * |
||
| 193 | * @since 1.0 |
||
| 194 | * @access public |
||
| 195 | * |
||
| 196 | * @var string |
||
| 197 | */ |
||
| 198 | public $comment_status = 'open'; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * The ping status |
||
| 202 | * |
||
| 203 | * @since 1.0 |
||
| 204 | * @access public |
||
| 205 | * |
||
| 206 | * @var string |
||
| 207 | */ |
||
| 208 | public $ping_status = 'open'; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * The post password |
||
| 212 | * |
||
| 213 | * @since 1.0 |
||
| 214 | * @access public |
||
| 215 | * |
||
| 216 | * @var string |
||
| 217 | */ |
||
| 218 | public $post_password = ''; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * The post name |
||
| 222 | * |
||
| 223 | * @since 1.0 |
||
| 224 | * @access public |
||
| 225 | * |
||
| 226 | * @var string |
||
| 227 | */ |
||
| 228 | public $post_name = ''; |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Ping |
||
| 232 | * |
||
| 233 | * @since 1.0 |
||
| 234 | * @access public |
||
| 235 | * |
||
| 236 | * @var string |
||
| 237 | */ |
||
| 238 | public $to_ping = ''; |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Pinged |
||
| 242 | * |
||
| 243 | * @since 1.0 |
||
| 244 | * @access public |
||
| 245 | * |
||
| 246 | * @var string |
||
| 247 | */ |
||
| 248 | public $pinged = ''; |
||
| 249 | |||
| 250 | /** |
||
| 251 | * The post modified date |
||
| 252 | * |
||
| 253 | * @since 1.0 |
||
| 254 | * @access public |
||
| 255 | * |
||
| 256 | * @var string |
||
| 257 | */ |
||
| 258 | public $post_modified = '0000-00-00 00:00:00'; |
||
| 259 | |||
| 260 | /** |
||
| 261 | * The post modified GTM date |
||
| 262 | * |
||
| 263 | * @since 1.0 |
||
| 264 | * @access public |
||
| 265 | * |
||
| 266 | * @var string |
||
| 267 | */ |
||
| 268 | public $post_modified_gmt = '0000-00-00 00:00:00'; |
||
| 269 | |||
| 270 | /** |
||
| 271 | * The post filtered content |
||
| 272 | * |
||
| 273 | * @since 1.0 |
||
| 274 | * @access public |
||
| 275 | * |
||
| 276 | * @var string |
||
| 277 | */ |
||
| 278 | public $post_content_filtered = ''; |
||
| 279 | |||
| 280 | /** |
||
| 281 | * The post parent |
||
| 282 | * |
||
| 283 | * @since 1.0 |
||
| 284 | * @access public |
||
| 285 | * |
||
| 286 | * @var int |
||
| 287 | */ |
||
| 288 | public $post_parent = 0; |
||
| 289 | |||
| 290 | /** |
||
| 291 | * The post GUID |
||
| 292 | * |
||
| 293 | * @since 1.0 |
||
| 294 | * @access public |
||
| 295 | * |
||
| 296 | * @var string |
||
| 297 | */ |
||
| 298 | public $guid = ''; |
||
| 299 | |||
| 300 | /** |
||
| 301 | * The menu order |
||
| 302 | * |
||
| 303 | * @since 1.0 |
||
| 304 | * @access public |
||
| 305 | * |
||
| 306 | * @var int |
||
| 307 | */ |
||
| 308 | public $menu_order = 0; |
||
| 309 | |||
| 310 | /** |
||
| 311 | * The mime type0 |
||
| 312 | * |
||
| 313 | * @since 1.0 |
||
| 314 | * @access public |
||
| 315 | * |
||
| 316 | * @var string |
||
| 317 | */ |
||
| 318 | public $post_mime_type = ''; |
||
| 319 | |||
| 320 | /** |
||
| 321 | * The comment count |
||
| 322 | * |
||
| 323 | * @since 1.0 |
||
| 324 | * @access public |
||
| 325 | * |
||
| 326 | * @var int |
||
| 327 | */ |
||
| 328 | public $comment_count = 0; |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Filtered |
||
| 332 | * |
||
| 333 | * @since 1.0 |
||
| 334 | * @access public |
||
| 335 | * |
||
| 336 | * @var string |
||
| 337 | */ |
||
| 338 | public $filter; |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Class Constructor |
||
| 342 | * |
||
| 343 | * Set up the Give Donate Form Class. |
||
| 344 | * |
||
| 345 | * @since 1.0 |
||
| 346 | * @access public |
||
| 347 | * |
||
| 348 | * @param bool $_id Post id. Default is false. |
||
| 349 | * @param array $_args Arguments passed. |
||
| 350 | */ |
||
| 351 | public function __construct( $_id = false, $_args = array() ) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Given the donation form data, let's set the variables |
||
| 360 | * |
||
| 361 | * @since 1.5 |
||
| 362 | * @access private |
||
| 363 | * |
||
| 364 | * @param WP_Post $donation_form WP_Post Object for the donation form. |
||
| 365 | * |
||
| 366 | * @return bool If the setup was successful or not. |
||
| 367 | */ |
||
| 368 | private function setup_donation_form( $donation_form ) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Magic __get function to dispatch a call to retrieve a private property |
||
| 400 | * |
||
| 401 | * @since 1.0 |
||
| 402 | * @access public |
||
| 403 | * |
||
| 404 | * @param string $key |
||
| 405 | * |
||
| 406 | * @return mixed |
||
| 407 | */ |
||
| 408 | View Code Duplication | public function __get( $key ) { |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Creates a donation form |
||
| 425 | * |
||
| 426 | * @since 1.5 |
||
| 427 | * @access public |
||
| 428 | * |
||
| 429 | * @param array $data Array of attributes for a donation form. |
||
| 430 | * |
||
| 431 | * @return bool|int False if data isn't passed and class not instantiated for creation, or New Form ID. |
||
| 432 | */ |
||
| 433 | public function create( $data = array() ) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Retrieve the ID |
||
| 472 | * |
||
| 473 | * @since 1.0 |
||
| 474 | * @access public |
||
| 475 | * |
||
| 476 | * @return int Donation form ID. |
||
| 477 | */ |
||
| 478 | public function get_ID() { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Retrieve the donation form name |
||
| 484 | * |
||
| 485 | * @since 1.5 |
||
| 486 | * @access public |
||
| 487 | * |
||
| 488 | * @return string Donation form name. |
||
| 489 | */ |
||
| 490 | public function get_name() { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Retrieve the price |
||
| 496 | * |
||
| 497 | * @since 1.0 |
||
| 498 | * @access public |
||
| 499 | * |
||
| 500 | * @return float Price. |
||
| 501 | */ |
||
| 502 | View Code Duplication | public function get_price() { |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Retrieve the minimum price. |
||
| 533 | * |
||
| 534 | * @since 1.3.6 |
||
| 535 | * @access public |
||
| 536 | * |
||
| 537 | * @return float Minimum price. |
||
| 538 | */ |
||
| 539 | View Code Duplication | public function get_minimum_price() { |
|
| 540 | |||
| 541 | if ( ! isset( $this->minimum_price ) ) { |
||
| 542 | |||
| 543 | $this->minimum_price = give_get_meta( $this->ID, '_give_custom_amount_range_minimum', true ); |
||
| 544 | |||
| 545 | // Give backward < 2.1 |
||
| 546 | if ( empty( $this->minimum_price ) ) { |
||
| 547 | $this->minimum_price = give_get_meta( $this->ID, '_give_custom_amount_minimum', true ); |
||
| 548 | } |
||
| 549 | |||
| 550 | if ( ! $this->is_custom_price_mode() ) { |
||
| 551 | $this->minimum_price = 0; |
||
| 552 | } |
||
| 553 | |||
| 554 | } |
||
| 555 | |||
| 556 | return apply_filters( 'give_get_set_minimum_price', $this->minimum_price, $this->ID ); |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Retrieve the maximum price. |
||
| 561 | * |
||
| 562 | * @since 2.1 |
||
| 563 | * @access public |
||
| 564 | * |
||
| 565 | * @return float Maximum price. |
||
| 566 | */ |
||
| 567 | public function get_maximum_price() { |
||
| 568 | |||
| 569 | if ( ! isset( $this->maximum_price ) ) { |
||
| 570 | $this->maximum_price = give_get_meta( $this->ID, '_give_custom_amount_range_maximum', true ); |
||
| 571 | |||
| 572 | if ( ! $this->is_custom_price_mode() ) { |
||
| 573 | $this->maximum_price = 999999.99; |
||
| 574 | } |
||
| 575 | } |
||
| 576 | |||
| 577 | return apply_filters( 'give_get_set_maximum_price', $this->maximum_price, $this->ID ); |
||
| 578 | } |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Retrieve the variable prices |
||
| 582 | * |
||
| 583 | * @since 1.0 |
||
| 584 | * @access public |
||
| 585 | * |
||
| 586 | * @return array Variable prices. |
||
| 587 | */ |
||
| 588 | public function get_prices() { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Retrieve the goal |
||
| 610 | * |
||
| 611 | * @since 1.0 |
||
| 612 | * @access public |
||
| 613 | * |
||
| 614 | * @return float Goal. |
||
| 615 | */ |
||
| 616 | View Code Duplication | public function get_goal() { |
|
| 635 | |||
| 636 | /** |
||
| 637 | * Determine if single price mode is enabled or disabled |
||
| 638 | * |
||
| 639 | * @since 1.0 |
||
| 640 | * @access public |
||
| 641 | * |
||
| 642 | * @return bool |
||
| 643 | */ |
||
| 644 | View Code Duplication | public function is_single_price_mode() { |
|
| 664 | |||
| 665 | /** |
||
| 666 | * Determine if custom price mode is enabled or disabled |
||
| 667 | * |
||
| 668 | * @since 1.6 |
||
| 669 | * @access public |
||
| 670 | * |
||
| 671 | * @return bool |
||
| 672 | */ |
||
| 673 | public function is_custom_price_mode() { |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Determine if custom price mode is enabled or disabled |
||
| 696 | * |
||
| 697 | * @since 1.8.18 |
||
| 698 | * @access public |
||
| 699 | * |
||
| 700 | * @param string|float $amount Donation Amount. |
||
| 701 | * |
||
| 702 | * @return bool |
||
| 703 | */ |
||
| 704 | public function is_custom_price( $amount ) { |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Has Variable Prices |
||
| 735 | * |
||
| 736 | * Determine if the donation form has variable prices enabled |
||
| 737 | * |
||
| 738 | * @since 1.0 |
||
| 739 | * @access public |
||
| 740 | * |
||
| 741 | * @return bool |
||
| 742 | */ |
||
| 743 | View Code Duplication | public function has_variable_prices() { |
|
| 761 | |||
| 762 | /** |
||
| 763 | * Retrieve the donation form type, set or multi-level |
||
| 764 | * |
||
| 765 | * @since 1.5 |
||
| 766 | * @access public |
||
| 767 | * |
||
| 768 | * @return string Type of donation form, either 'set' or 'multi'. |
||
| 769 | */ |
||
| 770 | View Code Duplication | public function get_type() { |
|
| 785 | |||
| 786 | /** |
||
| 787 | * Get form tag classes. |
||
| 788 | * |
||
| 789 | * Provides the classes for the donation <form> html tag and filters for customization. |
||
| 790 | * |
||
| 791 | * @since 1.6 |
||
| 792 | * @access public |
||
| 793 | * |
||
| 794 | * @param $args |
||
| 795 | * |
||
| 796 | * @return string |
||
| 797 | */ |
||
| 798 | public function get_form_classes( $args ) { |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Get form wrap Classes. |
||
| 820 | * |
||
| 821 | * Provides the classes for the donation form div wrapper and filters for customization. |
||
| 822 | * |
||
| 823 | * @access public |
||
| 824 | * |
||
| 825 | * @param $args |
||
| 826 | * |
||
| 827 | * @return string |
||
| 828 | */ |
||
| 829 | public function get_form_wrap_classes( $args ) { |
||
| 861 | |||
| 862 | /** |
||
| 863 | * Get if form type set or not. |
||
| 864 | * |
||
| 865 | * @since 1.6 |
||
| 866 | * @access public |
||
| 867 | * |
||
| 868 | * @return bool |
||
| 869 | */ |
||
| 870 | public function is_set_type_donation_form() { |
||
| 875 | |||
| 876 | /** |
||
| 877 | * Get if form type multi or not. |
||
| 878 | * |
||
| 879 | * @since 1.6 |
||
| 880 | * @access public |
||
| 881 | * |
||
| 882 | * @return bool True if form type is 'multi' and false otherwise. |
||
| 883 | */ |
||
| 884 | public function is_multi_type_donation_form() { |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Retrieve the sale count for the donation form |
||
| 893 | * |
||
| 894 | * @since 1.0 |
||
| 895 | * @access public |
||
| 896 | * |
||
| 897 | * @return int Donation form sale count. |
||
| 898 | */ |
||
| 899 | View Code Duplication | public function get_sales() { |
|
| 919 | |||
| 920 | /** |
||
| 921 | * Increment the sale count by one |
||
| 922 | * |
||
| 923 | * @since 1.0 |
||
| 924 | * @access public |
||
| 925 | * |
||
| 926 | * @param int $quantity The quantity to increase the donations by. Default is 1. |
||
| 927 | * |
||
| 928 | * @return int|false New number of total sales. |
||
| 929 | */ |
||
| 930 | public function increase_sales( $quantity = 1 ) { |
||
| 946 | |||
| 947 | /** |
||
| 948 | * Decrement the sale count by one |
||
| 949 | * |
||
| 950 | * @since 1.0 |
||
| 951 | * @access public |
||
| 952 | * |
||
| 953 | * @param int $quantity The quantity to decrease by. Default is 1. |
||
| 954 | * |
||
| 955 | * @return int|false New number of total sales. |
||
| 956 | */ |
||
| 957 | public function decrease_sales( $quantity = 1 ) { |
||
| 980 | |||
| 981 | /** |
||
| 982 | * Retrieve the total earnings for the form |
||
| 983 | * |
||
| 984 | * @since 1.0 |
||
| 985 | * @access public |
||
| 986 | * |
||
| 987 | * @return float Donation form total earnings. |
||
| 988 | */ |
||
| 989 | View Code Duplication | public function get_earnings() { |
|
| 1009 | |||
| 1010 | /** |
||
| 1011 | * Increase the earnings by the given amount |
||
| 1012 | * |
||
| 1013 | * @since 1.0 |
||
| 1014 | * @access public |
||
| 1015 | * |
||
| 1016 | * @param int $amount Amount of donation. Default is 0. |
||
| 1017 | * |
||
| 1018 | * @return float|false |
||
| 1019 | */ |
||
| 1020 | View Code Duplication | public function increase_earnings( $amount = 0 ) { |
|
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Decrease the earnings by the given amount |
||
| 1039 | * |
||
| 1040 | * @since 1.0 |
||
| 1041 | * @access public |
||
| 1042 | * |
||
| 1043 | * @param int $amount Amount of donation. |
||
| 1044 | * |
||
| 1045 | * @return float|false |
||
| 1046 | */ |
||
| 1047 | View Code Duplication | public function decrease_earnings( $amount ) { |
|
| 1069 | |||
| 1070 | /** |
||
| 1071 | * Determine if the donation is free or if the given price ID is free |
||
| 1072 | * |
||
| 1073 | * @since 1.0 |
||
| 1074 | * @access public |
||
| 1075 | * |
||
| 1076 | * @param int $price_id Price ID. Default is false. |
||
| 1077 | * |
||
| 1078 | * @return bool |
||
| 1079 | */ |
||
| 1080 | public function is_free( $price_id = false ) { |
||
| 1098 | |||
| 1099 | /** |
||
| 1100 | * Determine if donation form closed or not |
||
| 1101 | * |
||
| 1102 | * Form will be close if: |
||
| 1103 | * a. form has fixed goal |
||
| 1104 | * b. close form when goal achieved cmb2 setting is set to 'Yes' |
||
| 1105 | * c. goal has been achieved |
||
| 1106 | * |
||
| 1107 | * @since 1.4.5 |
||
| 1108 | * @access public |
||
| 1109 | * |
||
| 1110 | * @return bool |
||
| 1111 | */ |
||
| 1112 | public function is_close_donation_form() { |
||
| 1133 | |||
| 1134 | /** |
||
| 1135 | * Updates a single meta entry for the donation form |
||
| 1136 | * |
||
| 1137 | * @since 1.5 |
||
| 1138 | * @access private |
||
| 1139 | * |
||
| 1140 | * @param string $meta_key The meta_key to update. |
||
| 1141 | * @param string|array|object $meta_value The value to put into the meta. |
||
| 1142 | * |
||
| 1143 | * @return bool The result of the update query. |
||
| 1144 | */ |
||
| 1145 | private function update_meta( $meta_key = '', $meta_value = '' ) { |
||
| 1161 | |||
| 1162 | } |
||
| 1163 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.