Conditions | 4 |
Paths | 4 |
Total Lines | 54 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
62 | public function rest_api_notification( \WP_REST_Request $request ) { |
||
63 | $body = $request->get_body(); |
||
64 | |||
65 | if ( empty( $body ) ) { |
||
66 | return new \WP_Error( |
||
67 | 'ideal_basic_empty_notification', |
||
68 | __( 'The iDEAL Basic notification is empty.', 'pronamic_ideal' ), |
||
69 | array( 'status' => 400 ) |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | $xml = Core_Util::simplexml_load_string( $body ); |
||
74 | |||
75 | if ( \is_wp_error( $xml ) ) { |
||
76 | return $xml; |
||
77 | } |
||
78 | |||
79 | $notification = NotificationParser::parse( $xml ); |
||
80 | |||
81 | // Get payment for purchase ID. |
||
82 | $purchase_id = $notification->get_purchase_id(); |
||
83 | |||
84 | $payment = get_pronamic_payment_by_purchase_id( $purchase_id ); |
||
85 | |||
86 | if ( null === $payment ) { |
||
87 | return new \WP_Error( |
||
88 | 'ideal_basic_no_payment', |
||
89 | \sprintf( |
||
90 | /* translators: %s: Purchase ID. */ |
||
91 | __( 'Could not find iDEAL Basic payment by purchase ID: %s.', 'pronamic_ideal' ), |
||
92 | $purchase_id |
||
93 | ), |
||
94 | array( 'status' => 404 ) |
||
95 | ); |
||
96 | } |
||
97 | |||
98 | // Add note. |
||
99 | $note = sprintf( |
||
100 | /* translators: %s: payment provider name */ |
||
101 | __( 'Webhook requested by %s.', 'pronamic_ideal' ), |
||
102 | __( 'iDEAL Basic', 'pronamic_ideal' ) |
||
103 | ); |
||
104 | |||
105 | $payment->add_note( $note ); |
||
106 | |||
107 | // Update payment with notification data. |
||
108 | $payment->set_status( $notification->get_status() ); |
||
109 | $payment->set_transaction_id( $notification->get_transaction_id() ); |
||
110 | |||
111 | // Log webhook request. |
||
112 | \do_action( 'pronamic_pay_webhook_log_payment', $payment ); |
||
113 | |||
114 | // Update payment. |
||
115 | Plugin::update_payment( $payment ); |
||
116 | } |
||
134 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.