From 73e7492d6d0988ab26e4712eeb01a2d332bcd90b Mon Sep 17 00:00:00 2001
From: Gary Lockett <gary@creativecow.uk>
Date: Thu, 20 Jan 2022 12:38:46 +0000
Subject: [PATCH] fix(manager/composer): respect the PHP override in
 composer.json (#13657)

* fix(composer): respect the PHP override in composer.json

Signed-off-by: Gary Lockett <gary@creativecow.uk>

* chore(docs): document new configuration

Signed-off-by: Gary Lockett <gary@creativecow.uk>

* Apply suggestions from code review

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>

* fix(tests): add missing composer version to expected output

Signed-off-by: Gary Lockett <gary@creativecow.uk>

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
Co-authored-by: Rhys Arkins <rhys@arkins.net>
---
 lib/manager/composer/types.ts      | 11 +++++++++++
 lib/manager/composer/utils.spec.ts | 12 ++++++++++++
 lib/manager/composer/utils.ts      |  6 ++++--
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/lib/manager/composer/types.ts b/lib/manager/composer/types.ts
index b5df7f99c1..65e9c3fe7a 100644
--- a/lib/manager/composer/types.ts
+++ b/lib/manager/composer/types.ts
@@ -8,6 +8,17 @@ export interface Repo {
 }
 export interface ComposerConfig {
   type?: string;
+  /**
+   * Setting a fixed PHP version (e.g. {"php": "7.0.3"}) will let you fake the
+   * platform version so that you can emulate a production env or define your
+   * target platform in the config.
+   * See https://getcomposer.org/doc/06-config.md#platform
+   */
+  config?: {
+    platform?: {
+      php?: string;
+    };
+  };
   /**
    * A repositories field can be an array of Repo objects or an object of repoName: Repo
    * Also it can be a boolean (usually false) to disable packagist.
diff --git a/lib/manager/composer/utils.spec.ts b/lib/manager/composer/utils.spec.ts
index 563ff991fc..47de19bd1a 100644
--- a/lib/manager/composer/utils.spec.ts
+++ b/lib/manager/composer/utils.spec.ts
@@ -18,6 +18,18 @@ describe('manager/composer/utils', () => {
       ).toEqual({ php: '>=5.3.2', composer: '1.1.0' });
     });
 
+    it('returns platform php version', () => {
+      expect(
+        extractContraints(
+          {
+            config: { platform: { php: '7.4.27' } },
+            require: { php: '~7.4 || ~8.0' },
+          },
+          {}
+        )
+      ).toEqual({ composer: '1.*', php: '7.4.27' });
+    });
+
     it('returns from require-dev', () => {
       expect(
         extractContraints(
diff --git a/lib/manager/composer/utils.ts b/lib/manager/composer/utils.ts
index d17fe86a9d..b2b0d31fe5 100644
--- a/lib/manager/composer/utils.ts
+++ b/lib/manager/composer/utils.ts
@@ -69,8 +69,10 @@ export function extractContraints(
   const res: Record<string, string> = { composer: '1.*' };
 
   // extract php
-  if (composerJson.require?.php) {
-    res.php = composerJson.require?.php;
+  if (composerJson.config?.platform?.php) {
+    res.php = composerJson.config.platform.php;
+  } else if (composerJson.require?.php) {
+    res.php = composerJson.require.php;
   }
 
   // extract direct composer dependency
-- 
GitLab