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:
| 1 | <?php |
||
| 26 | class Give_Payment_Stats extends Give_Stats { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Retrieve sale stats |
||
| 30 | * |
||
| 31 | * @since 1.0 |
||
| 32 | * @access public |
||
| 33 | * |
||
| 34 | * @param $form_id int The donation form to retrieve stats for. If false, gets stats for all forms |
||
| 35 | * @param $start_date string|bool The starting date for which we'd like to filter our sale stats. If false, we'll use the default start date of `this_month` |
||
| 36 | * @param $end_date string|bool The end date for which we'd like to filter our sale stats. If false, we'll use the default end date of `this_month` |
||
| 37 | * @param $status string|array The sale status(es) to count. Only valid when retrieving global stats |
||
| 38 | * |
||
| 39 | * @return float|int Total amount of donations based on the passed arguments. |
||
| 40 | */ |
||
| 41 | public function get_sales( $form_id = 0, $start_date = false, $end_date = false, $status = 'publish' ) { |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * Retrieve earning stats |
||
| 77 | * |
||
| 78 | * @since 1.0 |
||
| 79 | * @access public |
||
| 80 | * |
||
| 81 | * @param $form_id int The donation form to retrieve stats for. If false, gets stats for all forms. |
||
| 82 | * @param $start_date string|bool The starting date for which we'd like to filter our donation earnings stats. If false, method will use the default start date of `this_month`. |
||
| 83 | * @param $end_date string|bool The end date for which we'd like to filter the donations stats. If false, method will use the default end date of `this_month`. |
||
| 84 | * @param $gateway_id string|bool The gateway to get earnings for such as 'paypal' or 'stripe'. |
||
| 85 | * |
||
| 86 | * @return float|int Total amount of donations based on the passed arguments. |
||
| 87 | 2 | */ |
|
| 88 | public function get_earnings( $form_id = 0, $start_date = false, $end_date = false, $gateway_id = false ) { |
||
| 89 | $this->setup_dates( $start_date, $end_date ); |
||
| 90 | |||
| 91 | // Make sure start date is valid |
||
| 92 | if ( is_wp_error( $this->start_date ) ) { |
||
| 93 | return $this->start_date; |
||
| 94 | } |
||
| 95 | |||
| 96 | // Make sure end date is valid |
||
| 97 | if ( is_wp_error( $this->end_date ) ) { |
||
| 98 | return $this->end_date; |
||
| 99 | } |
||
| 100 | |||
| 101 | $args = array( |
||
| 102 | 'status' => 'publish', |
||
| 103 | 'give_forms' => $form_id, |
||
| 104 | 'start_date' => $this->start_date, |
||
| 105 | 2 | 'end_date' => $this->end_date, |
|
| 106 | 'fields' => 'ids', |
||
| 107 | 2 | 'number' => - 1, |
|
| 108 | ); |
||
| 109 | 2 | ||
| 110 | |||
| 111 | // Filter by Gateway ID meta_key |
||
| 112 | 2 | if ( $gateway_id ) { |
|
| 113 | $args['meta_query'][] = array( |
||
| 114 | 'key' => '_give_payment_gateway', |
||
| 115 | 'value' => $gateway_id, |
||
| 116 | ); |
||
| 117 | 2 | } |
|
| 118 | |||
| 119 | // Filter by Gateway ID meta_key |
||
| 120 | if ( $form_id ) { |
||
| 121 | 2 | $args['meta_query'][] = array( |
|
| 122 | 'key' => '_give_payment_form_id', |
||
| 123 | 2 | 'value' => $form_id, |
|
| 124 | ); |
||
| 125 | } |
||
| 126 | |||
| 127 | 2 | View Code Duplication | if ( ! empty( $args['meta_query'] ) && 1 < count( $args['meta_query'] ) ) { |
| 128 | 2 | $args['meta_query']['relation'] = 'AND'; |
|
| 129 | 2 | } |
|
| 130 | 2 | ||
| 131 | 2 | $args = apply_filters( 'give_stats_earnings_args', $args ); |
|
| 132 | 2 | $key = Give_Cache::get_key( 'give_stats', $args ); |
|
| 133 | 2 | ||
| 134 | //Set transient for faster stats |
||
| 135 | 2 | $earnings = Give_Cache::get( $key ); |
|
| 136 | 2 | ||
| 137 | if ( false === $earnings ) { |
||
| 138 | 2 | ||
| 139 | $this->timestamp = false; |
||
| 140 | $payments = new Give_Payments_Query( $args ); |
||
| 141 | 2 | $payments = $payments->get_payments(); |
|
| 142 | $earnings = 0; |
||
| 143 | |||
| 144 | if ( ! empty( $payments ) ) { |
||
| 145 | foreach ( $payments as $payment ) { |
||
| 146 | 2 | $earnings += give_get_payment_amount( $payment->ID ); |
|
| 147 | 2 | } |
|
| 148 | 2 | ||
| 149 | } |
||
| 150 | 2 | ||
| 151 | 2 | // Cache the results for one hour |
|
| 152 | 2 | Give_Cache::set( $key, give_sanitize_amount_for_db( $earnings ), 60 * 60 ); |
|
| 153 | 2 | } |
|
| 154 | 2 | ||
| 155 | 2 | //return earnings |
|
| 156 | 2 | return round( $earnings, give_currency_decimal_filter() ); |
|
| 157 | |||
| 158 | 2 | } |
|
| 159 | 2 | ||
| 160 | /** |
||
| 161 | 2 | * Retrieve earning stat transient key |
|
| 162 | * |
||
| 163 | * @since 1.0 |
||
| 164 | * @access public |
||
| 165 | * |
||
| 166 | * @param $form_id int The donation form to retrieve stats for. If false, gets stats for all forms |
||
| 167 | * @param $start_date string|bool The starting date for which we'd like to filter our donation earnings stats. If false, we'll use the default start date of `this_month` |
||
| 168 | * @param $end_date string|bool The end date for which we'd like to filter our sale stats. If false, we'll use the default end date of `this_month` |
||
| 169 | * @param $gateway_id string|bool The gateway to get earnings for such as 'paypal' or 'stripe' |
||
| 170 | * |
||
| 171 | * @return float|int Total amount of donations based on the passed arguments. |
||
| 172 | */ |
||
| 173 | public function get_earnings_cache_key( $form_id = 0, $start_date = false, $end_date = false, $gateway_id = false ) { |
||
| 174 | |||
| 175 | $this->setup_dates( $start_date, $end_date ); |
||
| 176 | |||
| 177 | // Make sure start date is valid |
||
| 178 | if ( is_wp_error( $this->start_date ) ) { |
||
| 179 | return $this->start_date; |
||
| 180 | } |
||
| 181 | |||
| 182 | // Make sure end date is valid |
||
| 183 | if ( is_wp_error( $this->end_date ) ) { |
||
| 184 | return $this->end_date; |
||
| 185 | } |
||
| 186 | |||
| 187 | $args = array( |
||
| 188 | 'status' => 'publish', |
||
| 189 | 'give_forms' => $form_id, |
||
| 190 | 'start_date' => $this->start_date, |
||
| 191 | 'end_date' => $this->end_date, |
||
| 192 | 'fields' => 'ids', |
||
| 193 | 'number' => - 1, |
||
| 194 | ); |
||
| 195 | |||
| 196 | |||
| 197 | // Filter by Gateway ID meta_key |
||
| 198 | if ( $gateway_id ) { |
||
| 199 | $args['meta_query'][] = array( |
||
| 200 | 'key' => '_give_payment_gateway', |
||
| 201 | 'value' => $gateway_id, |
||
| 202 | ); |
||
| 203 | } |
||
| 204 | |||
| 205 | 2 | // Filter by Gateway ID meta_key |
|
| 206 | if ( $form_id ) { |
||
| 207 | $args['meta_query'][] = array( |
||
| 208 | 2 | 'key' => '_give_payment_form_id', |
|
| 209 | 'value' => $form_id, |
||
| 210 | ); |
||
| 211 | } |
||
| 212 | |||
| 213 | View Code Duplication | if ( ! empty( $args['meta_query'] ) && 1 < count( $args['meta_query'] ) ) { |
|
| 214 | $args['meta_query']['relation'] = 'AND'; |
||
| 215 | } |
||
| 216 | |||
| 217 | $args = apply_filters( 'give_stats_earnings_args', $args ); |
||
| 218 | $key = Give_Cache::get_key( 'give_stats', $args ); |
||
| 219 | |||
| 220 | //return earnings |
||
| 221 | return $key; |
||
| 222 | 1 | ||
| 223 | } |
||
| 224 | 1 | ||
| 225 | /** |
||
| 226 | 1 | * Get the best selling forms |
|
| 227 | * |
||
| 228 | 1 | * @since 1.0 |
|
| 229 | * @access public |
||
| 230 | 1 | * |
|
| 231 | 1 | * @param $number int The number of results to retrieve with the default set to 10. |
|
| 232 | * |
||
| 233 | 1 | * @return array Best selling forms |
|
| 234 | */ |
||
| 235 | public function get_best_selling( $number = 10 ) { |
||
| 248 | |||
| 249 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.