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_Donor_Wall 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_Donor_Wall, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Give_Donor_Wall { |
||
|
|
|||
| 26 | |||
| 27 | /** |
||
| 28 | * Instance. |
||
| 29 | * |
||
| 30 | * @since 2.2.0 |
||
| 31 | * @access private |
||
| 32 | * @var Give_Donor_Wall |
||
| 33 | */ |
||
| 34 | static private $instance; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Singleton pattern. |
||
| 38 | * |
||
| 39 | * @since 2.2.0 |
||
| 40 | * @access private |
||
| 41 | */ |
||
| 42 | private function __construct() { |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Get instance. |
||
| 48 | * |
||
| 49 | * @since 2.2.0 |
||
| 50 | * @access public |
||
| 51 | * @return Give_Donor_Wall |
||
| 52 | */ |
||
| 53 | View Code Duplication | public static function get_instance() { |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Setup the default hooks and actions |
||
| 65 | * |
||
| 66 | * @since 2.2.0 |
||
| 67 | * |
||
| 68 | * @return void |
||
| 69 | */ |
||
| 70 | public function setup_actions() { |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * Displays donors in a grid layout. |
||
| 82 | * |
||
| 83 | * @since 2.2.0 |
||
| 84 | * |
||
| 85 | * @param array $atts { |
||
| 86 | * Optional. Attributes of the donor wall shortcode. |
||
| 87 | * |
||
| 88 | * @type int $donors_per_page Number of donors per page. Default '20'. |
||
| 89 | * @type int $form_id The donation form to filter donors by. Default is all forms (no filter). |
||
| 90 | * @type bool $paged Whether to paginate donors. Default 'true'. |
||
| 91 | * @type string $ids A comma-separated list of donor IDs to display. Default empty. |
||
| 92 | * @type string $columns Maximum columns to display. Default 'best-fit'. |
||
| 93 | * Accepts 'best-fit', '1', '2', '3', '4'. |
||
| 94 | * @type bool $show_avatar Whether to display the donor's gravatar image if available. Default 'true'. |
||
| 95 | * @type bool $show_name Whether to display the donor's full name, first and last. Default 'true'. |
||
| 96 | * @type bool $show_total Whether to display the donor's donation amount. Default 'true'. |
||
| 97 | * @type bool $show_time Whether to display date of the last donation. Default 'true'. |
||
| 98 | * @type bool $show_comments Whether to display the donor's comment if they left one. Default 'true'. |
||
| 99 | * @type int $comment_length The number of words to display for the comments before a "Read more" field |
||
| 100 | * @type int $only_comments Whether to display the donors only with comment. Default 'false'. |
||
| 101 | * |
||
| 102 | * @type string $readmore_text Link label for modal in which donor can read full comment. |
||
| 103 | * @type string $loadmore_text Button label which will load more donor comments. |
||
| 104 | * @type int $avatar_size Avatar image size in pixels without the "px". Default "60" |
||
| 105 | * @type string $orderby The order in which you want the donations to appear. |
||
| 106 | * Currently we are using this attribute internally and it will sort donations by created date. |
||
| 107 | * @type string $order The order in which you want the donors to appear. Accepts "ASC". "DESC". |
||
| 108 | * |
||
| 109 | * } |
||
| 110 | * @return string|bool The markup of the form grid or false. |
||
| 111 | */ |
||
| 112 | public function render_shortcode( $atts ) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Parse shortcode attributes |
||
| 170 | * |
||
| 171 | * @since 2.2.0 |
||
| 172 | * @access public |
||
| 173 | * |
||
| 174 | * @param array $atts Shortcode attributes. |
||
| 175 | * |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function parse_atts( $atts ) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get donors |
||
| 234 | * |
||
| 235 | * @since 2.2.0 |
||
| 236 | * @access public |
||
| 237 | * |
||
| 238 | * @param array $donor_query Dorno query. |
||
| 239 | * |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | public function get_donors( $donor_query ) { |
||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * Ajax handler |
||
| 252 | * |
||
| 253 | * @since 2.2.0 |
||
| 254 | * @access public |
||
| 255 | */ |
||
| 256 | public function ajax_handler() { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get query params |
||
| 284 | * |
||
| 285 | * @since 2.3.0 |
||
| 286 | * |
||
| 287 | * @param array $atts |
||
| 288 | * |
||
| 289 | * @return array |
||
| 290 | */ |
||
| 291 | private function get_query_param( $atts = array() ) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Get donation data. |
||
| 310 | * |
||
| 311 | * @since 2.3.0 |
||
| 312 | * |
||
| 313 | * @param array $atts |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | private function get_donation_data( $atts = array() ) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Get donation list for specific query |
||
| 376 | * |
||
| 377 | * @since 2.3.0 |
||
| 378 | * |
||
| 379 | * @param array $atts |
||
| 380 | * |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | private function get_donations( $atts = array() ) { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Get donor comments |
||
| 437 | * |
||
| 438 | * @since 2.3.0 |
||
| 439 | * |
||
| 440 | * @param array $donations_data |
||
| 441 | * |
||
| 442 | * @return array |
||
| 443 | */ |
||
| 444 | private function get_donor_comments( $donations_data = array() ) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Check if donation exist or not for specific query |
||
| 498 | * |
||
| 499 | * @since 2.3.0 |
||
| 500 | * |
||
| 501 | * @param array $atts |
||
| 502 | * |
||
| 503 | * @return bool |
||
| 504 | */ |
||
| 505 | private function has_donations( $atts = array() ) { |
||
| 508 | } |
||
| 509 | |||
| 512 |