Skip to content

Commit

Permalink
Issue iziz#391 - Adding nullability specifiers to NBPhoneNumberUtil f…
Browse files Browse the repository at this point in the history
…unctions

* Address analyzer warnings related to passing nil to non-nil parameters

* Fix build failure with 'ShortNumber' test target
  • Loading branch information
LowAmmo committed May 12, 2023
1 parent 8327e0d commit 121a673
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 342 deletions.
2 changes: 0 additions & 2 deletions libPhoneNumber.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
940725A924BE77420011AE05 /* NBShortNumberUtil.h in Headers */ = {isa = PBXBuildFile; fileRef = 940725A724BE77420011AE05 /* NBShortNumberUtil.h */; settings = {ATTRIBUTES = (Public, ); }; };
940725AA24BE77420011AE05 /* NBShortNumberUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 940725A824BE77420011AE05 /* NBShortNumberUtil.m */; };
940725AC24BF63050011AE05 /* NBShortNumberInfoTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 940725AB24BF63050011AE05 /* NBShortNumberInfoTest.m */; };
940725AD24BF63500011AE05 /* NBMetadataHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = FD12C2681A87401B00B53856 /* NBMetadataHelper.h */; settings = {ATTRIBUTES = (Public, ); }; };
940725AF24BF79BE0011AE05 /* NBGeneratedShortNumberMetadata.h in Headers */ = {isa = PBXBuildFile; fileRef = 940725AE24BF79BE0011AE05 /* NBGeneratedShortNumberMetadata.h */; settings = {ATTRIBUTES = (Private, ); }; };
940725B224BF7B040011AE05 /* NBShortNumberTestHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 940725B124BF7B040011AE05 /* NBShortNumberTestHelper.m */; };
940725B524BF90290011AE05 /* NBShortNumberMetadataHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 940725B324BF90290011AE05 /* NBShortNumberMetadataHelper.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -503,7 +502,6 @@
940725A924BE77420011AE05 /* NBShortNumberUtil.h in Headers */,
9407259B24BE768A0011AE05 /* libPhoneNumberShortNumber.h in Headers */,
940725AF24BF79BE0011AE05 /* NBGeneratedShortNumberMetadata.h in Headers */,
940725AD24BF63500011AE05 /* NBMetadataHelper.h in Headers */,
940725B524BF90290011AE05 /* NBShortNumberMetadataHelper.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
6 changes: 3 additions & 3 deletions libPhoneNumber/NBMetadataHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ - (NSString *)countryCodeFromRegionCode:(NSString *)regionCode {
* @param {?string} regionCode
* @return {i18n.phonenumbers.PhoneMetadata}
*/
- (NBPhoneMetaData *)getMetadataForRegion:(NSString *)regionCode {
- (NBPhoneMetaData * _Nullable)getMetadataForRegion:(NSString * _Nonnull)regionCode {
regionCode = StringByTrimming(regionCode);
if (regionCode.length == 0) {
return nil;
Expand Down Expand Up @@ -178,12 +178,12 @@ - (NBPhoneMetaData *)getMetadataForRegion:(NSString *)regionCode {
* @param countryCallingCode countryCallingCode
* @return {i18n.phonenumbers.PhoneMetadata}
*/
- (NBPhoneMetaData *)getMetadataForNonGeographicalRegion:(NSNumber *)countryCallingCode {
- (NBPhoneMetaData * _Nullable)getMetadataForNonGeographicalRegion:(NSNumber * _Nonnull)countryCallingCode {
NSString *countryCallingCodeStr = countryCallingCode.stringValue;
return [self getMetadataForRegion:countryCallingCodeStr];
}

+ (BOOL)hasValue:(NSString *)string {
+ (BOOL)hasValue:(NSString * _Nonnull)string {
string = StringByTrimming(string);
return string.length != 0;
}
Expand Down
233 changes: 118 additions & 115 deletions libPhoneNumber/NBPhoneNumberUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,126 +13,128 @@

@interface NBPhoneNumberUtil : NSObject

+ (NBPhoneNumberUtil *)sharedInstance;
- (instancetype)initWithMetadataHelper:(NBMetadataHelper *)helper;
+ (NBPhoneNumberUtil * _Nonnull)sharedInstance;
- (instancetype _Nonnull)initWithMetadataHelper:(NBMetadataHelper * _Nonnull)helper;

- (instancetype)init NS_UNAVAILABLE;
- (instancetype _Nonnull)init NS_UNAVAILABLE;

@property(nonatomic, strong, readonly) NSDictionary *DIGIT_MAPPINGS;
@property(nonatomic, strong, readonly, nonnull) NSDictionary * DIGIT_MAPPINGS;

// regular expressions
- (NSArray *)matchesByRegex:(NSString *)sourceString regex:(NSString *)pattern;
- (NSArray *)matchedStringByRegex:(NSString *)sourceString regex:(NSString *)pattern;
- (NSString *)replaceStringByRegex:(NSString *)sourceString
regex:(NSString *)pattern
withTemplate:(NSString *)templateString;
- (int)stringPositionByRegex:(NSString *)sourceString regex:(NSString *)pattern;
- (NSArray * _Nullable)matchesByRegex:(NSString * _Nonnull)sourceString regex:(NSString * _Nonnull)pattern;
- (NSArray * _Nullable)matchedStringByRegex:(NSString * _Nonnull)sourceString regex:(NSString * _Nonnull)pattern;
- (NSString * _Nonnull)replaceStringByRegex:(NSString * _Nonnull)sourceString
regex:(NSString * _Nonnull)pattern
withTemplate:(NSString * _Nonnull)templateString;
- (int)stringPositionByRegex:(NSString * _Nullable)sourceString regex:(NSString * _Nullable)pattern;

// libPhoneNumber Util functions
- (NSString *)convertAlphaCharactersInNumber:(NSString *)number;

- (NSString *)normalize:(NSString *)phoneNumber;
- (NSString *)normalizeDigitsOnly:(NSString *)number;
- (NSString *)normalizeDiallableCharsOnly:(NSString *)number;

- (BOOL)isNumberGeographical:(NBPhoneNumber *)phoneNumber;

- (NSString *)extractPossibleNumber:(NSString *)phoneNumber;
- (NSNumber *)extractCountryCode:(NSString *)fullNumber nationalNumber:(NSString **)nationalNumber;
- (NSString *)countryCodeByCarrier;

- (NSString *)getNddPrefixForRegion:(NSString *)regionCode stripNonDigits:(BOOL)stripNonDigits;
- (NSString *)getNationalSignificantNumber:(NBPhoneNumber *)phoneNumber;

- (NSArray *)getSupportedRegions;

- (NBEPhoneNumberType)getNumberType:(NBPhoneNumber *)phoneNumber;

- (NSNumber *)getCountryCodeForRegion:(NSString *)regionCode;

- (NSString *)getRegionCodeForCountryCode:(NSNumber *)countryCallingCode;
- (NSArray *)getRegionCodesForCountryCode:(NSNumber *)countryCallingCode;
- (NSString *)getRegionCodeForNumber:(NBPhoneNumber *)phoneNumber;

- (NBPhoneNumber *)getExampleNumber:(NSString *)regionCode error:(NSError **)error;
- (NBPhoneNumber *)getExampleNumberForType:(NSString *)regionCode
type:(NBEPhoneNumberType)type
error:(NSError **)error;
- (NBPhoneNumber *)getExampleNumberForNonGeoEntity:(NSNumber *)countryCallingCode
error:(NSError **)error;

- (BOOL)canBeInternationallyDialled:(NBPhoneNumber *)number error:(NSError **)error;

- (BOOL)truncateTooLongNumber:(NBPhoneNumber *)number;

- (BOOL)isValidNumber:(NBPhoneNumber *)number;
- (BOOL)isViablePhoneNumber:(NSString *)phoneNumber;
- (BOOL)isAlphaNumber:(NSString *)number;
- (BOOL)isValidNumberForRegion:(NBPhoneNumber *)number regionCode:(NSString *)regionCode;
- (BOOL)isNANPACountry:(NSString *)regionCode;
- (BOOL)isLeadingZeroPossible:(NSNumber *)countryCallingCode;

- (NBEValidationResult)isPossibleNumberWithReason:(NBPhoneNumber *)number error:(NSError **)error;

- (BOOL)isPossibleNumber:(NBPhoneNumber *)number;
- (BOOL)isPossibleNumber:(NBPhoneNumber *)number error:(NSError **)error;
- (BOOL)isPossibleNumberString:(NSString *)number
regionDialingFrom:(NSString *)regionDialingFrom
error:(NSError **)error;

- (NBEMatchType)isNumberMatch:(id)firstNumberIn second:(id)secondNumberIn error:(NSError **)error;

- (int)getLengthOfGeographicalAreaCode:(NBPhoneNumber *)phoneNumber error:(NSError **)error;
- (int)getLengthOfNationalDestinationCode:(NBPhoneNumber *)phoneNumber error:(NSError **)error;

- (BOOL)maybeStripNationalPrefixAndCarrierCode:(NSString **)numberStr
metadata:(NBPhoneMetaData *)metadata
carrierCode:(NSString **)carrierCode;
- (NBECountryCodeSource)maybeStripInternationalPrefixAndNormalize:(NSString **)numberStr
possibleIddPrefix:(NSString *)possibleIddPrefix;

- (NSNumber *)maybeExtractCountryCode:(NSString *)number
metadata:(NBPhoneMetaData *)defaultRegionMetadata
nationalNumber:(NSString **)nationalNumber
keepRawInput:(BOOL)keepRawInput
phoneNumber:(NBPhoneNumber **)phoneNumber
error:(NSError **)error;

- (NBPhoneNumber *)parse:(NSString *)numberToParse
defaultRegion:(NSString *)defaultRegion
error:(NSError **)error;
- (NBPhoneNumber *)parseAndKeepRawInput:(NSString *)numberToParse
defaultRegion:(NSString *)defaultRegion
error:(NSError **)error;
- (NBPhoneNumber *)parseWithPhoneCarrierRegion:(NSString *)numberToParse error:(NSError **)error;

- (NSString *)format:(NBPhoneNumber *)phoneNumber
numberFormat:(NBEPhoneNumberFormat)numberFormat
error:(NSError **)error;
- (NSString *)formatByPattern:(NBPhoneNumber *)number
numberFormat:(NBEPhoneNumberFormat)numberFormat
userDefinedFormats:(NSArray *)userDefinedFormats
error:(NSError **)error;
- (NSString *)formatNumberForMobileDialing:(NBPhoneNumber *)number
regionCallingFrom:(NSString *)regionCallingFrom
withFormatting:(BOOL)withFormatting
error:(NSError **)error;
- (NSString *)formatOutOfCountryCallingNumber:(NBPhoneNumber *)number
regionCallingFrom:(NSString *)regionCallingFrom
error:(NSError **)error;
- (NSString *)formatOutOfCountryKeepingAlphaChars:(NBPhoneNumber *)number
regionCallingFrom:(NSString *)regionCallingFrom
error:(NSError **)error;
- (NSString *)formatNationalNumberWithCarrierCode:(NBPhoneNumber *)number
carrierCode:(NSString *)carrierCode
error:(NSError **)error;
- (NSString *)formatInOriginalFormat:(NBPhoneNumber *)number
regionCallingFrom:(NSString *)regionCallingFrom
error:(NSError **)error;
- (NSString *)formatNationalNumberWithPreferredCarrierCode:(NBPhoneNumber *)number
fallbackCarrierCode:(NSString *)fallbackCarrierCode
error:(NSError **)error;
- (BOOL)formattingRuleHasFirstGroupOnly:(NSString *)nationalPrefixFormattingRule;
- (NSString * _Nonnull)convertAlphaCharactersInNumber:(NSString * _Nonnull)number;

- (NSString * _Nonnull)normalize:(NSString * _Nonnull)number;
- (NSString * _Nonnull)normalizeDigitsOnly:(NSString * _Nonnull)number;
- (NSString * _Nonnull)normalizeDiallableCharsOnly:(NSString * _Nonnull)number;

- (BOOL)isNumberGeographical:(NBPhoneNumber * _Nonnull)phoneNumber;

- (NSString * _Nonnull)extractPossibleNumber:(NSString * _Nonnull)number;
- (NSNumber * _Nonnull)extractCountryCode:(NSString * _Nonnull)fullNumber nationalNumber:(NSString * _Nullable * _Nullable)nationalNumber;
- (NSString * _Nonnull)countryCodeByCarrier;

- (NSString * _Nullable)getNddPrefixForRegion:(NSString * _Nullable)regionCode stripNonDigits:(BOOL)stripNonDigits;
- (NSString * _Nonnull)getNationalSignificantNumber:(NBPhoneNumber * _Nonnull)phoneNumber;

- (NSArray * _Nullable)getSupportedRegions;

- (NBEPhoneNumberType)getNumberType:(NBPhoneNumber * _Nonnull)phoneNumber;

- (NSNumber * _Nonnull)getCountryCodeForRegion:(NSString * _Nullable)regionCode;

- (NSString * _Nonnull)getRegionCodeForCountryCode:(NSNumber * _Nonnull)countryCallingCode;
- (NSArray * _Nullable)getRegionCodesForCountryCode:(NSNumber * _Nonnull)countryCallingCode;
- (NSString * _Nullable)getRegionCodeForNumber:(NBPhoneNumber * _Nullable)phoneNumber;

- (NBPhoneNumber * _Nullable)getExampleNumber:(NSString * _Nonnull)regionCode error:(NSError * _Nullable __autoreleasing * _Nullable)error;
- (NBPhoneNumber * _Nullable)getExampleNumberForType:(NSString * _Nonnull)regionCode
type:(NBEPhoneNumberType)type
error:(NSError * _Nullable __autoreleasing * _Nullable)error;
- (NBPhoneNumber * _Nullable)getExampleNumberForNonGeoEntity:(NSNumber * _Nonnull)countryCallingCode
error:(NSError * _Nullable * _Nullable)error;

- (BOOL)canBeInternationallyDialled:(NBPhoneNumber * _Nonnull)number error:(NSError * _Nullable * _Nullable)error;

- (BOOL)truncateTooLongNumber:(NBPhoneNumber * _Nonnull)number;

- (BOOL)isValidNumber:(NBPhoneNumber * _Nonnull)number;
- (BOOL)isViablePhoneNumber:(NSString * _Nonnull)phoneNumber;
- (BOOL)isAlphaNumber:(NSString * _Nonnull)number;
- (BOOL)isValidNumberForRegion:(NBPhoneNumber * _Nonnull)number regionCode:(NSString * _Nonnull)regionCode;
- (BOOL)isNANPACountry:(NSString * _Nullable)regionCode;
- (BOOL)isLeadingZeroPossible:(NSNumber * _Nonnull)countryCallingCode;

- (NBEValidationResult)isPossibleNumberWithReason:(NBPhoneNumber * _Nonnull)number
error:(NSError * _Nullable * _Nullable)error;

- (BOOL)isPossibleNumber:(NBPhoneNumber * _Nonnull)number;
- (BOOL)isPossibleNumber:(NBPhoneNumber * _Nonnull)number error:(NSError * _Nullable * _Nullable)error;
- (BOOL)isPossibleNumberString:(NSString * _Nonnull)number
regionDialingFrom:(NSString * _Nullable)regionDialingFrom
error:(NSError * _Nullable * _Nullable)error;

- (NBEMatchType)isNumberMatch:(id _Nonnull)firstNumberIn second:(id _Nonnull)secondNumberIn error:(NSError * _Nullable * _Nullable)error;

- (int)getLengthOfGeographicalAreaCode:(NBPhoneNumber * _Nonnull)phoneNumber error:(NSError * _Nullable * _Nullable)error;
- (int)getLengthOfNationalDestinationCode:(NBPhoneNumber * _Nonnull)phoneNumber error:(NSError * _Nullable * _Nullable)error;

- (BOOL)maybeStripNationalPrefixAndCarrierCode:(NSString * _Nullable * _Nullable)number
metadata:(NBPhoneMetaData * _Nonnull)metadata
carrierCode:(NSString * _Nullable * _Nullable)carrierCode;
- (NBECountryCodeSource)maybeStripInternationalPrefixAndNormalize:(NSString * _Nullable * _Nullable)numberStr
possibleIddPrefix:(NSString * _Nonnull)possibleIddPrefix;

- (NSNumber * _Nonnull)maybeExtractCountryCode:(NSString * _Nonnull)number
metadata:(NBPhoneMetaData * _Nullable)defaultRegionMetadata
nationalNumber:(NSString * _Nullable * _Nullable)nationalNumber
keepRawInput:(BOOL)keepRawInput
phoneNumber:(NBPhoneNumber * _Nullable * _Nullable)phoneNumber
error:(NSError * _Nullable * _Nullable)error;

- (NBPhoneNumber * _Nullable)parse:(NSString * _Nullable)numberToParse
defaultRegion:(NSString * _Nullable)defaultRegion
error:(NSError * _Nullable * _Nullable)error;
- (NBPhoneNumber * _Nullable)parseAndKeepRawInput:(NSString * _Nonnull)numberToParse
defaultRegion:(NSString * _Nullable)defaultRegion
error:(NSError * _Nullable * _Nullable)error;
- (NBPhoneNumber * _Nullable)parseWithPhoneCarrierRegion:(NSString * _Nullable)numberToParse
error:(NSError * _Nullable * _Nullable)error;

- (NSString * _Nullable)format:(NBPhoneNumber * _Nonnull)phoneNumber
numberFormat:(NBEPhoneNumberFormat)numberFormat
error:(NSError * _Nullable * _Nullable)error;
- (NSString * _Nullable)formatByPattern:(NBPhoneNumber * _Nonnull)number
numberFormat:(NBEPhoneNumberFormat)numberFormat
userDefinedFormats:(NSArray * _Nullable)userDefinedFormats
error:(NSError * _Nullable * _Nullable)error;
- (NSString * _Nullable)formatNumberForMobileDialing:(NBPhoneNumber * _Nonnull)number
regionCallingFrom:(NSString * _Nonnull)regionCallingFrom
withFormatting:(BOOL)withFormatting
error:(NSError * _Nullable * _Nullable)error;
- (NSString * _Nullable)formatOutOfCountryCallingNumber:(NBPhoneNumber * _Nonnull)number
regionCallingFrom:(NSString * _Nonnull)regionCallingFrom
error:(NSError * _Nullable * _Nullable)error;
- (NSString * _Nullable)formatOutOfCountryKeepingAlphaChars:(NBPhoneNumber * _Nonnull)number
regionCallingFrom:(NSString * _Nonnull)regionCallingFrom
error:(NSError * _Nullable * _Nullable)error;
- (NSString * _Nullable)formatNationalNumberWithCarrierCode:(NBPhoneNumber * _Nonnull)number
carrierCode:(NSString * _Nullable)carrierCode
error:(NSError * _Nullable * _Nullable)error;
- (NSString * _Nullable)formatInOriginalFormat:(NBPhoneNumber * _Nonnull)number
regionCallingFrom:(NSString * _Nonnull)regionCallingFrom
error:(NSError * _Nullable * _Nullable)error;
- (NSString * _Nullable)formatNationalNumberWithPreferredCarrierCode:(NBPhoneNumber * _Nonnull)number
fallbackCarrierCode:(NSString * _Nonnull)fallbackCarrierCode
error:(NSError * _Nullable * _Nullable)error;
- (BOOL)formattingRuleHasFirstGroupOnly:(NSString * _Nullable)nationalPrefixFormattingRule;

/**
* Returns the mobile token for the provided country calling code if it has one, otherwise
Expand All @@ -142,6 +144,7 @@
* @param countryCallingCode the country calling code for which we want the mobile token.
* @return the mobile token, as a string, for the given country calling code.
*/
- (NSString *)getCountryMobileTokenFromCountryCode:(NSInteger)countryCallingCode;
- (NSString * _Nonnull)getCountryMobileTokenFromCountryCode:(NSInteger)countryCallingCode;

@end

Loading

0 comments on commit 121a673

Please sign in to comment.