Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit 59a6b55

Browse files
committed
- Golden Path: Return Code Execution if an Error occurs instead of if/else
1 parent 14db29f commit 59a6b55

File tree

9 files changed

+43
-33
lines changed

9 files changed

+43
-33
lines changed

Sources/AuthenticationManager/Authentication/Sign In/Anonymous/Anonymous.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ extension AuthenticationManager {
2424
auth.signInAnonymously { _, error in
2525
if let error {
2626
completion(AuthenticationError.firebase(error: error))
27-
} else {
28-
print("Created account for anonymous user with id \(userId ?? "")")
29-
self.currentProvider = .anonymous
30-
completion(nil)
27+
return
3128
}
29+
30+
print("Created account for anonymous user with id \(userId ?? "")")
31+
self.currentProvider = .anonymous
32+
completion(nil)
3233
}
3334
}
3435
}

Sources/AuthenticationManager/Authentication/Sign In/EmailPassword/EmailPassword.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ extension AuthenticationManager {
1919
auth.createUser(withEmail: email, password: password) { _, error in
2020
if let error {
2121
completion(AuthenticationError.firebase(error: error))
22-
} else {
23-
print("Welcome user with id \(userId ?? "")")
24-
self.currentProvider = .emailPassword
25-
completion(nil)
22+
return
2623
}
24+
25+
print("Welcome user with id \(userId ?? "")")
26+
self.currentProvider = .emailPassword
27+
completion(nil)
2728
}
2829
}
2930

@@ -36,11 +37,12 @@ extension AuthenticationManager {
3637
auth.signIn(withEmail: email, password: password) { _, error in
3738
if let error {
3839
completion(AuthenticationError.firebase(error: error))
39-
} else {
40-
print("Welcome user with id \(userId ?? "")")
41-
self.currentProvider = .emailPassword
42-
completion(nil)
40+
return
4341
}
42+
43+
print("Welcome user with id \(userId ?? "")")
44+
self.currentProvider = .emailPassword
45+
completion(nil)
4446
}
4547
}
4648

@@ -109,7 +111,7 @@ extension AuthenticationManager {
109111

110112
reauthenticateWithEmail(password: currentPassword) { error in
111113
currentUser.delete { error in
112-
if let error = error {
114+
if let error {
113115
completion(AuthenticationError.firebase(error: error))
114116
return
115117
}

Sources/AuthenticationManager/AuthenticationProvider.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,19 @@ extension AuthenticationManager {
3434
If a user is already signed in but the provider key is missing (e.g. because of earlier version), we need to deduct the provider by checking the login methods
3535
*/
3636
private static func setDefaultProviderKey() {
37-
if hasUser {
38-
if userIsAuthenticated {
39-
if authorizationKey != nil {
40-
currentProvider = .signInWithApple
41-
} else {
42-
currentProvider = .emailPassword
43-
}
37+
guard hasUser else {
38+
currentProvider = nil
39+
return
40+
}
41+
42+
if userIsAuthenticated {
43+
if authorizationKey != nil {
44+
currentProvider = .signInWithApple
4445
} else {
45-
currentProvider = .anonymous
46+
currentProvider = .emailPassword
4647
}
4748
} else {
48-
currentProvider = nil
49+
currentProvider = .anonymous
4950
}
5051
}
5152
}

Sources/AuthenticationManager/Errors/ErrorHandling.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ extension AuthenticationManager {
3131
auth.signIn(with: linkedCredential) { result, error in
3232
if let error {
3333
self.handleError(error, completion: completion)
34-
} else {
35-
self.currentProvider = .signInWithApple
36-
completion(nil)
34+
return
3735
}
36+
37+
self.currentProvider = .signInWithApple
38+
completion(nil)
3839
}
3940
}
4041
default:

Sources/FirestoreManager/CollectionRepository.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ open class CollectionRepository<T: Codable>: ObservableObject {
6161
FirestoreManager.deleteDocument(id: id, reference: ref) { error in
6262
if let error {
6363
self.didReceiveError(error)
64-
} else {
65-
print("Object deleted \(self.getPath(id: id))!")
64+
return
6665
}
66+
67+
print("Object deleted \(self.getPath(id: id))!")
6768
}
6869
}
6970

Sources/FirestoreManager/FirestoreCRUD/Create.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ extension FirestoreManager {
5959
batch.commit { error in
6060
if let error {
6161
completion(.fail(error: error, action: .batchCreate, reference: reference, id: nil))
62-
} else {
63-
completion(nil)
62+
return
6463
}
64+
65+
completion(nil)
6566
}
6667
}
6768
}

Sources/FirestoreManager/FirestoreCRUD/Delete.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ extension FirestoreManager {
2020
try reference.reference().document(id).delete { error in
2121
if let error {
2222
completion?(.fail(error: error, action: .delete, reference: reference, id: id))
23-
} else {
24-
completion?(nil)
23+
return
2524
}
25+
26+
completion?(nil)
2627
}
2728
} catch {
2829
completion?(.incompleteReference(reference: reference))

Sources/FirestoreManager/FirestoreCRUD/Update.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ extension FirestoreManager {
4848
try reference.reference().document(id).setData(newValues, merge: true) { error in
4949
if let error {
5050
completion(.failure(.fail(error: error, action: .update, reference: reference, id: id)))
51-
} else {
52-
completion(.success(id))
51+
return
5352
}
53+
54+
completion(.success(id))
5455
}
5556
} catch {
5657
completion(.failure(.incompleteReference(reference: reference)))

Sources/StorageManager/FirebaseStorageManager.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ open class FirebaseStorageManager {
1919
let taskReference = storageRef.putFile(from: localFile, metadata: nil) { metadata, error in
2020
if let error {
2121
promise(.failure(.upload(error: error)))
22+
return
2223
}
2324
promise(.success(true))
2425
}

0 commit comments

Comments
 (0)