diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 6f96ca0cf5..aa4e8b03c1 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -44,8 +44,3 @@ parameters: message: "#^Unreachable statement \\- code above always terminates\\.$#" count: 1 path: src/Symfony/BootstrapCompilerPass.php - - - - message: "#^Method Drush\\\\Commands\\\\core\\\\DrupalDependenciesCommands\\:\\:create\\(\\) should return static\\(Drush\\\\Commands\\\\core\\\\DrupalDependenciesCommands\\) but returns Drush\\\\Commands\\\\core\\\\DrupalDependenciesCommands\\.$#" - count: 1 - path: src/Commands/AutowireTrait.php diff --git a/src/Commands/core/DrupalDependenciesCommands.php b/src/Commands/core/DrupalDependenciesCommands.php index e8e061f455..e7da4026c8 100644 --- a/src/Commands/core/DrupalDependenciesCommands.php +++ b/src/Commands/core/DrupalDependenciesCommands.php @@ -12,16 +12,24 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Extension\Dependency; use Drupal\Core\Extension\Extension; +use Drupal\Core\Extension\ModuleExtensionList; use Drush\Attributes as CLI; use Drush\Boot\DrupalBootLevels; +use Drush\Commands\AutowireTrait; use Drush\Commands\DrushCommands; use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\DependencyInjection\Attribute\Autowire; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Drush commands revealing Drupal dependencies. */ -class DrupalDependenciesCommands extends DrushCommands +final class DrupalDependenciesCommands extends DrushCommands { + use AutowireTrait { + create as traitCreate; + } + public const WHY_MODULE = 'why:module'; public const WHY_CONFIG = 'why:config'; @@ -35,6 +43,24 @@ class DrupalDependenciesCommands extends DrushCommands 'config-config' => [], ]; + public function __construct( + #[Autowire(param: 'container.modules')] + private readonly array $installedModules, + private readonly ModuleExtensionList $moduleExtensionList, + ) { + parent::__construct(); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container): self + { + // Wrap the AutowireTrait method, to pass the Symfony container instead + // of League container so that we can benefit from container params. + return static::traitCreate($container); + } + #[CLI\Command(name: self::WHY_MODULE, aliases: ['wm'])] #[CLI\Help(description: 'List all objects (modules, configurations) depending on a given module')] #[CLI\Argument(name: 'module', description: 'The module to check dependents for')] @@ -114,19 +140,18 @@ public function validateDependentsOfModule(CommandData $commandData): void throw new \InvalidArgumentException("Cannot use --dependent-type=config together with --no-only-installed"); } - $installedModules = \Drupal::getContainer()->getParameter('container.modules'); $module = $commandData->input()->getArgument('module'); if ($type === 'module') { $this->dependencies['module-module'] = array_map(function (Extension $extension): array { return array_map(function (string $dependencyString) { return Dependency::createFromString($dependencyString)->getName(); }, $extension->info['dependencies']); - }, \Drupal::service('extension.list.module')->getList()); + }, $this->moduleExtensionList->getList()); if (!$notOnlyInstalled) { $this->dependencies['module-module'] = array_intersect_key( $this->dependencies['module-module'], - $installedModules + $this->installedModules ); } if (!isset($this->dependencies['module-module'][$module])) { @@ -134,7 +159,7 @@ public function validateDependentsOfModule(CommandData $commandData): void '@module' => $module, ])); } - } elseif (!isset($installedModules[$module])) { + } elseif (!isset($this->installedModules[$module])) { throw new \InvalidArgumentException(dt('Invalid @module module', [ '@module' => $module, ])); diff --git a/tests/integration/DrupalDependenciesTest.php b/tests/integration/DrupalDependenciesTest.php index 993245c211..3ff9fb7559 100644 --- a/tests/integration/DrupalDependenciesTest.php +++ b/tests/integration/DrupalDependenciesTest.php @@ -1,9 +1,8 @@