| Total Complexity | 57 | 
| Total Lines | 440 | 
| 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() { | ||
| 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() { | ||
| 274 | |||
| 275 | } | ||
| 276 | |||
| 277 | /** | ||
| 278 | * Retrieves address details. | ||
| 279 | * | ||
| 280 | * @return array | ||
| 281 | * @param WPInv_Invoice $invoice | ||
| 282 | * @param string $type | ||
| 283 | */ | ||
| 284 |     public function prepare_address_details( $invoice, $type = 'billing' ) { | ||
| 285 | |||
| 286 | $data = $this->payment_form_submission->get_data(); | ||
| 287 | $type = sanitize_key( $type ); | ||
| 288 | $address = array(); | ||
| 289 | $prepared = array(); | ||
| 290 | |||
| 291 | 		if ( ! empty( $data[ $type ] ) ) { | ||
| 292 | $address = $data[ $type ]; | ||
| 293 | } | ||
| 294 | |||
| 295 | // Clean address details. | ||
| 296 | 		foreach ( $address as $key => $value ) { | ||
| 297 | $key = sanitize_key( $key ); | ||
| 298 | $key = str_replace( 'wpinv_', '', $key ); | ||
| 299 | $value = wpinv_clean( $value ); | ||
| 300 | 			$prepared[ $key] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); | ||
| 301 | } | ||
| 302 | |||
| 303 | // Filter address details. | ||
| 304 | 		$prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); | ||
| 305 | |||
| 306 | // Remove non-whitelisted values. | ||
| 307 | return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); | ||
| 308 | |||
| 309 | } | ||
| 310 | |||
| 311 | /** | ||
| 312 | * Prepares the billing details. | ||
| 313 | * | ||
| 314 | * @return array | ||
| 315 | * @param WPInv_Invoice $invoice | ||
| 316 | */ | ||
| 317 |     protected function prepare_billing_info( &$invoice ) { | ||
| 318 | |||
| 319 | $billing_address = $this->prepare_address_details( $invoice, 'billing' ); | ||
| 320 | |||
| 321 | // Update the invoice with the billing details. | ||
| 322 | $invoice->set_props( $billing_address ); | ||
| 323 | |||
| 324 | } | ||
| 325 | |||
| 326 | /** | ||
| 327 | * Prepares the shipping details. | ||
| 328 | * | ||
| 329 | * @return array | ||
| 330 | * @param WPInv_Invoice $invoice | ||
| 331 | */ | ||
| 332 |     protected function prepare_shipping_info( $invoice ) { | ||
| 333 | |||
| 334 | $data = $this->payment_form_submission->get_data(); | ||
| 335 | |||
| 336 | 		if ( empty( $data['same-shipping-address'] ) ) { | ||
| 337 | return $this->prepare_address_details( $invoice, 'shipping' ); | ||
| 338 | } | ||
| 339 | |||
| 340 | return $this->prepare_address_details( $invoice, 'billing' ); | ||
| 341 | |||
| 342 | } | ||
| 343 | |||
| 344 | /** | ||
| 345 | * Confirms the submission is valid and send users to the gateway. | ||
| 346 | * | ||
| 347 | * @param WPInv_Invoice $invoice | ||
| 348 | * @param array $prepared_payment_form_data | ||
| 349 | * @param array $shipping | ||
| 350 | */ | ||
| 351 | 	protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { | ||
| 352 | |||
| 353 | // Ensure the invoice exists. | ||
| 354 |         if ( ! $invoice->exists() ) { | ||
| 355 | wp_send_json_error( __( 'An error occured while saving your invoice. Please try again.', 'invoicing' ) ); | ||
| 356 | } | ||
| 357 | |||
| 358 | // Save payment form data. | ||
| 359 | $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); | ||
| 360 | delete_post_meta( $invoice->get_id(), 'payment_form_data' ); | ||
| 361 | delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); | ||
| 362 | 		if ( ! empty( $prepared_payment_form_data ) ) { | ||
| 363 | |||
| 364 | 			if ( ! empty( $prepared_payment_form_data['all'] ) ) { | ||
| 365 | update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); | ||
| 366 | } | ||
| 367 | |||
| 368 | 			if ( ! empty( $prepared_payment_form_data['meta'] ) ) { | ||
| 369 | update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); | ||
| 370 | } | ||
| 371 | |||
| 372 | } | ||
| 373 | |||
| 374 | // Save payment form data. | ||
| 375 |         if ( ! empty( $shipping ) ) { | ||
| 376 | update_post_meta( $invoice->get_id(), 'shipping_address', $shipping ); | ||
| 377 | } | ||
| 378 | |||
| 379 | // Backwards compatibility. | ||
| 380 | add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) ); | ||
| 381 | |||
| 382 | $this->process_payment( $invoice ); | ||
| 383 | |||
| 384 | // If we are here, there was an error. | ||
| 385 | wpinv_send_back_to_checkout( $invoice ); | ||
| 386 | |||
| 387 | } | ||
| 388 | |||
| 389 | /** | ||
| 390 | * Processes the actual payment. | ||
| 391 | * | ||
| 392 | * @param WPInv_Invoice $invoice | ||
| 393 | */ | ||
| 394 | 	protected function process_payment( $invoice ) { | ||
| 429 | |||
| 430 | } | ||
| 431 | |||
| 432 | /** | ||
| 433 | * Marks the invoice as paid in case the checkout is free. | ||
| 434 | * | ||
| 435 | * @param WPInv_Invoice $invoice | ||
| 436 | */ | ||
| 437 | 	protected function process_free_payment( $invoice ) { | ||
| 438 | |||
| 439 | $invoice->set_gateway( 'none' ); | ||
| 440 | $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); | ||
| 441 | $invoice->mark_paid(); | ||
| 442 | wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); | ||
| 443 | |||
| 444 | } | ||
| 445 | |||
| 446 | /** | ||
| 447 | * Sends a redrect response to payment details. | ||
| 448 | * | ||
| 449 | */ | ||
| 450 |     public function send_redirect_response( $url ) { | ||
| 453 | } | ||
| 454 | |||
| 455 | } | ||
| 456 | 
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.