| Conditions | 9 |
| Paths | 4 |
| Total Lines | 71 |
| Lines | 28 |
| Ratio | 39.44 % |
| 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 |
||
| 171 | public static function get_webhook_status_message() { |
||
| 172 | $monitoring_began_at = self::get_monitoring_began_at(); |
||
| 173 | $last_success_at = self::get_last_webhook_success_at(); |
||
| 174 | $last_failure_at = self::get_last_webhook_failure_at(); |
||
| 175 | $last_error = self::get_last_error_reason(); |
||
| 176 | $test_mode = self::get_testmode(); |
||
| 177 | |||
| 178 | $date_format = 'Y-m-d H:i:s e'; |
||
| 179 | |||
| 180 | // Case 1 (Nominal case): Most recent = success |
||
| 181 | View Code Duplication | if ( $last_success_at > $last_failure_at ) { |
|
|
|
|||
| 182 | $message = sprintf( |
||
| 183 | $test_mode ? |
||
| 184 | /* translators: 1) date and time of last webhook received, e.g. 2020-06-28 10:30:50 UTC */ |
||
| 185 | __( 'The most recent test webhook, timestamped %s, was processed successfully.', 'woocommerce-gateway-stripe' ) : |
||
| 186 | /* translators: 1) date and time of last webhook received, e.g. 2020-06-28 10:30:50 UTC */ |
||
| 187 | __( 'The most recent live webhook, timestamped %s, was processed successfully.', 'woocommerce-gateway-stripe' ), |
||
| 188 | date( $date_format, $last_success_at ) |
||
| 189 | ); |
||
| 190 | return $message; |
||
| 191 | } |
||
| 192 | |||
| 193 | // Case 2: No webhooks received yet |
||
| 194 | if ( ( 0 == $last_success_at ) && ( 0 == $last_failure_at ) ) { |
||
| 195 | $message = sprintf( |
||
| 196 | $test_mode ? |
||
| 197 | /* translators: 1) date and time webhook monitoring began, e.g. 2020-06-28 10:30:50 UTC */ |
||
| 198 | __( 'No test webhooks have been received since monitoring began at %s.', 'woocommerce-gateway-stripe' ) : |
||
| 199 | /* translators: 1) date and time webhook monitoring began, e.g. 2020-06-28 10:30:50 UTC */ |
||
| 200 | __( 'No live webhooks have been received since monitoring began at %s.', 'woocommerce-gateway-stripe' ), |
||
| 201 | date( $date_format, $monitoring_began_at ) |
||
| 202 | ); |
||
| 203 | return $message; |
||
| 204 | } |
||
| 205 | |||
| 206 | // Case 3: Failure after success |
||
| 207 | View Code Duplication | if ( $last_success_at > 0 ) { |
|
| 208 | $message = sprintf( |
||
| 209 | $test_mode ? |
||
| 210 | /* translators: 1) date and time of last failed webhook e.g. 2020-06-28 10:30:50 UTC */ |
||
| 211 | /* translators: 2) reason webhook failed */ |
||
| 212 | /* translators: 3) date and time of last successful webhook e.g. 2020-05-28 10:30:50 UTC */ |
||
| 213 | __( 'Warning: The most recent test webhook, received at %s, could not be processed. Reason: %s. (The last test webhook to process successfully was timestamped %s.)', 'woocommerce-gateway-stripe' ) : |
||
| 214 | /* translators: 1) date and time of last failed webhook e.g. 2020-06-28 10:30:50 UTC */ |
||
| 215 | /* translators: 2) reason webhook failed */ |
||
| 216 | /* translators: 3) date and time of last successful webhook e.g. 2020-05-28 10:30:50 UTC */ |
||
| 217 | __( 'Warning: The most recent live webhook, received at %s, could not be processed. Reason: %s. (The last live webhook to process successfully was timestamped %s.)', 'woocommerce-gateway-stripe' ), |
||
| 218 | date( $date_format, $last_failure_at ), |
||
| 219 | $last_error, |
||
| 220 | date( $date_format, $last_success_at ) |
||
| 221 | ); |
||
| 222 | return $message; |
||
| 223 | } |
||
| 224 | |||
| 225 | // Case 4: Failure with no prior success |
||
| 226 | $message = sprintf( |
||
| 227 | $test_mode ? |
||
| 228 | /* translators: 1) date and time of last failed webhook e.g. 2020-06-28 10:30:50 UTC */ |
||
| 229 | /* translators: 2) reason webhook failed */ |
||
| 230 | /* translators: 3) date and time webhook monitoring began e.g. 2020-05-28 10:30:50 UTC */ |
||
| 231 | __( 'Warning: The most recent test webhook, received at %s, could not be processed. Reason: %s. (No test webhooks have been processed successfully since monitoring began at %s.)', 'woocommerce-gateway-stripe' ) : |
||
| 232 | /* translators: 1) date and time of last failed webhook e.g. 2020-06-28 10:30:50 UTC */ |
||
| 233 | /* translators: 2) reason webhook failed */ |
||
| 234 | /* translators: 3) date and time webhook monitoring began e.g. 2020-05-28 10:30:50 UTC */ |
||
| 235 | __( 'Warning: The most recent live webhook, received at %s, could not be processed. Reason: %s. (No live webhooks have been processed successfully since monitoring began at %s.)', 'woocommerce-gateway-stripe' ), |
||
| 236 | date( $date_format, $last_failure_at ), |
||
| 237 | $last_error, |
||
| 238 | date( $date_format, $monitoring_began_at ) |
||
| 239 | ); |
||
| 240 | return $message; |
||
| 241 | } |
||
| 242 | }; |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.