From 8097502a76182b27f1f83658e2fd5a5cc03321bb Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Tue, 23 Dec 2025 11:38:37 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Ensure=20invalid=20or=20unexpect?= =?UTF-8?q?ed=20`composer.json`=20format=20does=20not=20crash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Integrations/Valet/Sites/ValetSite.swift | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift b/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift index 5ff8e6f2..f5f3cf24 100644 --- a/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift +++ b/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift @@ -203,28 +203,31 @@ class ValetSite: ValetListable { /** Checks the contents of the composer.json file and determine the notable dependencies, - as well as the requested PHP version. If no composer.json file is found, nothing happens. + as well as the requested PHP version. This info is then used to determine project type. + + If no composer.json file is found or is invalid, some features may be unavailable, like + for example project type inference based on dependencies. */ private func determineComposerInformation() { let path = "\(absolutePath)/composer.json" - do { - if container.filesystem.fileExists(path) { - let decoded = try JSONDecoder().decode( - ComposerJson.self, - from: String( - contentsOf: URL(fileURLWithPath: path), - encoding: .utf8 - ).data(using: .utf8)! - ) - - (self.preferredPhpVersion, - self.preferredPhpVersionSource) = decoded.getPhpVersion() - self.notableComposerDependencies = decoded.getNotableDependencies() - } - } catch { - Log.err("Something went wrong reading the Composer JSON file.") + guard container.filesystem.fileExists(path) else { + return } + + guard let fileContents = try? String(contentsOf: URL(fileURLWithPath: path), encoding: .utf8), + let jsonData = fileContents.data(using: .utf8) else { + Log.err("Could not read the Composer JSON file at: \(path)") + return + } + + guard let decoded = try? JSONDecoder().decode(ComposerJson.self, from: jsonData) else { + Log.err("Could not parse the Composer JSON file at: \(path)") + return + } + + (self.preferredPhpVersion, self.preferredPhpVersionSource) = decoded.getPhpVersion() + self.notableComposerDependencies = decoded.getNotableDependencies() } /**