From 73d0ef92d88f4a83eae1c7d73ddb8b7ef88cd412 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Costa=20Silva?=
 <1574795+joaocsilva@users.noreply.github.com>
Date: Thu, 15 Feb 2024 10:18:32 +0100
Subject: [PATCH] DQA-8756: Fix .env and parse_ini_file issue (#744)

---
 composer.json                                      |  3 ++-
 src/TaskRunner/Commands/ComponentCheckCommands.php | 12 ++++++++----
 tests/fixtures/commands/component-check.yml        |  3 +++
 3 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/composer.json b/composer.json
index 4359d83f7..25db5adfc 100644
--- a/composer.json
+++ b/composer.json
@@ -33,7 +33,8 @@
         "phpstan/phpstan": "^1.10",
         "phpstan/phpstan-deprecation-rules": "^1.0",
         "phpunit/phpunit": "^9.5 || ^10.0",
-        "squizlabs/php_codesniffer": "^3.7"
+        "squizlabs/php_codesniffer": "^3.7",
+        "vlucas/phpdotenv": "^5.6"
     },
     "suggest": {
         "drupal/console": "The Drupal CLI, tool to generate boilerplate code, interact with and debug Drupal."
diff --git a/src/TaskRunner/Commands/ComponentCheckCommands.php b/src/TaskRunner/Commands/ComponentCheckCommands.php
index 3f310a594..f41eb7700 100644
--- a/src/TaskRunner/Commands/ComponentCheckCommands.php
+++ b/src/TaskRunner/Commands/ComponentCheckCommands.php
@@ -5,6 +5,7 @@
 namespace EcEuropa\Toolkit\TaskRunner\Commands;
 
 use Composer\Semver\Semver;
+use Dotenv\Dotenv;
 use EcEuropa\Toolkit\DrupalReleaseHistory;
 use EcEuropa\Toolkit\TaskRunner\AbstractCommands;
 use EcEuropa\Toolkit\Website;
@@ -303,17 +304,20 @@ protected function validateEnvironmentVariables()
                     $parsed_yaml = Yaml::parseFile($filename);
                     // Loop through all the services looking for environment variables.
                     if (!empty($parsed_yaml['services'])) {
-                        foreach ($parsed_yaml['services'] as $service_name => $service_settings) {
-                            if (!empty($service_settings['environment'])) {
+                        foreach ($parsed_yaml['services'] as $serviceName => $serviceSettings) {
+                            if (!empty($serviceSettings['environment'])) {
                                 // Add environment variables set for check.
-                                $envVarsSet[$filename . '_' . $service_name] = $service_settings['environment'];
+                                $envVarsSet[$filename . '_' . $serviceName] = $serviceSettings['environment'];
                             }
                         }
                     }
                 // Ini files.
                 } else {
                     // Add environment variables set for check.
-                    $envVarsSet[$filename] = parse_ini_file($filename);
+                    $contentParsed = Dotenv::parse(file_get_contents($filename));
+                    if (is_array($contentParsed)) {
+                        $envVarsSet[$filename] = $contentParsed;
+                    }
                 }
             }
         }
diff --git a/tests/fixtures/commands/component-check.yml b/tests/fixtures/commands/component-check.yml
index 7f61463c5..fb89e35a3 100644
--- a/tests/fixtures/commands/component-check.yml
+++ b/tests/fixtures/commands/component-check.yml
@@ -527,8 +527,11 @@
     - touch: composer.json
     - file: .env
       content: |
+        # Use special characters like `~/.bashrc` or (parentheses).
+        # This is the user for the Nextcloud.
         NEXTCLOUD_USER=
         NEXTCLOUD_PASS=
+        # This is the deprecated ASDA user.
         ASDA_USER=
         ASDA_PASSWORD=
         QA_API_BASIC_AUTH=