diff --git a/core/lib/Drupal/Core/Composer/Composer.php b/core/lib/Drupal/Core/Composer/Composer.php
index 51a4917..1e24630 100644
--- a/core/lib/Drupal/Core/Composer/Composer.php
+++ b/core/lib/Drupal/Core/Composer/Composer.php
@@ -106,6 +106,8 @@ public static function preAutoloadDump(Event $event) {
    * Ensures that .htaccess and web.config files are present in Composer root.
    *
    * @param \Composer\Script\Event $event
+   *   A Event object to get the configured composer vendor directories
+   *   from.
    */
   public static function ensureHtaccess(Event $event) {
 
@@ -233,17 +235,18 @@ protected static function deleteRecursive($path) {
       return unlink($path);
     }
     $success = TRUE;
-    $dir = dir($path);
-    while (($entry = $dir->read()) !== FALSE) {
-      if ($entry == '.' || $entry == '..') {
-        continue;
+    // Avoid warnings when opendir does not have the permissions to open a
+    // directory.
+    if ($handle = @opendir($path)) {
+      while (FALSE !== ($entry = readdir($handle))) {
+        if ($entry == '.' || $entry == '..') {
+          continue;
+        }
+        $entry_path = $path . '/' . $entry;
+        $success = static::deleteRecursive($entry_path) && $success;
       }
-      $entry_path = $path . '/' . $entry;
-      $success = static::deleteRecursive($entry_path) && $success;
+      closedir($handle);
+      return rmdir($path) && $success;
     }
-    $dir->close();
-
-    return rmdir($path) && $success;
   }
-
 }
