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 | 57 | private $sales; |
|
| 104 | |||
| 105 | /** |
||
| 106 | 57 | * The form's total earnings |
|
| 107 | * |
||
| 108 | 57 | * @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 | 57 | /** |
|
| 121 | * The post author |
||
| 122 | 57 | * |
|
| 123 | 2 | * @since 1.0 |
|
| 124 | * @access public |
||
| 125 | * |
||
| 126 | 57 | * @var int |
|
| 127 | */ |
||
| 128 | public $post_author = 0; |
||
| 129 | |||
| 130 | 57 | /** |
|
| 131 | * The post date |
||
| 132 | * |
||
| 133 | * @since 1.0 |
||
| 134 | 57 | * @access public |
|
| 135 | * |
||
| 136 | * @var string |
||
| 137 | */ |
||
| 138 | 57 | public $post_date = '0000-00-00 00:00:00'; |
|
| 139 | 57 | ||
| 140 | 57 | /** |
|
| 141 | * The post GTM date |
||
| 142 | 57 | * |
|
| 143 | * @since 1.0 |
||
| 144 | 57 | * @access public |
|
| 145 | * |
||
| 146 | 57 | * @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 | 44 | /** |
|
| 161 | * The post title |
||
| 162 | 44 | * |
|
| 163 | * @since 1.0 |
||
| 164 | 44 | * @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 | 1 | public $post_name = ''; |
|
| 229 | |||
| 230 | 1 | /** |
|
| 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 | 15 | /** |
|
| 251 | * The post modified date |
||
| 252 | 15 | * |
|
| 253 | * @since 1.0 |
||
| 254 | 15 | * @access public |
|
| 255 | * |
||
| 256 | 15 | * @var string |
|
| 257 | */ |
||
| 258 | 4 | public $post_modified = '0000-00-00 00:00:00'; |
|
| 259 | |||
| 260 | 4 | /** |
|
| 261 | * The post modified GTM date |
||
| 262 | 11 | * |
|
| 263 | * @since 1.0 |
||
| 264 | * @access public |
||
| 265 | * |
||
| 266 | 15 | * @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 | 15 | * @var string |
|
| 277 | */ |
||
| 278 | public $post_content_filtered = ''; |
||
| 279 | |||
| 280 | /** |
||
| 281 | * The post parent |
||
| 282 | * |
||
| 283 | * @since 1.0 |
||
| 284 | * @access public |
||
| 285 | 2 | * |
|
| 286 | * @var int |
||
| 287 | 2 | */ |
|
| 288 | public $post_parent = 0; |
||
| 289 | 2 | ||
| 290 | 2 | /** |
|
| 291 | * The post GUID |
||
| 292 | 2 | * |
|
| 293 | * @since 1.0 |
||
| 294 | 1 | * @access public |
|
| 295 | * |
||
| 296 | 1 | * @var string |
|
| 297 | */ |
||
| 298 | 1 | public $guid = ''; |
|
| 299 | |||
| 300 | /** |
||
| 301 | * The menu order |
||
| 302 | 2 | * |
|
| 303 | * @since 1.0 |
||
| 304 | 2 | * @access public |
|
| 305 | * |
||
| 306 | * @var int |
||
| 307 | */ |
||
| 308 | public $menu_order = 0; |
||
| 309 | |||
| 310 | /** |
||
| 311 | * The mime type0 |
||
| 312 | * |
||
| 313 | 36 | * @since 1.0 |
|
| 314 | * @access public |
||
| 315 | 36 | * |
|
| 316 | * @var string |
||
| 317 | 36 | */ |
|
| 318 | public $post_mime_type = ''; |
||
| 319 | 36 | ||
| 320 | /** |
||
| 321 | * The comment count |
||
| 322 | * |
||
| 323 | * @since 1.0 |
||
| 324 | * @access public |
||
| 325 | * |
||
| 326 | * @var int |
||
| 327 | */ |
||
| 328 | public $comment_count = 0; |
||
| 329 | 36 | ||
| 330 | /** |
||
| 331 | * Filtered |
||
| 332 | * |
||
| 333 | * @since 1.0 |
||
| 334 | * @access public |
||
| 335 | * |
||
| 336 | * @var string |
||
| 337 | */ |
||
| 338 | public $filter; |
||
| 339 | 2 | ||
| 340 | /** |
||
| 341 | 2 | * Class Constructor |
|
| 342 | * |
||
| 343 | 2 | * Set up the Give Donate Form Class. |
|
| 344 | * |
||
| 345 | 2 | * @since 1.0 |
|
| 346 | * @access public |
||
| 347 | 1 | * |
|
| 348 | * @param int|bool $_id Post id. Default is false. |
||
| 349 | 1 | * @param array $_args Arguments passed. |
|
| 350 | */ |
||
| 351 | 2 | public function __construct( $_id = false, $_args = array() ) { |
|
| 357 | 2 | ||
| 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 | 53 | /** |
|
| 399 | 53 | * Magic __get function to dispatch a call to retrieve a private property |
|
| 400 | * |
||
| 401 | 53 | * @since 1.0 |
|
| 402 | 36 | * @access public |
|
| 403 | 36 | * |
|
| 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 | 42 | * @since 1.0 |
|
| 474 | * @access public |
||
| 475 | 42 | * |
|
| 476 | 42 | * @return int Donation form ID. |
|
| 477 | 42 | */ |
|
| 478 | public function get_ID() { |
||
| 481 | 42 | ||
| 482 | /** |
||
| 483 | 42 | * Retrieve the donation form name |
|
| 484 | * |
||
| 485 | * @since 1.5 |
||
| 486 | * @access public |
||
| 487 | 1 | * |
|
| 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 | 19 | * |
|
| 500 | * @return float Price. |
||
| 501 | 19 | */ |
|
| 502 | View Code Duplication | public function get_price() { |
|
| 530 | |||
| 531 | 43 | /** |
|
| 532 | * Retrieve the minimum price. |
||
| 533 | 43 | * |
|
| 534 | 23 | * @since 1.3.6 |
|
| 535 | 23 | * @access public |
|
| 536 | * |
||
| 537 | 43 | * @return float Minimum price. |
|
| 538 | */ |
||
| 539 | 43 | public function get_minimum_price() { |
|
| 558 | 42 | ||
| 559 | 42 | /** |
|
| 560 | * Retrieve the maximum price. |
||
| 561 | 42 | * |
|
| 562 | * @since 2.1 |
||
| 563 | 42 | * @access public |
|
| 564 | * |
||
| 565 | 42 | * @return float Maximum price. |
|
| 566 | */ |
||
| 567 | View Code Duplication | public function get_maximum_price() { |
|
| 579 | 19 | ||
| 580 | /** |
||
| 581 | 19 | * Retrieve the variable prices |
|
| 582 | * |
||
| 583 | 19 | * @since 1.0 |
|
| 584 | * @access public |
||
| 585 | 19 | * |
|
| 586 | * @return array Variable prices. |
||
| 587 | */ |
||
| 588 | 19 | public function get_prices() { |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Get donation form level info |
||
| 610 | * |
||
| 611 | * @since 2.0.6 |
||
| 612 | * @access public |
||
| 613 | * |
||
| 614 | * @param $price_id |
||
| 615 | * |
||
| 616 | * @return array|null |
||
| 617 | */ |
||
| 618 | public function get_level_info( $price_id ) { |
||
| 637 | |||
| 638 | 1 | ||
| 639 | /** |
||
| 640 | 1 | * Retrieve the goal |
|
| 641 | 1 | * |
|
| 642 | 1 | * @since 1.0 |
|
| 643 | 1 | * @access public |
|
| 644 | * |
||
| 645 | * @return float Goal. |
||
| 646 | */ |
||
| 647 | public function get_goal() { |
||
| 670 | 42 | ||
| 671 | 42 | /** |
|
| 672 | * Determine if single price mode is enabled or disabled |
||
| 673 | * |
||
| 674 | * @since 1.0 |
||
| 675 | 42 | * @access public |
|
| 676 | * |
||
| 677 | 42 | * @return bool |
|
| 678 | */ |
||
| 679 | 42 | View Code Duplication | public function is_single_price_mode() { |
| 699 | |||
| 700 | /** |
||
| 701 | * Determine if custom price mode is enabled or disabled |
||
| 702 | * |
||
| 703 | * @since 1.6 |
||
| 704 | * @access public |
||
| 705 | * |
||
| 706 | * @return bool |
||
| 707 | */ |
||
| 708 | public function is_custom_price_mode() { |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Determine if custom price mode is enabled or disabled |
||
| 731 | * |
||
| 732 | * @since 1.8.18 |
||
| 733 | * @access public |
||
| 734 | * |
||
| 735 | * @param string|float $amount Donation Amount. |
||
| 736 | * |
||
| 737 | * @return bool |
||
| 738 | */ |
||
| 739 | public function is_custom_price( $amount ) { |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Has Variable Prices |
||
| 770 | * |
||
| 771 | * Determine if the donation form has variable prices enabled |
||
| 772 | * |
||
| 773 | * @since 1.0 |
||
| 774 | * @access public |
||
| 775 | * |
||
| 776 | * @return bool |
||
| 777 | */ |
||
| 778 | View Code Duplication | public function has_variable_prices() { |
|
| 796 | |||
| 797 | /** |
||
| 798 | * Retrieve the donation form type, set or multi-level |
||
| 799 | * |
||
| 800 | * @since 1.5 |
||
| 801 | * @access public |
||
| 802 | * |
||
| 803 | * @return string Type of donation form, either 'set' or 'multi'. |
||
| 804 | */ |
||
| 805 | View Code Duplication | public function get_type() { |
|
| 820 | |||
| 821 | /** |
||
| 822 | * Get form tag classes. |
||
| 823 | * |
||
| 824 | * Provides the classes for the donation <form> html tag and filters for customization. |
||
| 825 | * |
||
| 826 | * @since 1.6 |
||
| 827 | * @access public |
||
| 828 | * |
||
| 829 | * @param $args |
||
| 830 | * |
||
| 831 | * @return string |
||
| 832 | */ |
||
| 833 | public function get_form_classes( $args ) { |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Get form wrap Classes. |
||
| 855 | * |
||
| 856 | * Provides the classes for the donation form div wrapper and filters for customization. |
||
| 857 | * |
||
| 858 | * @access public |
||
| 859 | * |
||
| 860 | * @param $args |
||
| 861 | * |
||
| 862 | * @return string |
||
| 863 | */ |
||
| 864 | public function get_form_wrap_classes( $args ) { |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Get if form type set or not. |
||
| 899 | * |
||
| 900 | * @since 1.6 |
||
| 901 | * @access public |
||
| 902 | * |
||
| 903 | * @return bool |
||
| 904 | */ |
||
| 905 | public function is_set_type_donation_form() { |
||
| 910 | |||
| 911 | /** |
||
| 912 | * Get if form type multi or not. |
||
| 913 | * |
||
| 914 | * @since 1.6 |
||
| 915 | * @access public |
||
| 916 | * |
||
| 917 | * @return bool True if form type is 'multi' and false otherwise. |
||
| 918 | */ |
||
| 919 | public function is_multi_type_donation_form() { |
||
| 925 | |||
| 926 | /** |
||
| 927 | * Retrieve the sale count for the donation form |
||
| 928 | * |
||
| 929 | * @since 1.0 |
||
| 930 | * @access public |
||
| 931 | * |
||
| 932 | * @return int Donation form sale count. |
||
| 933 | */ |
||
| 934 | View Code Duplication | public function get_sales() { |
|
| 954 | |||
| 955 | /** |
||
| 956 | * Increment the sale count by one |
||
| 957 | * |
||
| 958 | * @since 1.0 |
||
| 959 | * @access public |
||
| 960 | * |
||
| 961 | * @param int $quantity The quantity to increase the donations by. Default is 1. |
||
| 962 | * |
||
| 963 | * @return int|false New number of total sales. |
||
| 964 | */ |
||
| 965 | public function increase_sales( $quantity = 1 ) { |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Decrement the sale count by one |
||
| 984 | * |
||
| 985 | * @since 1.0 |
||
| 986 | * @access public |
||
| 987 | * |
||
| 988 | * @param int $quantity The quantity to decrease by. Default is 1. |
||
| 989 | * |
||
| 990 | * @return int|false New number of total sales. |
||
| 991 | */ |
||
| 992 | public function decrease_sales( $quantity = 1 ) { |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Retrieve the total earnings for the form |
||
| 1018 | * |
||
| 1019 | * @since 1.0 |
||
| 1020 | * @access public |
||
| 1021 | * |
||
| 1022 | * @return float Donation form total earnings. |
||
| 1023 | */ |
||
| 1024 | View Code Duplication | public function get_earnings() { |
|
| 1044 | |||
| 1045 | /** |
||
| 1046 | * Increase the earnings by the given amount |
||
| 1047 | * |
||
| 1048 | * @since 1.0 |
||
| 1049 | * @since 2.1 Pass the donation ID. |
||
| 1050 | * |
||
| 1051 | * @access public |
||
| 1052 | * |
||
| 1053 | * @param int $amount Amount of donation. Default is 0. |
||
| 1054 | * @param int $payment_id Donation ID. |
||
| 1055 | * |
||
| 1056 | * @return float|false |
||
| 1057 | */ |
||
| 1058 | View Code Duplication | public function increase_earnings( $amount = 0, $payment_id = 0 ) { |
|
| 1086 | |||
| 1087 | /** |
||
| 1088 | * Decrease the earnings by the given amount |
||
| 1089 | * |
||
| 1090 | * @since 1.0 |
||
| 1091 | * @access public |
||
| 1092 | * |
||
| 1093 | * @param int $amount Amount of donation. |
||
| 1094 | * @param int $payment_id Donation ID. |
||
| 1095 | * |
||
| 1096 | * @return float|false |
||
| 1097 | */ |
||
| 1098 | View Code Duplication | public function decrease_earnings( $amount, $payment_id = 0 ) { |
|
| 1129 | |||
| 1130 | /** |
||
| 1131 | * Determine if donation form closed or not |
||
| 1132 | * |
||
| 1133 | * Form will be close if: |
||
| 1134 | * a. form has fixed goal |
||
| 1135 | * b. close form when goal achieved cmb2 setting is set to 'Yes' |
||
| 1136 | * c. goal has been achieved |
||
| 1137 | * |
||
| 1138 | * @since 1.4.5 |
||
| 1139 | * @access public |
||
| 1140 | * |
||
| 1141 | * @return bool |
||
| 1142 | */ |
||
| 1143 | public function is_close_donation_form() { |
||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Updates a single meta entry for the donation form |
||
| 1168 | * |
||
| 1169 | * @since 1.5 |
||
| 1170 | * @access private |
||
| 1171 | * |
||
| 1172 | * @param string $meta_key The meta_key to update. |
||
| 1173 | * @param string|array|object $meta_value The value to put into the meta. |
||
| 1174 | * |
||
| 1175 | * @return bool The result of the update query. |
||
| 1176 | */ |
||
| 1177 | private function update_meta( $meta_key = '', $meta_value = '' ) { |
||
| 1193 | |||
| 1194 | /** |
||
| 1195 | * Backward Compatible function for is_close_donation_form() |
||
| 1196 | * |
||
| 1197 | * @since 2.1.0 |
||
| 1198 | * |
||
| 1199 | * @return bool |
||
| 1200 | */ |
||
| 1201 | private function bc_210_is_close_donation_form() { |
||
| 1239 | |||
| 1240 | } |
||
| 1241 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.