From a4be79551d6044e21a6a1712af8b601775d3bb9e Mon Sep 17 00:00:00 2001 From: Jerome Thayananthajothy Date: Sun, 6 Oct 2024 13:30:06 +0100 Subject: [PATCH] Refactor phpvmrc.js to improve findPHPVMRCFile and autoSwitchPHPVersion functions --- lib/utils/phpvmrc.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/utils/phpvmrc.js b/lib/utils/phpvmrc.js index eb4fc2d..16ddb6c 100644 --- a/lib/utils/phpvmrc.js +++ b/lib/utils/phpvmrc.js @@ -11,12 +11,16 @@ const { usePHPVersion } = require('../commands/use'); function findPHPVMRCFile(startDir = process.cwd()) { let currentDir = startDir; - while (currentDir !== '/') { + while (true) { const phpvmrcPath = path.join(currentDir, '.phpvmrc'); if (fs.existsSync(phpvmrcPath)) { return phpvmrcPath; } - currentDir = path.resolve(currentDir, '..'); + + const parentDir = path.resolve(currentDir, '..'); + if (parentDir === currentDir) break; // Stop if we've reached the root directory + + currentDir = parentDir; } return null; @@ -41,10 +45,10 @@ async function autoSwitchPHPVersion() { } catch (error) { console.error(`Failed to switch to PHP ${version}: ${error.message}`); } - } else { - // Silent if no .phpvmrc file is found - return; } + + // Silent return when no .phpvmrc is found + return; } module.exports = { findPHPVMRCFile, autoSwitchPHPVersion };