diff --git a/.swiftlint.yml b/.swiftlint.yml index 3a07daa9eb53..672be8c89f09 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -19,6 +19,9 @@ only_rules: # Prefer `contains` over using `filter(where:).isEmpty`. - contains_over_filter_is_empty + # Prefer `contains` over `range(of:) != nil` and `range(of:) == nil`. + - contains_over_range_nil_comparison + # if,for,while,do statements shouldn't wrap their conditionals in parentheses. - control_statement diff --git a/Modules/Sources/WordPressKit/WordPressOrgXMLRPCValidator.swift b/Modules/Sources/WordPressKit/WordPressOrgXMLRPCValidator.swift index 51d82b5701aa..e084fe928a25 100644 --- a/Modules/Sources/WordPressKit/WordPressOrgXMLRPCValidator.swift +++ b/Modules/Sources/WordPressKit/WordPressOrgXMLRPCValidator.swift @@ -231,8 +231,8 @@ open class WordPressOrgXMLRPCValidator: NSObject { if httpResponse?.url != url { // we where redirected, let's check the answer content if let data = (error as NSError).userInfo[WordPressOrgXMLRPCApi.WordPressOrgXMLRPCApiErrorKeyData as String] as? Data, - let responseString = String(data: data, encoding: String.Encoding.utf8), responseString.range(of: "") != nil - || responseString.range(of: "dm404Container") != nil { + let responseString = String(data: data, encoding: String.Encoding.utf8), responseString.contains("") + || responseString.contains("dm404Container") { failure(WordPressOrgXMLRPCValidatorError.mobilePluginRedirectedError as NSError) return } diff --git a/Sources/WordPressAuthenticator/Helpers/Authenticator/WordPressAuthenticator.swift b/Sources/WordPressAuthenticator/Helpers/Authenticator/WordPressAuthenticator.swift index 66a82c817966..40e13ba6fc86 100644 --- a/Sources/WordPressAuthenticator/Helpers/Authenticator/WordPressAuthenticator.swift +++ b/Sources/WordPressAuthenticator/Helpers/Authenticator/WordPressAuthenticator.swift @@ -491,7 +491,7 @@ import WordPressKit if isSiteURLSchemeEmpty { path = "https://\(path)" - } else if path.isWordPressComPath() && path.range(of: "http://") != nil { + } else if path.isWordPressComPath() && path.contains("http://") { path = path.replacingOccurrences(of: "http://", with: "https://") } diff --git a/Tests/KeystoneTests/Helpers/OHHTTPStubs+Helpers.swift b/Tests/KeystoneTests/Helpers/OHHTTPStubs+Helpers.swift index d28ab7403e73..69c74f27637f 100644 --- a/Tests/KeystoneTests/Helpers/OHHTTPStubs+Helpers.swift +++ b/Tests/KeystoneTests/Helpers/OHHTTPStubs+Helpers.swift @@ -7,7 +7,7 @@ import OHHTTPStubsSwift public extension HTTPStubs { static func stubRequest(forEndpoint endpoint: String, withFileAtPath path: String) { stub(condition: { request in - return request.url?.absoluteString.range(of: endpoint) != nil + return request.url?.absoluteString.contains(endpoint) ?? false }) { _ in return fixture(filePath: path, headers: ["Content-Type" as NSObject: "application/json" as AnyObject]) } diff --git a/Tests/KeystoneTests/Tests/Services/NotificationSettingsServiceTests.swift b/Tests/KeystoneTests/Tests/Services/NotificationSettingsServiceTests.swift index 43a272f1fbc1..ef2e37aa8e0c 100644 --- a/Tests/KeystoneTests/Tests/Services/NotificationSettingsServiceTests.swift +++ b/Tests/KeystoneTests/Tests/Services/NotificationSettingsServiceTests.swift @@ -29,8 +29,8 @@ class NotificationSettingsServiceTests: CoreDataTestCase { service = NotificationSettingsService(coreDataStack: contextManager, wordPressComRestApi: remoteApi) stub(condition: { request in - return request.url?.absoluteString.range(of: self.settingsEndpoint) != nil - && request.httpMethod! == "GET" + let matchesEndpoint = request.url?.absoluteString.contains(self.settingsEndpoint) ?? false + return matchesEndpoint && request.httpMethod! == "GET" }) { _ in let stubPath = OHPathForFile(self.settingsFilename, type(of: self)) return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: self.contentTypeJson as AnyObject]) diff --git a/Tests/WordPressKitTests/WordPressKitTests/Tests/RemoteTestCase.swift b/Tests/WordPressKitTests/WordPressKitTests/Tests/RemoteTestCase.swift index 00422374ac8f..20d4cd3113c2 100644 --- a/Tests/WordPressKitTests/WordPressKitTests/Tests/RemoteTestCase.swift +++ b/Tests/WordPressKitTests/WordPressKitTests/Tests/RemoteTestCase.swift @@ -91,7 +91,7 @@ extension RemoteTestCase { } stub(condition: { request in - return request.url?.absoluteString.range(of: endpoint) != nil + return request.url?.absoluteString.contains(endpoint) ?? false }) { _ in var headers: [NSObject: AnyObject]? @@ -112,7 +112,7 @@ extension RemoteTestCase { /// func stubRemoteResponse(_ endpoint: String, data: Data, contentType: ResponseContentType, status: Int32 = 200) { stub(condition: { request in - return request.url?.absoluteString.range(of: endpoint) != nil + return request.url?.absoluteString.contains(endpoint) ?? false }) { _ in var headers: [NSObject: AnyObject]? @@ -140,7 +140,7 @@ extension RemoteTestCase { func stubRemoteResponse(_ endpoint: String, files: [String], contentType: ResponseContentType, status: Int32 = 200) { var callCounter = 0 stub(condition: { request in - return request.url?.absoluteString.range(of: endpoint) != nil + return request.url?.absoluteString.contains(endpoint) ?? false }) { response in guard files.indices.contains(callCounter) else { // An extra call was made to this stub and no corresponding response file existed.