From 04fcd4af7c646d35a3d93fd132544e274d89e4b8 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Fri, 1 Mar 2024 13:04:39 +0000
Subject: [PATCH] Issue #3112393 by cmlara, smustgrave, AdamPS, alexpott: Allow
 vendor hardening to remove individual files

(cherry picked from commit 661f5453d049717c14a13ddf12f28f3c4fa3c360)
---
 .../Plugin/VendorHardening/VendorHardeningPlugin.php | 12 ++++++------
 .../VendorHardening/VendorHardeningPluginTest.php    |  5 ++++-
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/composer/Plugin/VendorHardening/VendorHardeningPlugin.php b/composer/Plugin/VendorHardening/VendorHardeningPlugin.php
index f16c9d3c3af3..285e92d2713f 100644
--- a/composer/Plugin/VendorHardening/VendorHardeningPlugin.php
+++ b/composer/Plugin/VendorHardening/VendorHardeningPlugin.php
@@ -343,27 +343,27 @@ protected function cleanPathsForPackage(PackageInterface $package, $paths_for_pa
       return;
     }
 
-    $this->io->writeError(sprintf('%sCleaning directories in <comment>%s</comment>', str_repeat(' ', 4), $package_name), TRUE, IOInterface::VERY_VERBOSE);
+    $this->io->writeError(sprintf('%sCleaning paths in <comment>%s</comment>', str_repeat(' ', 4), $package_name), TRUE, IOInterface::VERY_VERBOSE);
     $fs = new Filesystem();
     foreach ($paths_for_package as $cleanup_item) {
       $cleanup_path = $package_dir . '/' . $cleanup_item;
-      if (!is_dir($cleanup_path)) {
+      if (!file_exists($cleanup_path)) {
         // If the package has changed or the --prefer-dist version does not
         // include the directory. This is not an error.
-        $this->io->writeError(sprintf("%s<comment>Directory '%s' does not exist.</comment>", str_repeat(' ', 6), $cleanup_path), TRUE, IOInterface::VERY_VERBOSE);
+        $this->io->writeError(sprintf("%s<comment>Path '%s' does not exist.</comment>", str_repeat(' ', 6), $cleanup_path), TRUE, IOInterface::VERY_VERBOSE);
         continue;
       }
 
-      if (!$fs->removeDirectory($cleanup_path)) {
+      if (!$fs->remove($cleanup_path)) {
         // Always display a message if this fails as it means something
         // has gone wrong. Therefore the message has to include the
         // package name as the first informational message might not
         // exist.
-        $this->io->writeError(sprintf("%s<error>Failure removing directory '%s'</error> in package <comment>%s</comment>.", str_repeat(' ', 6), $cleanup_item, $package_name), TRUE, IOInterface::NORMAL);
+        $this->io->writeError(sprintf("%s<error>Failure removing path '%s'</error> in package <comment>%s</comment>.", str_repeat(' ', 6), $cleanup_item, $package_name), TRUE, IOInterface::NORMAL);
         continue;
       }
 
-      $this->io->writeError(sprintf("%sRemoving directory <info>'%s'</info>", str_repeat(' ', 4), $cleanup_item), TRUE, IOInterface::VERBOSE);
+      $this->io->writeError(sprintf("%sRemoving path <info>'%s'</info>", str_repeat(' ', 4), $cleanup_item), TRUE, IOInterface::VERBOSE);
     }
   }
 
diff --git a/core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/VendorHardeningPluginTest.php b/core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/VendorHardeningPluginTest.php
index e98ee24373bf..fa8b8feec6e8 100644
--- a/core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/VendorHardeningPluginTest.php
+++ b/core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/VendorHardeningPluginTest.php
@@ -35,6 +35,7 @@ protected function setUp(): void {
           'tests' => [
             'SomeTest.php' => '<?php',
           ],
+          'SomeFile.php' => '<?php',
         ],
       ],
     ]);
@@ -92,14 +93,16 @@ public function testCleanPathsForPackage() {
     $ref_io->setValue($plugin, $io->reveal());
 
     $this->assertFileExists(vfsStream::url('vendor/drupal/package/tests/SomeTest.php'));
+    $this->assertFileExists(vfsStream::url('vendor/drupal/package/SomeFile.php'));
 
     $package = $this->prophesize(PackageInterface::class);
     $package->getName()->willReturn('drupal/package');
 
     $ref_clean = new \ReflectionMethod($plugin, 'cleanPathsForPackage');
-    $ref_clean->invokeArgs($plugin, [$package->reveal(), ['tests']]);
+    $ref_clean->invokeArgs($plugin, [$package->reveal(), ['tests', 'SomeFile.php']]);
 
     $this->assertFileDoesNotExist(vfsStream::url('vendor/drupal/package/tests'));
+    $this->assertFileDoesNotExist(vfsStream::url('vendor/drupal/package/SomeFile.php'));
   }
 
   /**
-- 
GitLab