Problem/Motivation

The following code in drupal_phpunit_contrib_extension_directory_roots() results in deprecation notices in PHP 8.1

  // Note this also checks sites/../modules and sites/../profiles.
  foreach (scandir($sites_path) as $site) {
    if ($site[0] === '.' || $site === 'simpletest') {
      continue;
    }
    $path = "$sites_path/$site";
    $paths[] = is_dir("$path/modules") ? realpath("$path/modules") : NULL;
    $paths[] = is_dir("$path/profiles") ? realpath("$path/profiles") : NULL;
    $paths[] = is_dir("$path/themes") ? realpath("$path/themes") : NULL;
  }
  return array_filter($paths, 'file_exists');(

Steps to reproduce

Run tests on PHP 8.1 - see #3220021: [meta] Ensure compatibility of Drupal 9 with PHP 8.1 (as it evolves)

Proposed resolution

Refactor code so this does not occur.

Remaining tasks

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

N/a

CommentFileSizeAuthor
#2 3233010-2.patch503 bytesalexpott

Comments

alexpott created an issue. See original summary.

alexpott’s picture

Status: Active » Needs review
StatusFileSize
new503 bytes

Originally I refactored this by doing:

diff --git a/core/tests/bootstrap.php b/core/tests/bootstrap.php
index a14339d5628..1fd066afc35 100644
--- a/core/tests/bootstrap.php
+++ b/core/tests/bootstrap.php
@@ -63,9 +63,11 @@ function drupal_phpunit_contrib_extension_directory_roots($root = NULL) {
       continue;
     }
     $path = "$sites_path/$site";
-    $paths[] = is_dir("$path/modules") ? realpath("$path/modules") : NULL;
-    $paths[] = is_dir("$path/profiles") ? realpath("$path/profiles") : NULL;
-    $paths[] = is_dir("$path/themes") ? realpath("$path/themes") : NULL;
+    foreach (['modules', 'profiles', 'themes'] as $type) {
+      if (is_dir("$path/$type")) {
+        $paths[] = realpath("$path/$type");
+      }
+    }
   }
   return array_filter($paths, 'file_exists');
 }

but on reading the code more carefully... I think we can do something simpler - there's no need to call file_exists - we can just filter out the NULLS.

is_dir() guarentees that the paths found in the sites directory exist and are directories... all the other values added to $paths are guaranteed to exist and if they don't Drupal moans a lot...

  $paths = [
    $root . '/core/modules',
    $root . '/core/profiles',
    $root . '/core/themes',
    $root . '/modules',
    $root . '/profiles',
    $root . '/themes',
  ];
longwave’s picture

Status: Needs review » Reviewed & tested by the community

Yep, is_dir() already checks if the directory exists, no need to do it twice.

  • catch committed 504b0db on 9.3.x
    Issue #3233010 by alexpott, longwave:...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed 504b0db and pushed to 9.3.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.