Skip to content

Commit ff888a8

Browse files
Refactor if else ladder to switch statement for readability
1 parent 37b58b1 commit ff888a8

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

mailchimp.php

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,15 +1046,15 @@ function mailchimp_sf_merge_submit( $mv ) {
10461046
*
10471047
* @param array $opt_val Option value
10481048
* @param array $data Data
1049-
* @return void|WP_Error
1049+
* @return array|WP_Error
10501050
*/
10511051
function mailchimp_sf_merge_validate_phone( $opt_val, $data ) {
10521052
// Filter out falsy values
10531053
$opt_val = array_filter( $opt_val );
10541054

10551055
// If they were all empty
10561056
if ( ! $opt_val ) {
1057-
return;
1057+
return $opt_val;
10581058
}
10591059

10601060
// Trim whitespace
@@ -1064,15 +1064,34 @@ function mailchimp_sf_merge_validate_phone( $opt_val, $data ) {
10641064
// Format number for validation
10651065
$opt_val = implode( '-', $opt_val );
10661066

1067-
// Check string length
1068-
if ( strlen( $opt_val ) < 12 ) {
1069-
/* translators: %s: field name */
1070-
$message = sprintf( esc_html__( '%s must contain the correct amount of digits', 'mailchimp' ), esc_html( $data['name'] ) );
1071-
$opt_val = new WP_Error( 'mc_phone_validation', $message );
1072-
} else if ( ! preg_match( '/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $opt_val ) ) {
1073-
/* translators: %s: field name */
1074-
$message = sprintf( esc_html__( '%s must consist of only numbers', 'mailchimp' ), esc_html( $data['name'] ) );
1075-
$opt_val = new WP_Error( 'mc_phone_validation', $message );
1067+
switch ( true ) {
1068+
/**
1069+
* Phone number must be 12 characters long
1070+
*/
1071+
case strlen( $opt_val ) < 12:
1072+
$message = sprintf(
1073+
esc_html__( '%s must contain the correct amount of digits', 'mailchimp' ),
1074+
esc_html( $data['name'] )
1075+
);
1076+
$opt_val = new WP_Error( 'mc_phone_validation', $message );
1077+
break;
1078+
1079+
/**
1080+
* Phone number must consist of only numbers
1081+
*/
1082+
case ! preg_match( '/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $opt_val ):
1083+
$message = sprintf(
1084+
esc_html__( '%s must consist of only numbers', 'mailchimp' ),
1085+
esc_html( $data['name'] )
1086+
);
1087+
$opt_val = new WP_Error( 'mc_phone_validation', $message );
1088+
break;
1089+
1090+
/**
1091+
* No issues, pass validation
1092+
*/
1093+
default:
1094+
break;
10761095
}
10771096

10781097
return $opt_val;

0 commit comments

Comments
 (0)