From 4adbe2ebda7beca0cfa0ac1b0ab2e1a3da446b15 Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Wed, 1 Jan 2025 16:23:33 +0200 Subject: [PATCH 1/4] File to be tested --- test.swift | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 test.swift diff --git a/test.swift b/test.swift new file mode 100644 index 0000000..ff009ca --- /dev/null +++ b/test.swift @@ -0,0 +1,26 @@ +struct userAccount{ + var userName:String +var userAge:Int + var isActive : Bool + + func printUserDetails(){print("\(userName) is \(userAge) years old") + if(isActive==true){ + print("Account is active") + } + else { + print("Account is inactive") + } + } + + +func calculateDiscount()->Double{ + var discount:Double = 0.0 + if userAge>60{discount=0.2} + else if(userAge>30){ + discount = 0.1 + } + return discount +}} + +let newUser=userAccount(userName: "john",userAge:25,isActive:true) +newUser.printUserDetails() From 99878d9eba28bdd41a79619639c4b27d5ec8ee5b Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Wed, 1 Jan 2025 17:49:46 +0200 Subject: [PATCH 2/4] Refactored files for lint --- .../Factory/factory-practice-2.swift | 16 ++++---- test.swift | 37 ++++++++++--------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/DesignPatterns/Factory/factory-practice-2.swift b/DesignPatterns/Factory/factory-practice-2.swift index 0fe5090..b2f5acd 100644 --- a/DesignPatterns/Factory/factory-practice-2.swift +++ b/DesignPatterns/Factory/factory-practice-2.swift @@ -4,10 +4,10 @@ protocol Character { func attack() func defend() - var HP: Int { get } - var MP: Int { get } - var weaponType: String { get } - var armorType: String { get } + var HP:Int { get } + var MP:Int { get } + var weaponType:String { get } + var armorType:String { get } } enum CharacterType { @@ -30,19 +30,19 @@ class Warrior: Character { print("Warrior is defending.") } - var HP: Int { + var HP:Int { return 100 } - var MP: Int { + var MP:Int { return 57 } - var weaponType: String { + var weaponType:String { return "Sword" } - var armorType: String { + var armorType:String { return "Strong" } } diff --git a/test.swift b/test.swift index ff009ca..db560ea 100644 --- a/test.swift +++ b/test.swift @@ -1,26 +1,27 @@ -struct userAccount{ - var userName:String -var userAge:Int - var isActive : Bool - - func printUserDetails(){print("\(userName) is \(userAge) years old") - if(isActive==true){ +struct userAccount { + var userName: String + var userAge: Int + var isActive: Bool + + func printUserDetails() { + print("\(userName) is \(userAge) years old") + if isActive == true { print("Account is active") - } - else { + } else { print("Account is inactive") } } - - -func calculateDiscount()->Double{ - var discount:Double = 0.0 - if userAge>60{discount=0.2} - else if(userAge>30){ + + func calculateDiscount() -> Double { + var discount: Double = 0.0 + if userAge > 60 { + discount = 0.2 + } else if userAge > 30 { discount = 0.1 + } + return discount } - return discount -}} +} -let newUser=userAccount(userName: "john",userAge:25,isActive:true) +let newUser = userAccount(userName: "john", userAge: 25, isActive: true) newUser.printUserDetails() From 65bc8872ae2fca50e750bd7af77a83ca507d66ab Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Wed, 1 Jan 2025 18:07:45 +0200 Subject: [PATCH 3/4] Updated lint and automation --- .github/workflows/swift-lint.yml | 11 +++--- .../Factory/factory-practice-2.swift | 26 +++++++------- DesignPatterns/Factory/factory-practice.swift | 36 +++++++++---------- DesignPatterns/Factory/factory.swift | 8 ++--- Sources/.swiftlint.yml | 18 ++++++++-- 5 files changed, 54 insertions(+), 45 deletions(-) diff --git a/.github/workflows/swift-lint.yml b/.github/workflows/swift-lint.yml index 37342b4..45b58d8 100644 --- a/.github/workflows/swift-lint.yml +++ b/.github/workflows/swift-lint.yml @@ -1,17 +1,14 @@ +# .github/workflows/swiftlint.yml name: Swift Lint - on: push: branches: [ main ] pull_request: branches: [ main ] - jobs: lint: runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - - name: Run SwiftLint - uses: norio-nomura/action-swiftlint@3.2.1 + - uses: actions/checkout@v3 + - name: Run SwiftLint + uses: norio-nomura/action-swiftlint@3.2.1 \ No newline at end of file diff --git a/DesignPatterns/Factory/factory-practice-2.swift b/DesignPatterns/Factory/factory-practice-2.swift index b2f5acd..183705c 100644 --- a/DesignPatterns/Factory/factory-practice-2.swift +++ b/DesignPatterns/Factory/factory-practice-2.swift @@ -4,10 +4,10 @@ protocol Character { func attack() func defend() - var HP:Int { get } - var MP:Int { get } - var weaponType:String { get } - var armorType:String { get } + var HP: Int { get } + var MP: Int { get } + var weaponType: String { get } + var armorType: String { get } } enum CharacterType { @@ -30,30 +30,30 @@ class Warrior: Character { print("Warrior is defending.") } - var HP:Int { + var HP: Int { return 100 } - var MP:Int { + var MP: Int { return 57 } - var weaponType:String { + var weaponType: String { return "Sword" } - var armorType:String { + var armorType: String { return "Strong" } } -class CharacterFactory { +class CharacterFactory { static func createCharacter(character: CharacterType) throws -> Character { switch character { - case .warrior: - return Warrior() - default: - throw CharacterError.invalidCharacterType + case .warrior: + return Warrior() + default: + throw CharacterError.invalidCharacterType } } } diff --git a/DesignPatterns/Factory/factory-practice.swift b/DesignPatterns/Factory/factory-practice.swift index 1accb4c..ad1ffad 100644 --- a/DesignPatterns/Factory/factory-practice.swift +++ b/DesignPatterns/Factory/factory-practice.swift @@ -5,13 +5,13 @@ protocol Document { /// Opens the document from the specified file path /// - Parameter filePath: The path to the document func openDocument(filePath: String) - + /// Saves the document func saveDocument() - + /// The file extension for this document type var fileExtension: String { get } - + /// The size of the document in kilobytes var fileSizeKB: Int { get } } @@ -28,15 +28,15 @@ class PDF: Document { print("PDF file has been opened") print("Reading file..") } - - func saveDocument(){ + + func saveDocument() { print("PDF document has been saved") } - + var fileExtension: String { return ".pdf" } - + var fileSizeKB: Int { return 32 } @@ -47,15 +47,15 @@ class Word: Document { print("Word document has been opened") print("Reading file...") } - - func saveDocument(){ + + func saveDocument() { print("Word document has been saved") } - + var fileExtension: String { return ".word" } - + var fileSizeKB: Int { return 64 } @@ -66,15 +66,15 @@ class Text: Document { print("Text document has been opened") print("Reading file...") } - + func saveDocument() { print("Text document has been saved") } - + var fileExtension: String { return ".txt" } - + var fileSizeKB: Int { return 16 } @@ -103,19 +103,19 @@ class DocumentFactory { // Processor class DocumentProcessor { private let document: Document - + init(documentType: DocumentType) throws { self.document = try DocumentFactory.createDocument(fileExtension: documentType) } - + func processDocument(filePath: String) throws { guard !filePath.isEmpty else { throw DocumentError.invalidFilePath } - + document.openDocument(filePath: filePath) document.saveDocument() - + print("Processing complete: ") print("- Size: \(document.fileSizeKB)KB") print("- Extension: \(document.fileExtension)") diff --git a/DesignPatterns/Factory/factory.swift b/DesignPatterns/Factory/factory.swift index 1e45632..f84b7fa 100644 --- a/DesignPatterns/Factory/factory.swift +++ b/DesignPatterns/Factory/factory.swift @@ -10,7 +10,7 @@ class CreditCard: PaymentMethod { print("Processing credit card payment of $\(amount)") print("Fee applied: $\(fee)") } - + var fee: Double { return 2.5 } @@ -21,7 +21,7 @@ class PayPal: PaymentMethod { print("Processing PayPal payment of $\(amount)") print("Fee applied: $\(fee)") } - + var fee: Double { return 3.0 } @@ -32,7 +32,7 @@ class BankTransfer: PaymentMethod { print("Processing bank transfer of $\(amount)") print("Fee applied: $\(fee)") } - + var fee: Double { return 1.0 } @@ -44,7 +44,7 @@ class ApplePay: PaymentMethod { print("Processing Apple Pay payment of $\(amount)") print("Fee applied: $\(fee)") } - + var fee: Double { return 1.5 } diff --git a/Sources/.swiftlint.yml b/Sources/.swiftlint.yml index 62f3673..ae8a884 100644 --- a/Sources/.swiftlint.yml +++ b/Sources/.swiftlint.yml @@ -1,13 +1,25 @@ +# .swiftlint.yml included: - Sources excluded: - Tests - .build - +disabled_rules: + - trailing_whitespace + - function_body_length + - type_body_length + - file_length + - identifier_name + - force_cast + - force_try + - cyclomatic_complexity + - nesting + - multiple_closures_with_trailing_closure + - todo + - type_name line_length: warning: 120 error: 200 - opt_in_rules: - empty_count - - missing_docs + - missing_docs \ No newline at end of file From ce16f781955c20dd77f430bcf947627a0776142a Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Wed, 1 Jan 2025 18:10:46 +0200 Subject: [PATCH 4/4] Updated file to respect the lint --- test.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.swift b/test.swift index db560ea..f61249f 100644 --- a/test.swift +++ b/test.swift @@ -1,4 +1,4 @@ -struct userAccount { +struct UserAccount { var userName: String var userAge: Int var isActive: Bool @@ -23,5 +23,5 @@ struct userAccount { } } -let newUser = userAccount(userName: "john", userAge: 25, isActive: true) +let newUser = UserAccount(userName: "john", userAge: 25, isActive: true) newUser.printUserDetails()