| Total Complexity | 52 |
| Total Lines | 417 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like GetPaid_Checkout 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.
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 GetPaid_Checkout, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class GetPaid_Checkout { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var GetPaid_Payment_Form_Submission |
||
| 17 | */ |
||
| 18 | protected $payment_form_submission; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Class constructor. |
||
| 22 | * |
||
| 23 | * @param GetPaid_Payment_Form_Submission $submission |
||
| 24 | */ |
||
| 25 | public function __construct( $submission ) { |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Processes the checkout. |
||
| 31 | * |
||
| 32 | */ |
||
| 33 | public function process_checkout() { |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Validates the submission. |
||
| 61 | * |
||
| 62 | */ |
||
| 63 | protected function validate_submission() { |
||
| 97 | } |
||
| 98 | |||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Retrieves submission items. |
||
| 103 | * |
||
| 104 | * @return GetPaid_Form_Item[] |
||
| 105 | */ |
||
| 106 | protected function get_submission_items() { |
||
| 107 | |||
| 108 | $items = $this->payment_form_submission->get_items(); |
||
| 109 | |||
| 110 | // Ensure that we have items. |
||
| 111 | if ( empty( $items ) && ! $this->payment_form_submission->has_fees() ) { |
||
| 112 | wp_send_json_error( __( 'Please provide at least one item or amount.', 'invoicing' ) ); |
||
| 113 | } |
||
| 114 | |||
| 115 | return $items; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Retrieves submission invoice. |
||
| 120 | * |
||
| 121 | * @return WPInv_Invoice |
||
| 122 | */ |
||
| 123 | protected function get_submission_invoice() { |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Processes the submission invoice. |
||
| 144 | * |
||
| 145 | * @param WPInv_Invoice $invoice |
||
| 146 | * @param GetPaid_Form_Item[] $items |
||
| 147 | * @return WPInv_Invoice |
||
| 148 | */ |
||
| 149 | protected function process_submission_invoice( $invoice, $items ) { |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Retrieves the submission's customer. |
||
| 177 | * |
||
| 178 | * @return int The customer id. |
||
| 179 | */ |
||
| 180 | protected function get_submission_customer() { |
||
| 181 | $submission = $this->payment_form_submission; |
||
| 182 | |||
| 183 | // If this is an existing invoice... |
||
| 184 | if ( $submission->has_invoice() ) { |
||
| 185 | return $submission->get_invoice()->get_user_id(); |
||
| 186 | } |
||
| 187 | |||
| 188 | // (Maybe) create the user. |
||
| 189 | $user = get_current_user_id(); |
||
| 190 | |||
| 191 | if ( empty( $user ) ) { |
||
| 192 | $user = get_user_by( 'email', $submission->get_billing_email() ); |
||
| 193 | } |
||
| 194 | |||
| 195 | if ( empty( $user ) ) { |
||
| 196 | $user = wpinv_create_user( $submission->get_billing_email() ); |
||
| 197 | |||
| 198 | // (Maybe) send new user notification. |
||
| 199 | $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
||
| 200 | if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ) ) ) { |
||
| 201 | wp_send_new_user_notifications( $user, 'user' ); |
||
| 202 | } |
||
| 203 | |||
| 204 | } |
||
| 205 | |||
| 206 | if ( is_wp_error( $user ) ) { |
||
| 207 | wp_send_json_error( $user->get_error_message() ); |
||
| 208 | } |
||
| 209 | |||
| 210 | if ( is_numeric( $user ) ) { |
||
| 211 | return $user; |
||
| 212 | } |
||
| 213 | |||
| 214 | return $user->ID; |
||
| 215 | |||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Prepares submission data for saving to the database. |
||
| 220 | * |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | public function prepare_submission_data_for_saving() { |
||
| 261 | |||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Retrieves address details. |
||
| 266 | * |
||
| 267 | * @return array |
||
| 268 | * @param WPInv_Invoice $invoice |
||
| 269 | * @param string $type |
||
| 270 | */ |
||
| 271 | public function prepare_address_details( $invoice, $type = 'billing' ) { |
||
| 272 | |||
| 273 | $data = $this->payment_form_submission->get_data(); |
||
| 274 | $type = sanitize_key( $type ); |
||
| 275 | $address = array(); |
||
| 276 | $prepared = array(); |
||
| 277 | |||
| 278 | if ( ! empty( $data[ $type ] ) ) { |
||
| 279 | $address = $data[ $type ]; |
||
| 280 | } |
||
| 281 | |||
| 282 | // Clean address details. |
||
| 283 | foreach ( $address as $key => $value ) { |
||
| 284 | $key = sanitize_key( $key ); |
||
| 285 | $key = str_replace( 'wpinv_', '', $key ); |
||
| 286 | $value = wpinv_clean( $value ); |
||
| 287 | $prepared[ $key] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
||
| 288 | } |
||
| 289 | |||
| 290 | // Filter address details. |
||
| 291 | $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
||
| 292 | |||
| 293 | // Remove non-whitelisted values. |
||
| 294 | return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
||
| 295 | |||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Prepares the billing details. |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | * @param WPInv_Invoice $invoice |
||
| 303 | */ |
||
| 304 | protected function prepare_billing_info( &$invoice ) { |
||
| 305 | |||
| 306 | $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
||
| 307 | |||
| 308 | // Update the invoice with the billing details. |
||
| 309 | $invoice->set_props( $billing_address ); |
||
| 310 | |||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Prepares the shipping details. |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | * @param WPInv_Invoice $invoice |
||
| 318 | */ |
||
| 319 | protected function prepare_shipping_info( $invoice ) { |
||
| 320 | |||
| 321 | $data = $this->payment_form_submission->get_data(); |
||
| 322 | |||
| 323 | if ( empty( $data['same-shipping-address'] ) ) { |
||
| 324 | return $this->prepare_address_details( $invoice, 'shipping' ); |
||
| 325 | } |
||
| 326 | |||
| 327 | return $this->prepare_address_details( $invoice, 'billing' ); |
||
| 328 | |||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Confirms the submission is valid and send users to the gateway. |
||
| 333 | * |
||
| 334 | * @param WPInv_Invoice $invoice |
||
| 335 | * @param array $prepared_payment_form_data |
||
| 336 | * @param array $shipping |
||
| 337 | */ |
||
| 338 | protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
||
| 339 | |||
| 340 | // Ensure the invoice exists. |
||
| 341 | if ( ! $invoice->exists() ) { |
||
| 342 | wp_send_json_error( __( 'An error occured while saving your invoice. Please try again.', 'invoicing' ) ); |
||
| 343 | } |
||
| 344 | |||
| 345 | // Save payment form data. |
||
| 346 | $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
||
| 347 | if ( ! empty( $prepared_payment_form_data ) ) { |
||
| 348 | update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data ); |
||
| 349 | } |
||
| 350 | |||
| 351 | // Save payment form data. |
||
| 352 | if ( ! empty( $shipping ) ) { |
||
| 353 | update_post_meta( $invoice->get_id(), 'shipping_address', $shipping ); |
||
| 354 | } |
||
| 355 | |||
| 356 | // Backwards compatibility. |
||
| 357 | add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) ); |
||
| 358 | |||
| 359 | $this->process_payment( $invoice ); |
||
| 360 | |||
| 361 | // If we are here, there was an error. |
||
| 362 | wpinv_send_back_to_checkout( $invoice ); |
||
| 363 | |||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Processes the actual payment. |
||
| 368 | * |
||
| 369 | * @param WPInv_Invoice $invoice |
||
| 370 | */ |
||
| 371 | protected function process_payment( $invoice ) { |
||
| 406 | |||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Marks the invoice as paid in case the checkout is free. |
||
| 411 | * |
||
| 412 | * @param WPInv_Invoice $invoice |
||
| 413 | */ |
||
| 414 | protected function process_free_payment( $invoice ) { |
||
| 415 | |||
| 416 | $invoice->set_gateway( 'none' ); |
||
| 417 | $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
||
| 418 | $invoice->mark_paid(); |
||
| 419 | wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
||
| 420 | |||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Sends a redrect response to payment details. |
||
| 425 | * |
||
| 426 | */ |
||
| 427 | public function send_redirect_response( $url ) { |
||
| 430 | } |
||
| 431 | |||
| 432 | } |
||
| 433 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.