From 5d9e03ba40317964f6daf35bcf5aa58f4f5a6848 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 9 Jan 2019 16:19:31 -0700 Subject: [PATCH] convert to guard statements for readability --- SignalServiceKit/src/Contacts/PhoneNumber.m | 59 +++++++++++---------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/SignalServiceKit/src/Contacts/PhoneNumber.m b/SignalServiceKit/src/Contacts/PhoneNumber.m index db530e2de..a0a561600 100644 --- a/SignalServiceKit/src/Contacts/PhoneNumber.m +++ b/SignalServiceKit/src/Contacts/PhoneNumber.m @@ -324,35 +324,38 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN // Order matters; better results should appear first so prefer // matches with the same country code as this client's phone number. - OWSAssertDebug(clientPhoneNumber.length > 0); - if (clientPhoneNumber.length > 0) { - // Note that NBPhoneNumber uses "country code" to refer to what we call a - // "calling code" (i.e. 44 in +44123123). Within SSK we use "country code" - // (and sometimes "region code") to refer to a country's ISO 2-letter code - // (ISO 3166-1 alpha-2). - NSNumber *callingCodeForLocalNumber = [[PhoneNumber phoneNumberFromE164:clientPhoneNumber] getCountryCode]; - if (callingCodeForLocalNumber != nil) { - NSString *callingCodePrefix = [NSString stringWithFormat:@"+%@", callingCodeForLocalNumber]; - - tryParsingWithCountryCode( - [callingCodePrefix stringByAppendingString:sanitizedString], [self defaultCountryCode]); - - // Try to determine what the country code is for the local phone number - // and also try parsing the phone number using that country code if it - // differs from the device's region code. - // - // For example, a French person living in Italy might have an - // Italian phone number but use French region/language for their - // phone. They're likely to have both Italian and French contacts. - NSString *localCountryCode = - [PhoneNumberUtil.sharedThreadLocal probableCountryCodeForCallingCode:callingCodePrefix]; - if (localCountryCode && ![localCountryCode isEqualToString:[self defaultCountryCode]]) { - tryParsingWithCountryCode( - [callingCodePrefix stringByAppendingString:sanitizedString], localCountryCode); - } - } + if (clientPhoneNumber.length == 0) { + OWSFailDebug(@"clientPhoneNumber had unexpected length"); + return result; } - + + // Note that NBPhoneNumber uses "country code" to refer to what we call a + // "calling code" (i.e. 44 in +44123123). Within SSK we use "country code" + // (and sometimes "region code") to refer to a country's ISO 2-letter code + // (ISO 3166-1 alpha-2). + NSNumber *callingCodeForLocalNumber = [[PhoneNumber phoneNumberFromE164:clientPhoneNumber] getCountryCode]; + if (callingCodeForLocalNumber == nil) { + OWSFailDebug(@"callingCodeForLocalNumber was unexpectedly nil"); + return result; + } + + NSString *callingCodePrefix = [NSString stringWithFormat:@"+%@", callingCodeForLocalNumber]; + + tryParsingWithCountryCode([callingCodePrefix stringByAppendingString:sanitizedString], [self defaultCountryCode]); + + // Try to determine what the country code is for the local phone number + // and also try parsing the phone number using that country code if it + // differs from the device's region code. + // + // For example, a French person living in Italy might have an + // Italian phone number but use French region/language for their + // phone. They're likely to have both Italian and French contacts. + NSString *localCountryCode = + [PhoneNumberUtil.sharedThreadLocal probableCountryCodeForCallingCode:callingCodePrefix]; + if (localCountryCode && ![localCountryCode isEqualToString:[self defaultCountryCode]]) { + tryParsingWithCountryCode([callingCodePrefix stringByAppendingString:sanitizedString], localCountryCode); + } + return result; }