diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 50732785..2fd4be33 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -52,7 +52,8 @@ describe('Utils tests', () => { expect(await utils.parseVersion('4.x')).toBe(undefined); }); - it('checking resolveVersionInput', async () => { + it('checking resolveVersionInput', () => { + // composer.json expect(utils.parsePhpVersionFile('{"require": {"php": "^8.1"}}')).toBe( '8.1' ); @@ -68,6 +69,10 @@ describe('Utils tests', () => { expect(utils.parsePhpVersionFile('{"require": {"php": "^8.1|^8.0"}}')).toBe( '8.1' ); + // asdf: .tool-versions + expect(utils.parsePhpVersionFile('php 8.1')).toBe('8.1'); + // phpenv: .php-version + expect(utils.parsePhpVersionFile('8.1.1')).toBe('8.1'); }); it('checking parseIniFile', async () => { diff --git a/dist/index.js b/dist/index.js index d0aa2242..db9eaa04 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1070,10 +1070,8 @@ async function resolveVersionInput() { } exports.resolveVersionInput = resolveVersionInput; function parsePhpVersionFile(contents) { - const json = JSON.parse(contents); - const version = json.config?.platform?.php ?? json.require?.php; - const match = version.match(/(\d+(\.\d+))/); - return match ? match[1] : ''; + const match = contents.match(/("php:")?.*?(\d+(\.\d+))/); + return match ? match[2] : ''; } exports.parsePhpVersionFile = parsePhpVersionFile; async function getManifestURL() { diff --git a/src/utils.ts b/src/utils.ts index 206e5674..bf885ff6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -76,10 +76,8 @@ export async function resolveVersionInput(): Promise { } export function parsePhpVersionFile(contents: string): string { - const json = JSON.parse(contents); - const version = json.config?.platform?.php ?? json.require?.php; - const match = version.match(/(\d+(\.\d+))/); - return match ? match[1] : ''; + const match = contents.match(/("php:")?.*?(\d+(\.\d+))/); + return match ? match[2] : ''; } /** Function to get manifest URL