Conditions | 11 |
Paths | 34 |
Total Lines | 112 |
Lines | 24 |
Ratio | 21.43 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
89 | public function get_earnings( $form_id = 0, $start_date = false, $end_date = false, $gateway_id = false ) { |
||
90 | global $wpdb; |
||
91 | $this->setup_dates( $start_date, $end_date ); |
||
92 | |||
93 | // Make sure start date is valid |
||
94 | if ( is_wp_error( $this->start_date ) ) { |
||
95 | return $this->start_date; |
||
96 | } |
||
97 | |||
98 | // Make sure end date is valid |
||
99 | if ( is_wp_error( $this->end_date ) ) { |
||
100 | return $this->end_date; |
||
101 | } |
||
102 | |||
103 | $args = array( |
||
104 | 'status' => 'publish', |
||
105 | 'give_forms' => $form_id, |
||
106 | 'start_date' => $this->start_date, |
||
107 | 'end_date' => $this->end_date, |
||
108 | 'fields' => 'ids', |
||
109 | 'number' => - 1, |
||
110 | 'output' => '', |
||
111 | ); |
||
112 | |||
113 | |||
114 | // Filter by Gateway ID meta_key |
||
115 | if ( $gateway_id ) { |
||
116 | $args['meta_query'][] = array( |
||
117 | 'key' => '_give_payment_gateway', |
||
118 | 'value' => $gateway_id, |
||
119 | ); |
||
120 | } |
||
121 | |||
122 | // Filter by Gateway ID meta_key |
||
123 | if ( $form_id ) { |
||
124 | $args['meta_query'][] = array( |
||
125 | 'key' => '_give_payment_form_id', |
||
126 | 'value' => $form_id, |
||
127 | ); |
||
128 | } |
||
129 | |||
130 | View Code Duplication | if ( ! empty( $args['meta_query'] ) && 1 < count( $args['meta_query'] ) ) { |
|
131 | $args['meta_query']['relation'] = 'AND'; |
||
132 | } |
||
133 | |||
134 | $args = apply_filters( 'give_stats_earnings_args', $args ); |
||
135 | $key = Give_Cache::get_key( 'give_stats', $args ); |
||
136 | |||
137 | // Set transient for faster stats. |
||
138 | $earnings = Give_Cache::get( $key ); |
||
139 | |||
140 | if ( false === $earnings ) { |
||
141 | |||
142 | $this->timestamp = false; |
||
143 | $payments = new Give_Payments_Query( $args ); |
||
144 | $payments = $payments->get_payments(); |
||
145 | $earnings = 0; |
||
146 | |||
147 | if ( ! empty( $payments ) ) { |
||
148 | $donation_id_col = Give()->payment_meta->get_meta_type() . '_id'; |
||
149 | $query = "SELECT {$donation_id_col} as id, meta_value as total |
||
150 | FROM {$wpdb->donationmeta} |
||
151 | WHERE meta_key='_give_payment_total' |
||
152 | AND {$donation_id_col} IN ('". implode( '\',\'', $payments ) ."')"; |
||
153 | |||
154 | $payments = $wpdb->get_results($query, ARRAY_A); |
||
155 | |||
156 | View Code Duplication | if( ! empty( $payments ) ) { |
|
157 | foreach ( $payments as $payment ) { |
||
158 | $currency_code = give_get_payment_currency_code( $payment['id'] ); |
||
159 | |||
160 | /** |
||
161 | * Filter the donation amount |
||
162 | * Note: this filter documented in payments/functions.php:give_donation_amount() |
||
163 | * |
||
164 | * @since 2.1 |
||
165 | */ |
||
166 | $formatted_amount = apply_filters( |
||
167 | 'give_donation_amount', |
||
168 | give_format_amount( $payment['total'], array( 'donation_id' => $payment['id'] ) ), |
||
169 | $payment['total'], |
||
170 | $payment['id'], |
||
171 | array( 'type' => 'stats', 'currency'=> false, 'amount' => false ) |
||
172 | ); |
||
173 | |||
174 | $earnings += (float) give_maybe_sanitize_amount( $formatted_amount, array( 'currency' => $currency_code ) ); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | } |
||
179 | |||
180 | // Cache the results for one hour. |
||
181 | Give_Cache::set( $key, give_sanitize_amount_for_db( $earnings ), 60 * 60 ); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Filter the earnings. |
||
186 | * |
||
187 | * @since 1.8.17 |
||
188 | * |
||
189 | * @param float $earnings Earning amount. |
||
190 | * @param int $form_id Donation Form ID. |
||
191 | * @param string|bool $start_date Earning start date. |
||
192 | * @param string|bool $end_date Earning end date. |
||
193 | * @param string|bool $gateway_id Payment gateway id. |
||
194 | */ |
||
195 | $earnings = apply_filters( 'give_get_earnings', $earnings, $form_id, $start_date, $end_date, $gateway_id ); |
||
196 | |||
197 | //return earnings |
||
198 | return round( $earnings, give_get_price_decimals( $form_id ) ); |
||
199 | |||
200 | } |
||
201 | |||
294 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.