Skip to content

Commit

Permalink
PHPStan Level 2 fixes, more (#5974)
Browse files Browse the repository at this point in the history
* PHPStan Level 2 fixes, more

* Chasing D11 theme extension list
  • Loading branch information
weitzman committed Apr 30, 2024
1 parent d5f5f32 commit 9cb5a45
Show file tree
Hide file tree
Showing 21 changed files with 72 additions and 94 deletions.
6 changes: 5 additions & 1 deletion phpstan.neon.dist
Expand Up @@ -6,7 +6,11 @@ parameters:
paths:
- src
drupal:
drupal_root: sut
drupal_root: %currentWorkingDirectory%/sut
universalObjectCratesClasses:
# Useful until we have https://www.drupal.org/project/drupal/issues/2024043
- Drupal\Core\Extension\Extension
- Drupal\views\ViewExecutable
excludePaths:
# Deliberately calls optional external code.
- src/Psysh/Caster.php
Expand Down
3 changes: 2 additions & 1 deletion src/Boot/BaseBoot.php
Expand Up @@ -49,8 +49,9 @@ public function validRoot(?string $path): bool
return false;
}

public function getVersion($root)
public function getVersion(string $root)
{
return null;
}

public function commandDefaults()
Expand Down
8 changes: 4 additions & 4 deletions src/Boot/BootstrapManager.php
Expand Up @@ -58,7 +58,7 @@ protected function setPhase(int $phase): void
/**
* Add a bootstrap object to the list of candidates.
*
* @param \Drush\Boot\Boot|Array
* @param \Drush\Boot\Boot|array $candidateList
* List of boot candidates
*/
public function add($candidateList): void
Expand Down Expand Up @@ -126,7 +126,7 @@ public function setUri($uri): void
/**
* Crete the bootstrap object if necessary, then return it.
*/
public function bootstrap(): Boot
public function bootstrap(): DrupalBoot8
{
if (!$this->bootstrap) {
$this->bootstrap = $this->bootstrapObjectForRoot($this->getRoot());
Expand Down Expand Up @@ -382,14 +382,14 @@ public function bootstrapToPhaseIndex(int $max_phase_index, ?AnnotationData $ann
/**
* Bootstrap to the highest level possible, without triggering any errors.
*
* @param int $max_phase_index
* @param $max_phase_index
* (optional) Only attempt bootstrap to the specified level.
* @param AnnotationData $annotationData
* Optional annotation data from the command.
*
* The maximum phase to which we bootstrapped.
*/
public function bootstrapMax($max_phase_index = false, ?AnnotationData $annotationData = null): int
public function bootstrapMax(bool|int|null $max_phase_index = false, ?AnnotationData $annotationData = null): int
{
// Bootstrap as far as we can without throwing an error, but log for
// debugging purposes.
Expand Down
4 changes: 0 additions & 4 deletions src/Boot/DrupalBoot.php
Expand Up @@ -45,10 +45,6 @@ protected function scanUpForUri($root, $scan)
return false;
}

public function getVersion($drupal_root)
{
}

public function confPath(bool $require_settings = true, bool $reset = false): ?string
{
return null;
Expand Down
5 changes: 3 additions & 2 deletions src/Boot/DrupalBoot8.php
Expand Up @@ -16,6 +16,7 @@
use Drush\Runtime\LegacyServiceFinder;
use Drush\Runtime\LegacyServiceInstantiator;
use Drush\Runtime\ServiceManager;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Robo\Robo;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -58,7 +59,7 @@ public function getKernel(): DrupalKernelInterface
*/
public function setLogger(LoggerInterface $logger): void
{
if ($this->drupalLoggerAdapter) {
if ($this->drupalLoggerAdapter && $this->drupalLoggerAdapter instanceof LoggerAwareInterface) {
$this->drupalLoggerAdapter->setLogger($logger);
}
parent::setLogger($logger);
Expand All @@ -79,7 +80,7 @@ public function validRoot(?string $path): bool
return false;
}

public function getVersion($drupal_root): string
public function getVersion($root): string
{
return \Drupal::VERSION;
}
Expand Down
13 changes: 11 additions & 2 deletions src/Commands/config/ConfigImportCommands.php
Expand Up @@ -20,6 +20,7 @@
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ModuleInstallerInterface;
use Drupal\Core\Extension\ThemeExtensionList;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\Site\Settings;
Expand Down Expand Up @@ -119,6 +120,11 @@ public function getModuleExtensionList(): ModuleExtensionList
return $this->moduleExtensionList;
}

public function getThemeExtensionList(): ThemeExtensionList
{
return $this->themeExtensionList;
}

public function __construct(
protected ConfigManagerInterface $configManager,
protected StorageInterface $configStorage,
Expand All @@ -130,7 +136,8 @@ public function __construct(
protected ModuleInstallerInterface $moduleInstaller,
protected ThemeHandlerInterface $themeHandler,
protected TranslationInterface $stringTranslation,
protected ModuleExtensionList $moduleExtensionList
protected ModuleExtensionList $moduleExtensionList,
protected ThemeExtensionList $themeExtensionList
) {
parent::__construct();
}
Expand All @@ -149,6 +156,7 @@ public static function create(ContainerInterface $container): self
$container->get('theme_handler'),
$container->get('string_translation'),
$container->get('extension.list.module'),
$container->get('extension.list.theme')
);

if ($container->has('config.import_transformer')) {
Expand Down Expand Up @@ -240,7 +248,8 @@ public function doImport($storage_comparer): void
$this->getModuleInstaller(),
$this->getThemeHandler(),
$this->getStringTranslation(),
$this->getModuleExtensionList()
$this->getModuleExtensionList(),
$this->getThemeExtensionList()
);
if ($config_importer->alreadyImporting()) {
$this->logger()->warning('Another request may be synchronizing configuration already.');
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/core/DeployCommands.php
Expand Up @@ -6,13 +6,13 @@

use Consolidation\SiteAlias\SiteAlias;
use Consolidation\SiteAlias\SiteAliasManagerInterface;
use Consolidation\SiteProcess\ProcessManager;
use Drush\Attributes as CLI;
use Drush\Boot\DrupalBootLevels;
use Drush\Commands\AutowireTrait;
use Drush\Commands\config\ConfigImportCommands;
use Drush\Commands\DrushCommands;
use Drush\Drush;
use Drush\SiteAlias\ProcessManager;

#[CLI\Bootstrap(DrupalBootLevels::NONE)]
final class DeployCommands extends DrushCommands
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/core/EntityCreateCommands.php
Expand Up @@ -9,7 +9,6 @@
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityConstraintViolationListInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
Expand Down Expand Up @@ -56,6 +55,7 @@ public function __construct(
public function createEntity(string $entity_type, $bundle, array $options = ['validate' => true, 'uid' => self::REQ, 'skip-fields' => self::REQ]): string
{
$bundleKey = $this->entityTypeManager->getDefinition($entity_type)->getKey('bundle');
/** @var ContentEntityInterface $entity */
$entity = $this->entityTypeManager->getStorage($entity_type)->create([$bundleKey => $bundle]);
$instances = $this->entityFieldManager->getFieldDefinitions($entity_type, $bundle);
$skip_fields = StringUtils::csvToArray($options['skip-fields']);
Expand Down Expand Up @@ -211,7 +211,7 @@ private function filterViolations(EntityConstraintViolationListInterface &$viola
}
}

protected function setValue(EntityInterface $entity, int|string $name, mixed $value): void
protected function setValue(ContentEntityInterface $entity, int|string $name, mixed $value): void
{
switch ($entity->get($name)->getFieldDefinition()->getType()) {
case 'timestamp':
Expand Down
5 changes: 2 additions & 3 deletions src/Commands/core/MigrateRunnerCommands.php
Expand Up @@ -4,7 +4,6 @@

namespace Drush\Commands\core;

use Drush\Drupal\Migrate\ValidateMigrationId;
use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\AnnotatedCommand\CommandError;
use Consolidation\AnnotatedCommand\Hooks\HookManager;
Expand All @@ -22,10 +21,10 @@
use Drush\Attributes as CLI;
use Drush\Commands\AutowireTrait;
use Drush\Commands\DrushCommands;
use Drush\Drupal\Migrate;
use Drush\Drupal\Migrate\MigrateExecutable;
use Drush\Drupal\Migrate\MigrateMessage;
use Drush\Drupal\Migrate\MigrateUtils;
use Drush\Drupal\Migrate\ValidateMigrationId;
use Drush\Drush;
use Drush\Utils\StringUtils;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
Expand Down Expand Up @@ -202,7 +201,7 @@ protected function getMigrationSourceRowsCount(MigrationInterface $migration): ?
* @param MigrationInterface $migration
* The migration plugin instance.
*
* @return int|null
* @return int
* The number of items that needs update.
*/
protected function getMigrationNeedingUpdateCount(MigrationInterface $migration): int
Expand Down
1 change: 1 addition & 0 deletions src/Commands/core/MkCommands.php
Expand Up @@ -113,6 +113,7 @@ protected static function appendTopics(AnnotatedCommand $command, string $dir_co
$body = "#### Topics\n\n";
foreach ($topics as $name) {
$value = "- `drush $name`\n";
/** @var AnnotatedCommand $topic_command */
$topic_command = Drush::getApplication()->find($name);
$topic_description = $topic_command->getDescription();
if ($docs_relative = $topic_command->getAnnotationData()->get('topic')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/core/WatchdogCommands.php
Expand Up @@ -348,7 +348,7 @@ protected function where(?string $type = null, $severity = null, ?string $filter
* A database result object.
* @param $extended
* Return extended message details.
* @return
* @return \stdClass
* The result object with some attributes themed.
*/
protected function formatResult(\stdClass $result, bool $extended = false): \stdClass
Expand Down
6 changes: 4 additions & 2 deletions src/Commands/help/ListCommands.php
Expand Up @@ -67,10 +67,12 @@ public function helpList($options = ['format' => 'listcli', 'raw' => false, 'fil
return null;
} elseif ($options['format'] == 'xml') {
$descriptor = new XmlDescriptor();
return $descriptor->describe($this->output, $application, []);
$descriptor->describe($this->output, $application, []);
return null;
} elseif ($options['format'] == 'json') {
$descriptor = new JsonDescriptor();
return $descriptor->describe($this->output, $application, []);
$descriptor->describe($this->output, $application, []);
return null;
} else {
// No longer used. Works for XML, but gives error for JSON.
// $dom = $this->buildDom($namespaced, $application);
Expand Down
19 changes: 6 additions & 13 deletions src/Config/ConfigLocator.php
Expand Up @@ -5,11 +5,10 @@
namespace Drush\Config;

use Consolidation\Config\ConfigInterface;
use Robo\Config\Config;
use Consolidation\Config\Loader\ConfigLoaderInterface;
use Drush\Config\Loader\YamlConfigLoader;
use Consolidation\Config\Loader\ConfigProcessor;
use Consolidation\Config\Util\EnvConfig;
use Drush\Config\Loader\YamlConfigLoader;
use Symfony\Component\Filesystem\Path;

/**
Expand Down Expand Up @@ -192,8 +191,6 @@ public function config(): ConfigInterface
* it to the configuration. The Environment object itself is only
* available during preflight; the information exported here may be
* obtained by commands et. al. as needed. @see Environment::exportConfigData()
*
* @param Environment $environent
*/
public function addEnvironment(Environment $environment): self
{
Expand All @@ -203,10 +200,8 @@ public function addEnvironment(Environment $environment): self

/**
* Add config paths defined in preflight configuration.
*
* @param array $paths
*/
public function addPreflightConfigFiles($filepaths): self
public function addPreflightConfigFiles(array $filepaths): self
{
$this->addConfigPaths(self::PREFLIGHT_CONTEXT, (array) $filepaths);
return $this;
Expand Down Expand Up @@ -253,10 +248,8 @@ public function addDrushConfig(string $drushProjectDir): self
/**
* Add any configuration files found around the Drupal root of the
* selected site.
*
* @param Path to the selected Drupal site
*/
public function addSitewideConfig($siteRoot): ?self
public function addSitewideConfig(string $siteRoot): ?self
{
// There might not be a site.
if (!is_dir($siteRoot)) {
Expand Down Expand Up @@ -503,8 +496,8 @@ function ($item) use ($prefix) {
* @param array $candidates
* An array filenames that are considered config files.
*
* @return
* An array whose first item is an array of files, and the second item is an
* @return array
* The first item is an array of files, and the second item is an
* array of dirs.
*/
protected function findConfigFiles(array $paths, array $candidates): array
Expand Down Expand Up @@ -538,7 +531,7 @@ protected function findConfigFiles(array $paths, array $candidates): array
* @param $siteConfig
* The site-specific config file.
*
* @return
* @return bool
* Whether the config exists and was processed.
*/
public static function addSiteSpecificConfig(DrushConfig $config, $siteConfig): bool
Expand Down
8 changes: 4 additions & 4 deletions src/Drush.php
Expand Up @@ -10,8 +10,8 @@
use Consolidation\SiteAlias\SiteAliasManager;
use Consolidation\SiteProcess\ProcessBase;
use Consolidation\SiteProcess\SiteProcess;
use Drush\Boot\Boot;
use Drush\Boot\BootstrapManager;
use Drush\Boot\DrupalBoot8;
use Drush\Config\DrushConfig;
use Drush\Preflight\PreflightArgs;
use Drush\Runtime\DependencyInjection;
Expand Down Expand Up @@ -360,10 +360,10 @@ public static function process($commandline, $cwd = null, $env = null, $input =
* @param mixed|null $input The input as stream resource, scalar or \Traversable, or null for no input
* @param int|float|null $timeout The timeout in seconds or null to disable
*
* @return
* @return ProcessBase
* A wrapper around Symfony Process.
*/
public static function shell(string $command, $cwd = null, ?array $env = null, $input = null, $timeout = 60): ProcessBase
public static function shell(string $command, ?string $cwd = null, ?array $env = null, mixed $input = null, int|float|null $timeout = 60): ProcessBase
{
return self::processManager()->shell($command, $cwd, $env, $input, $timeout);
}
Expand Down Expand Up @@ -433,7 +433,7 @@ public static function bootstrapManager(): BootstrapManager
/**
* Return the Bootstrap object.
*/
public static function bootstrap(): Boot
public static function bootstrap(): DrupalBoot8
{
return self::bootstrapManager()->bootstrap();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exec/ExecTrait.php
Expand Up @@ -21,7 +21,7 @@ trait ExecTrait
* Optional URI or site path to open in browser. If omitted, or if a site path
* is specified, the current site home page uri will be prepended if the site's
* hostname resolves.
* @return
* @return bool
* TRUE if browser was opened. FALSE if browser was disabled by the user or a
* default browser could not be found.
*/
Expand Down
6 changes: 4 additions & 2 deletions src/Preflight/ArgsPreprocessor.php
Expand Up @@ -126,7 +126,8 @@ protected function isAliasOrSiteSpec(string $arg): bool
* @param $optionsTable Table of option names and the name of the
* method that should be called to process that option.
* @param $opt The option string to check
* @return [$methodName, $optionValue, $acceptsValueFromNextArg]
* @return array
* [$methodName, $optionValue, $acceptsValueFromNextArg]
*/
protected function findMethodForOptionWithValues(array $optionsTable, string $opt): array
{
Expand Down Expand Up @@ -156,7 +157,8 @@ protected function findMethodForOptionWithValues(array $optionsTable, string $op
* '--'. If $key ends with '=', then the option must have a value.
* Otherwise, it cannot be supplied with a value, and always defaults
* to 'true'.
* @return [$methodName, $optionValue, $acceptsValueFromNextArg]
* @return array
* [$methodName, $optionValue, $acceptsValueFromNextArg]
*/
protected function checkMatchingOption(string $opt, string $keyParam, string $methodName): array
{
Expand Down

0 comments on commit 9cb5a45

Please sign in to comment.