From 69c0f5ace988d17cfa2fb3b1286e494795ddcf79 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Thu, 27 Jan 2022 19:39:02 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Have=20all=20tests=20pass,=20refact?= =?UTF-8?q?or=20comparison=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP Monitor.xcodeproj/project.pbxproj | 14 ++-- .../PHP/PHP Version/PhpVersionNumber.swift | 65 ++++++++++++------- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index 4f6f1e7..29d0373 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -24,6 +24,11 @@ 54FCFD31276C8DA4004CE748 /* HotkeyPreferenceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54FCFD2F276C8DA4004CE748 /* HotkeyPreferenceView.swift */; }; C405A4D024B9B9140062FAFA /* InternetAccessPolicy.strings in Resources */ = {isa = PBXBuildFile; fileRef = C405A4CE24B9B9130062FAFA /* InternetAccessPolicy.strings */; }; C405A4D124B9B9140062FAFA /* InternetAccessPolicy.plist in Resources */ = {isa = PBXBuildFile; fileRef = C405A4CF24B9B9140062FAFA /* InternetAccessPolicy.plist */; }; + C40B24F127A3106D0018C7D2 /* ServicesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EC1E67279DE0540010F296 /* ServicesView.swift */; }; + C40B24F227A310770018C7D2 /* Events.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EC1E72279DFCF40010F296 /* Events.swift */; }; + C40B24F327A310780018C7D2 /* Events.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EC1E72279DFCF40010F296 /* Events.swift */; }; + C40B24F427A310830018C7D2 /* StatusMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = C47331A1247093B7009A0597 /* StatusMenu.swift */; }; + C40B24F527A3108B0018C7D2 /* Async.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EC1E6C279DF87A0010F296 /* Async.swift */; }; C40C7F1E2772136000DDDCDC /* PhpEnv.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40C7F1D2772136000DDDCDC /* PhpEnv.swift */; }; C40C7F1F2772136000DDDCDC /* PhpEnv.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40C7F1D2772136000DDDCDC /* PhpEnv.swift */; }; C40C7F202772136000DDDCDC /* PhpEnv.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40C7F1D2772136000DDDCDC /* PhpEnv.swift */; }; @@ -149,7 +154,6 @@ C4EC1E6D279DF87A0010F296 /* Async.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EC1E6C279DF87A0010F296 /* Async.swift */; }; C4EC1E6E279DF87A0010F296 /* Async.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EC1E6C279DF87A0010F296 /* Async.swift */; }; C4EC1E73279DFCF40010F296 /* Events.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EC1E72279DFCF40010F296 /* Events.swift */; }; - C4EC1E74279DFCF40010F296 /* Events.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EC1E72279DFCF40010F296 /* Events.swift */; }; C4EE188422D3386B00E126E5 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EE188322D3386B00E126E5 /* Constants.swift */; }; C4EE55A927708B9E001DF387 /* PMHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EE55A627708B9E001DF387 /* PMHeaderView.swift */; }; C4EE55AA27708B9E001DF387 /* PMHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4EE55A627708B9E001DF387 /* PMHeaderView.swift */; }; @@ -181,7 +185,6 @@ C4F780C425D80B75000DBC97 /* MainMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4811D2922D70F9A00B5F6B3 /* MainMenu.swift */; }; C4F780C525D80B75000DBC97 /* MenuBarImageGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41C1B4822B00A9800E7CF16 /* MenuBarImageGenerator.swift */; }; C4F780C625D80B75000DBC97 /* XibLoadable.swift in Sources */ = {isa = PBXBuildFile; fileRef = C48D0C9225CC804200CC7490 /* XibLoadable.swift */; }; - C4F780C725D80B75000DBC97 /* StatusMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = C47331A1247093B7009A0597 /* StatusMenu.swift */; }; C4F780C825D80B75000DBC97 /* DateExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4F8C0A322D4F12C002EFE61 /* DateExtension.swift */; }; C4F780C925D80B75000DBC97 /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C46FA23E246C358E00944F05 /* StringExtension.swift */; }; C4F780CA25D80B75000DBC97 /* HomebrewPackage.swift in Sources */ = {isa = PBXBuildFile; fileRef = C412E5FB25700D5300A1FB67 /* HomebrewPackage.swift */; }; @@ -850,6 +853,7 @@ C40C7F2627721FA200DDDCDC /* Constants.swift in Sources */, C4B585402770FE3900DA4FBE /* Paths.swift in Sources */, C415D3E62770F540005EF286 /* main.swift in Sources */, + C40B24F327A310780018C7D2 /* Events.swift in Sources */, C40C7F2227721F8200DDDCDC /* PhpInstallation.swift in Sources */, C4B585432770FE3900DA4FBE /* Shell.swift in Sources */, C4D9ADC1277610E1007277F4 /* PhpSwitcher.swift in Sources */, @@ -858,7 +862,6 @@ C40C7F2327721F8200DDDCDC /* ActivePhpInstallation.swift in Sources */, C4B585462770FE3900DA4FBE /* Command.swift in Sources */, C4D9ADCA277611A0007277F4 /* InternalSwitcher.swift in Sources */, - C4EC1E74279DFCF40010F296 /* Events.swift in Sources */, C48D6C72279CD2AC00F26D7E /* PhpVersionNumber.swift in Sources */, C40C7F2527721F9800DDDCDC /* HomebrewPackage.swift in Sources */, C417DC76277614690015E6EE /* Helpers.swift in Sources */, @@ -972,10 +975,10 @@ C4AF9F7B2754499000D44ED0 /* Valet.swift in Sources */, C4F780C025D80B6E000DBC97 /* Startup.swift in Sources */, C4CCBA6D275C567B008C7055 /* PMWindowController.swift in Sources */, + C40B24F527A3108B0018C7D2 /* Async.swift in Sources */, C4B5635F276AB09000F12CCB /* VersionExtractor.swift in Sources */, C4F2E4382752F08D0020E974 /* HomebrewDiagnostics.swift in Sources */, C4F780AE25D80B37000DBC97 /* ExtensionParserTest.swift in Sources */, - C4F780C725D80B75000DBC97 /* StatusMenu.swift in Sources */, C4C8E819276F54D8003AC782 /* App+ConfigWatch.swift in Sources */, C48D6C75279CD3E400F26D7E /* PhpVersionNumberTest.swift in Sources */, C43603A1275E67610028EFC6 /* AppDelegate+Notifications.swift in Sources */, @@ -989,6 +992,7 @@ C464ADB3275A87CA003FCD53 /* SiteListCell.swift in Sources */, C415D3E92770F692005EF286 /* AppDelegate+InterApp.swift in Sources */, C4AF9F78275447F100D44ED0 /* ValetConfigParserTest.swift in Sources */, + C40B24F427A310830018C7D2 /* StatusMenu.swift in Sources */, C417DC75277614690015E6EE /* Helpers.swift in Sources */, C4B97B7C275CF20A003F3378 /* App+GlobalHotkey.swift in Sources */, C4B97B79275CF1B5003F3378 /* App+ActivationPolicy.swift in Sources */, @@ -1003,10 +1007,12 @@ C415938027A1B54F00D2E1B7 /* PhpFrameworks.swift in Sources */, C4D9ADC9277611A0007277F4 /* InternalSwitcher.swift in Sources */, C4F30B0B278E203C00755FCE /* MainMenu+Startup.swift in Sources */, + C40B24F227A310770018C7D2 /* Events.swift in Sources */, C4F30B0A278E1A1A00755FCE /* ComposerJson.swift in Sources */, C4AF9F7D275454A900D44ED0 /* ValetTest.swift in Sources */, C4B56362276AB0A500F12CCB /* VersionExtractorTest.swift in Sources */, C4B585452770FE3900DA4FBE /* Command.swift in Sources */, + C40B24F127A3106D0018C7D2 /* ServicesView.swift in Sources */, C4F780C525D80B75000DBC97 /* MenuBarImageGenerator.swift in Sources */, C4F780B725D80B5D000DBC97 /* App.swift in Sources */, C48D6C71279CD2AC00F26D7E /* PhpVersionNumber.swift in Sources */, diff --git a/phpmon-common/PHP/PHP Version/PhpVersionNumber.swift b/phpmon-common/PHP/PHP Version/PhpVersionNumber.swift index 356f0bb..07eee33 100644 --- a/phpmon-common/PHP/PHP Version/PhpVersionNumber.swift +++ b/phpmon-common/PHP/PHP Version/PhpVersionNumber.swift @@ -58,43 +58,26 @@ public struct PhpVersionNumberCollection: Equatable { public func matching(constraint: String, strict: Bool = false) -> [PhpVersionNumber] { if let version = PhpVersionNumber.make(from: constraint, type: .versionOnly) { // Strict constraint (e.g. "7.0") -> returns specific version - return self.versions.filter { - $0.major == version.major - && $0.minor == version.minor - && (strict ? $0.patch(strict, version) == version.patch(strict) : true) - } + return self.versions.filter { $0.isSameAs(version, strict) } } if let version = PhpVersionNumber.make(from: constraint, type: .caretVersionRange) { // Caret range means that the major version is never higher but minor version can be higher // ^7.2 will be compatible with all versions between 7.2 and 8.0 - return self.versions.filter { - $0.major == version.major && - ( - // Either the minor version is the same and the patch is higher or equal - $0.minor == version.minor && $0.patch(strict) >= version.patch(strict, $0) - // or the minor version number has been bumped - || $0.minor > version.minor - ) - } + return self.versions.filter { $0.hasNewerMinorVersionOrPatch(version, strict) } } if let version = PhpVersionNumber.make(from: constraint, type: .tildeVersionRange) { // Tilde range means that most specific digit is used as the basis. - if version.patch != nil { + return self.versions.filter { + version.patch != nil // If a patch is provided then the minor version cannot be bumped. - return self.versions.filter { - $0.major == version.major && $0.minor == version.minor - && $0.patch(strict, version) >= version.patch! - } - } else { + ? $0.hasSameMajorAndMinorButNewerOrSamePatch(version, strict) // If a patch is not provided then the major version cannot be bumped. - return self.versions.filter { - $0.major == version.major && $0.minor >= version.minor - } + : $0.hasSameMajorButNewerOrSameMinor(version, strict) } } - + return [] } } @@ -116,6 +99,14 @@ public struct PhpVersionNumber: Equatable { case versionOnly = #"^(?\d+).(?\d+).?(?\d+)?\z"# case caretVersionRange = #"^\^(?\d+).(?\d+).?(?\d+)?\z"# case tildeVersionRange = #"^~(?\d+).(?\d+).?(?\d+)?\z"# + + // TODO: Handle these cases + /* + case greaterThanOrEqual = #"^>=(?\d+).(?\d+).?(?\d+)?\z"# + case greaterThan = #"^>(?\d+).(?\d+).?(?\d+)?\z"# + case smallerThanOrEqual = #"^<=(?\d+).(?\d+).?(?\d+)?\z"# + case smallerThan = #"^<(?\d+).(?\d+).?(?\d+)?\z"# + */ } public static func make(from versionString: String, type: MatchType = .versionOnly) -> Self? { @@ -138,4 +129,30 @@ public struct PhpVersionNumber: Equatable { return nil } + + // MARK: Comparison Logic + + internal func isSameAs(_ version: PhpVersionNumber, _ strict: Bool) -> Bool { + return self.major == version.major + && self.minor == version.minor + && (strict ? self.patch(strict, version) == version.patch(strict) : true) + } + + internal func hasNewerMinorVersionOrPatch(_ version: PhpVersionNumber, _ strict: Bool) -> Bool { + return self.major == version.major && + ( + (self.minor == version.minor && self.patch(strict) >= version.patch(strict, self)) + || self.minor > version.minor + ) + } + + internal func hasSameMajorAndMinorButNewerOrSamePatch(_ version: PhpVersionNumber, _ strict: Bool) -> Bool { + return self.major == version.major && self.minor == version.minor + && self.patch(strict, version) >= version.patch! + } + + internal func hasSameMajorButNewerOrSameMinor(_ version: PhpVersionNumber, _ strict: Bool) -> Bool { + return self.major == version.major + && self.minor >= version.minor + } }