phpstan-drupal icon indicating copy to clipboard operation
phpstan-drupal copied to clipboard

Extensions in sites/<site-dir>/modules not discovered and autoloaded

Open aweingarten opened this issue 5 years ago • 2 comments

How is drupal-check installed?

  • [ ] drupal-check is installed using the phar
  • [ ] drupal-check is installed globally via Composer
  • [ ] drupal-check is installed globally using consolidation/cgr
  • [x] drupal-check is installed as a dependency to my project

Environment:

  • OS: DrupalVM/BLT
  • PHP Version: 7.2
  • Drupal core: 8.8.2

Describe the bug I suspect that drupal-check is not properly autoloading code from a multi-site directory.

We have 2 modules defined in our codebase.
managed_cookies is located in docroot/sites//modules/managed_cookies utility is located in docroot/modules/custom/modules/utility

utility has a dependency on managed_cookies.

Console output

vagrant@local:/var/www/$ ./vendor/bin/drupal-check -vvv docroot/modules/custom/utility/src/Service/WebSessionCookieManager.php
Current working directory: /var/www/mysite
Using Drupal root: /var/www/mysite/docroot
Using vendor root: /var/www/mysite/vendor
Using autoloader: /var/www/mysite/vendor/autoload.php
/var/www/mysite/docroot/modules/custom/utility/src/Service/WebSessionCookieManager.php
 ------ ----------------------------------------------------------------------------------------------
  Line   WebSessionCookieManager.php
 ------ ----------------------------------------------------------------------------------------------
         Class Drupal\managed_cookies\BaseCookieManager not found and could not be autoloaded.
 ------ ----------------------------------------------------------------------------------------------

 [ERROR] Found 1 error

When I relocate the module managed_cookies from docroot/sites/<mysite>/modules/managed_cookies to docroot/modules/custom/modules/managed_cookies

and re-run drupal-check, it passes:

vagrant@local/var/www/mysite$ ./vendor/bin/drupal-check -vvv docroot/modules/custom/utility/src/Service/WebSessionCookieManager.php
Current working directory: /var/www/mysite
Using Drupal root: /var/www/mysite/docroot
Using vendor root: /var/www/mysite/vendor
Using autoloader: /var/www/mysite/vendor/autoload.php
/var/www/mysite/docroot/modules/custom/utility/src/Service/WebSessionCookieManager.php
 [OK] No errors

aweingarten avatar Feb 24 '20 16:02 aweingarten

This should be reported as a bug in phpstan-drupal I guess.

mglaman avatar Feb 27 '20 03:02 mglaman

The problem is within \mglaman\PHPStanDrupal\Drupal\ExtensionDiscovery::scan. It scans the following:

$searchdirs[self::ORIGIN_CORE] = 'core';
// legacy
$searchdirs[self::ORIGIN_SITES_ALL] = 'sites/all';
// top level
$searchdirs[self::ORIGIN_ROOT] = '';

// current site
$searchdirs[self::ORIGIN_SITE] = $this->sitePath;

The value of $this->sitePath is always set to sites/default. I'd have thought the top-level search would reach into sites/* entirely.

mglaman avatar Feb 14 '24 03:02 mglaman