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 int|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 ) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Magic __get function to dispatch a call to retrieve a private property |
||
| 389 | * |
||
| 390 | * @since 1.0 |
||
| 391 | * @access public |
||
| 392 | * |
||
| 393 | * @param string $key |
||
| 394 | * |
||
| 395 | * @return mixed |
||
| 396 | */ |
||
| 397 | public function __get( $key ) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Creates a donation form |
||
| 410 | * |
||
| 411 | * @since 1.5 |
||
| 412 | * @access public |
||
| 413 | * |
||
| 414 | * @param array $data Array of attributes for a donation form. |
||
| 415 | * |
||
| 416 | * @return bool|int False if data isn't passed and class not instantiated for creation, or New Form ID. |
||
| 417 | */ |
||
| 418 | public function create( $data = array() ) { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Retrieve the ID |
||
| 457 | * |
||
| 458 | * @since 1.0 |
||
| 459 | * @access public |
||
| 460 | * |
||
| 461 | * @return int Donation form ID. |
||
| 462 | */ |
||
| 463 | public function get_ID() { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Retrieve the donation form name |
||
| 469 | * |
||
| 470 | * @since 1.5 |
||
| 471 | * @access public |
||
| 472 | * |
||
| 473 | * @return string Donation form name. |
||
| 474 | */ |
||
| 475 | public function get_name() { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Retrieve the price |
||
| 481 | * |
||
| 482 | * @since 1.0 |
||
| 483 | * @access public |
||
| 484 | * |
||
| 485 | * @return float Price. |
||
| 486 | */ |
||
| 487 | View Code Duplication | public function get_price() { |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Retrieve the minimum price. |
||
| 518 | * |
||
| 519 | * @since 1.3.6 |
||
| 520 | * @access public |
||
| 521 | * |
||
| 522 | * @return float Minimum price. |
||
| 523 | */ |
||
| 524 | public function get_minimum_price() { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Retrieve the maximum price. |
||
| 546 | * |
||
| 547 | * @since 2.1 |
||
| 548 | * @access public |
||
| 549 | * |
||
| 550 | * @return float Maximum price. |
||
| 551 | */ |
||
| 552 | View Code Duplication | public function get_maximum_price() { |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Retrieve the variable prices |
||
| 567 | * |
||
| 568 | * @since 1.0 |
||
| 569 | * @access public |
||
| 570 | * |
||
| 571 | * @return array Variable prices. |
||
| 572 | */ |
||
| 573 | public function get_prices() { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Get donation form level info |
||
| 595 | * |
||
| 596 | * @since 2.0.6 |
||
| 597 | * @access public |
||
| 598 | * |
||
| 599 | * @param $price_id |
||
| 600 | * |
||
| 601 | * @return array|null |
||
| 602 | */ |
||
| 603 | public function get_level_info( $price_id ) { |
||
| 622 | |||
| 623 | |||
| 624 | /** |
||
| 625 | * Retrieve the goal |
||
| 626 | * |
||
| 627 | * @since 1.0 |
||
| 628 | * @access public |
||
| 629 | * |
||
| 630 | * @return float Goal. |
||
| 631 | */ |
||
| 632 | public function get_goal() { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Determine if single price mode is enabled or disabled |
||
| 658 | * |
||
| 659 | * @since 1.0 |
||
| 660 | * @access public |
||
| 661 | * |
||
| 662 | * @return bool |
||
| 663 | */ |
||
| 664 | View Code Duplication | public function is_single_price_mode() { |
|
| 684 | |||
| 685 | /** |
||
| 686 | * Determine if custom price mode is enabled or disabled |
||
| 687 | * |
||
| 688 | * @since 1.6 |
||
| 689 | * @access public |
||
| 690 | * |
||
| 691 | * @return bool |
||
| 692 | */ |
||
| 693 | public function is_custom_price_mode() { |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Determine if custom price mode is enabled or disabled |
||
| 716 | * |
||
| 717 | * @since 1.8.18 |
||
| 718 | * @access public |
||
| 719 | * |
||
| 720 | * @param string|float $amount Donation Amount. |
||
| 721 | * |
||
| 722 | * @return bool |
||
| 723 | */ |
||
| 724 | public function is_custom_price( $amount ) { |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Has Variable Prices |
||
| 755 | * |
||
| 756 | * Determine if the donation form has variable prices enabled |
||
| 757 | * |
||
| 758 | * @since 1.0 |
||
| 759 | * @access public |
||
| 760 | * |
||
| 761 | * @return bool |
||
| 762 | */ |
||
| 763 | View Code Duplication | public function has_variable_prices() { |
|
| 781 | |||
| 782 | /** |
||
| 783 | * Retrieve the donation form type, set or multi-level |
||
| 784 | * |
||
| 785 | * @since 1.5 |
||
| 786 | * @access public |
||
| 787 | * |
||
| 788 | * @return string Type of donation form, either 'set' or 'multi'. |
||
| 789 | */ |
||
| 790 | View Code Duplication | public function get_type() { |
|
| 805 | |||
| 806 | /** |
||
| 807 | * Get form tag classes. |
||
| 808 | * |
||
| 809 | * Provides the classes for the donation <form> html tag and filters for customization. |
||
| 810 | * |
||
| 811 | * @since 1.6 |
||
| 812 | * @access public |
||
| 813 | * |
||
| 814 | * @param $args |
||
| 815 | * |
||
| 816 | * @return string |
||
| 817 | */ |
||
| 818 | public function get_form_classes( $args ) { |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Get form wrap Classes. |
||
| 840 | * |
||
| 841 | * Provides the classes for the donation form div wrapper and filters for customization. |
||
| 842 | * |
||
| 843 | * @access public |
||
| 844 | * |
||
| 845 | * @param $args |
||
| 846 | * |
||
| 847 | * @return string |
||
| 848 | */ |
||
| 849 | public function get_form_wrap_classes( $args ) { |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Get if form type set or not. |
||
| 884 | * |
||
| 885 | * @since 1.6 |
||
| 886 | * @access public |
||
| 887 | * |
||
| 888 | * @return bool |
||
| 889 | */ |
||
| 890 | public function is_set_type_donation_form() { |
||
| 895 | |||
| 896 | /** |
||
| 897 | * Get if form type multi or not. |
||
| 898 | * |
||
| 899 | * @since 1.6 |
||
| 900 | * @access public |
||
| 901 | * |
||
| 902 | * @return bool True if form type is 'multi' and false otherwise. |
||
| 903 | */ |
||
| 904 | public function is_multi_type_donation_form() { |
||
| 910 | |||
| 911 | /** |
||
| 912 | * Retrieve the sale count for the donation form |
||
| 913 | * |
||
| 914 | * @since 1.0 |
||
| 915 | * @access public |
||
| 916 | * |
||
| 917 | * @return int Donation form sale count. |
||
| 918 | */ |
||
| 919 | View Code Duplication | public function get_sales() { |
|
| 939 | |||
| 940 | /** |
||
| 941 | * Increment the sale count by one |
||
| 942 | * |
||
| 943 | * @since 1.0 |
||
| 944 | * @access public |
||
| 945 | * |
||
| 946 | * @param int $quantity The quantity to increase the donations by. Default is 1. |
||
| 947 | * |
||
| 948 | * @return int|false New number of total sales. |
||
| 949 | */ |
||
| 950 | public function increase_sales( $quantity = 1 ) { |
||
| 966 | |||
| 967 | /** |
||
| 968 | * Decrement the sale count by one |
||
| 969 | * |
||
| 970 | * @since 1.0 |
||
| 971 | * @access public |
||
| 972 | * |
||
| 973 | * @param int $quantity The quantity to decrease by. Default is 1. |
||
| 974 | * |
||
| 975 | * @return int|false New number of total sales. |
||
| 976 | */ |
||
| 977 | public function decrease_sales( $quantity = 1 ) { |
||
| 1000 | |||
| 1001 | /** |
||
| 1002 | * Retrieve the total earnings for the form |
||
| 1003 | * |
||
| 1004 | * @since 1.0 |
||
| 1005 | * @access public |
||
| 1006 | * |
||
| 1007 | * @return float Donation form total earnings. |
||
| 1008 | */ |
||
| 1009 | View Code Duplication | public function get_earnings() { |
|
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Increase the earnings by the given amount |
||
| 1032 | * |
||
| 1033 | * @since 1.0 |
||
| 1034 | * @since 2.1 Pass the donation ID. |
||
| 1035 | * |
||
| 1036 | * @access public |
||
| 1037 | * |
||
| 1038 | * @param int $amount Amount of donation. Default is 0. |
||
| 1039 | * @param int $payment_id Donation ID. |
||
| 1040 | * |
||
| 1041 | * @return float|false |
||
| 1042 | */ |
||
| 1043 | View Code Duplication | public function increase_earnings( $amount = 0, $payment_id = 0 ) { |
|
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Decrease the earnings by the given amount |
||
| 1074 | * |
||
| 1075 | * @since 1.0 |
||
| 1076 | * @access public |
||
| 1077 | * |
||
| 1078 | * @param int $amount Amount of donation. |
||
| 1079 | * @param int $payment_id Donation ID. |
||
| 1080 | * |
||
| 1081 | * @return float|false |
||
| 1082 | */ |
||
| 1083 | View Code Duplication | public function decrease_earnings( $amount, $payment_id = 0 ) { |
|
| 1114 | |||
| 1115 | /** |
||
| 1116 | * Determine if donation form closed or not |
||
| 1117 | * |
||
| 1118 | * Form will be close if: |
||
| 1119 | * a. form has fixed goal |
||
| 1120 | * b. close form when goal achieved cmb2 setting is set to 'Yes' |
||
| 1121 | * c. goal has been achieved |
||
| 1122 | * |
||
| 1123 | * @since 1.4.5 |
||
| 1124 | * @access public |
||
| 1125 | * |
||
| 1126 | * @return bool |
||
| 1127 | */ |
||
| 1128 | public function is_close_donation_form() { |
||
| 1150 | |||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * Check whether donation form has goal or not |
||
| 1154 | * |
||
| 1155 | * @since 2.4.2 |
||
| 1156 | * @access public |
||
| 1157 | * |
||
| 1158 | * @return bool |
||
| 1159 | */ |
||
| 1160 | public function has_goal() { |
||
| 1169 | |||
| 1170 | /** |
||
| 1171 | * Updates a single meta entry for the donation form |
||
| 1172 | * |
||
| 1173 | * @since 1.5 |
||
| 1174 | * @access private |
||
| 1175 | * |
||
| 1176 | * @param string $meta_key The meta_key to update. |
||
| 1177 | * @param string|array|object $meta_value The value to put into the meta. |
||
| 1178 | * |
||
| 1179 | * @return bool The result of the update query. |
||
| 1180 | */ |
||
| 1181 | private function update_meta( $meta_key = '', $meta_value = '' ) { |
||
| 1197 | |||
| 1198 | /** |
||
| 1199 | * Backward Compatible function for is_close_donation_form() |
||
| 1200 | * |
||
| 1201 | * @since 2.1.0 |
||
| 1202 | * |
||
| 1203 | * @return bool |
||
| 1204 | */ |
||
| 1205 | private function bc_210_is_close_donation_form() { |
||
| 1243 | |||
| 1244 | } |
||
| 1245 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.