Skip to content

Commit

Permalink
PHPStan Level 3 compat (#6044)
Browse files Browse the repository at this point in the history
* PHPStan Level 3 compat

* Fix extra space
  • Loading branch information
weitzman authored Jun 21, 2024
1 parent efe36c4 commit 8b77c9a
Show file tree
Hide file tree
Showing 19 changed files with 40 additions and 42 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 2
level: 3
editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%'
bootstrapFiles:
- phpstan-bootstrap.php
Expand Down
5 changes: 2 additions & 3 deletions src/Backend/BackendPathEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Drush\Backend;

use Consolidation\SiteAlias\SiteAlias;
use Consolidation\SiteAlias\HostPath;
use Consolidation\SiteAlias\SiteAlias;
use Drush\Drush;

class BackendPathEvaluator
Expand Down Expand Up @@ -35,9 +35,8 @@ public function evaluate(HostPath $path)
* returned.
*
* @param HostPath $path The host and path to resolve aliases on.
* @return string
*/
public function resolve(HostPath $path)
public function resolve(HostPath $path): string|false
{
if (!$path->hasPathAlias()) {
return false;
Expand Down
1 change: 1 addition & 0 deletions src/Boot/BootstrapManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public function bootstrap(): DrupalBoot8
if (!$this->bootstrap) {
$this->bootstrap = $this->bootstrapObjectForRoot($this->getRoot());
}
assert($this->bootstrap instanceof DrupalBoot8);
return $this->bootstrap;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Commands/DrushCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected function io(): DrushStyle
// Specify our own Style class when needed.
$this->io = new DrushStyle($this->input(), $this->output());
}
assert($this->io instanceof DrushStyle);
return $this->io;
}

Expand All @@ -71,6 +72,7 @@ protected function io(): DrushStyle
*/
public function logger(): ?DrushLoggerManager
{
assert($this->logger instanceof DrushLoggerManager);
return $this->logger;
}

Expand Down
5 changes: 1 addition & 4 deletions src/Commands/config/ConfigCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ public function __construct(
parent::__construct();
}

/**
* @return StorageInterface
*/
public function getConfigStorageExport()
public function getConfigStorageExport(): StorageManagerInterface|StorageCacheInterface
{
if (isset($this->configStorageExport)) {
return $this->configStorageExport;
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/core/MigrateRunnerCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Filesystem\Path;

class MigrateRunnerCommands extends DrushCommands
final class MigrateRunnerCommands extends DrushCommands
{
use AutowireTrait;

Expand Down
1 change: 1 addition & 0 deletions src/Commands/field/FieldBaseOverrideCreateCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ protected function createBaseFieldOverride(string $entityType, string $bundle, s
->setRequired($isRequired)
->save();

assert($override instanceof BaseFieldOverride);
return $override;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Commands/field/FieldCreateCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,11 @@ protected function getEntityDisplay(string $context): ?EntityDisplayInterface
$entityType = $this->input->getArgument('entityType');
$bundle = $this->input->getArgument('bundle');

return $this->entityTypeManager
$return = $this->entityTypeManager
->getStorage(sprintf('entity_%s_display', $context))
->load(sprintf('%s.%s.default', $entityType, $bundle));
assert($return instanceof EntityDisplayInterface || $return === null);
return $return;
}

protected function logResult(FieldConfigInterface $field): void
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/field/FieldEntityReferenceHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Drush\Commands\DrushCommands;
use Symfony\Component\Console\Input\InputInterface;

class FieldEntityReferenceHooks extends DrushCommands
final class FieldEntityReferenceHooks extends DrushCommands
{
use AutowireTrait;
use EntityTypeBundleValidationTrait;
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/field/FieldTextHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

class FieldTextHooks extends DrushCommands
final class FieldTextHooks extends DrushCommands
{
use AutowireTrait;
use EntityTypeBundleValidationTrait;
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/help/ListCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function renderListRaw(array $namespaced): void
/**
* @param Command[] $all
*
* @return Command[]
* @return array<string, array<Command>>
*/
public static function categorize(array $all, string $separator = ':'): array
{
Expand Down
1 change: 1 addition & 0 deletions src/Commands/pm/PmCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public function validateUninstall(CommandData $commandData): void
$modules = StringUtils::csvToArray($modules);
if ($validation_reasons = $this->getModuleInstaller()->validateUninstall($modules)) {
foreach ($validation_reasons as $module => $reasons) {
// @phpstan-ignore foreach.nonIterable
foreach ($reasons as $reason) {
$list[] = "$module: " . (string)$reason;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Commands/sql/sanitize/SanitizePluginInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ public function sanitize($result, CommandData $commandData);
/**
* Use #[CLI\Hook(type: HookManager::ON_EVENT, target: SanitizeCommands::CONFIRMS)]
*
* @param array $messages An array of messages to show during confirmation.
* @param array $messages An array of messages to show during confirmation. Make changes by reference.
* @param InputInterface $input The effective commandline input for this request.
*
* @return String[]
* An array of messages.
*/
public function messages(array &$messages, InputInterface $input);
}
8 changes: 4 additions & 4 deletions src/Config/ConfigAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ trait ConfigAwareTrait
* DrushConfig as return type. Helps with IDE completion.
*
* @see https://stackoverflow.com/a/37687295.
*
* @return DrushConfig
*/
public function getConfig()
public function getConfig(): DrushConfig
{
return $this->parentGetConfig();
$return = $this->parentGetConfig();
assert($return instanceof DrushConfig, 'Expected DrushConfig, got ' . get_class($return) . '.');
return $return;
}
}
3 changes: 1 addition & 2 deletions src/Config/ConfigLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Drush\Config;

use Consolidation\Config\ConfigInterface;
use Consolidation\Config\Loader\ConfigLoader;
use Consolidation\Config\Loader\ConfigProcessor;
use Consolidation\Config\Util\EnvConfig;
Expand Down Expand Up @@ -178,7 +177,7 @@ protected function addToSources(array $sources): void
* Return the configuration object. Create it and load it with
* all identified configuration if necessary.
*/
public function config(): ConfigInterface
public function config(): DrushConfig
{
return $this->config;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Drush.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ public static function output(): OutputInterface
*/
public static function drush(SiteAliasInterface $siteAlias, string $command, array $args = [], array $options = [], array $options_double_dash = []): SiteProcess
{
return self::processManager()->drush($siteAlias, $command, $args, $options, $options_double_dash);
$return = self::processManager()->drush($siteAlias, $command, $args, $options, $options_double_dash);
assert($return instanceof SiteProcess);
return $return;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Preflight/Preflight.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public function config(): DrushConfig
* @param $argv
* True if the request was successfully redispatched remotely. False if the request should proceed.
*
* @return array{preflightDidRedispatch: bool, exitStatus: int}
* @return array{bool, int}
*/
public function preflight($argv): array
{
Expand Down
21 changes: 9 additions & 12 deletions src/Runtime/LegacyServiceFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@ protected function addModuleDrushServiceProvider($module, $filename)
}

/**
* @param string $module Module name
* @param string $dir Full path to module base dir
* List of discovered drush.service.yml files
*
* @return string[]
* List of discovered drush.service.yml files
* @param $module Module name
* @param $dir Full path to module base dir
*/
protected function findModuleDrushServiceProvider($module, $dir)
protected function findModuleDrushServiceProvider(string $module, string $dir): string|null
{
$services = $this->findModuleDrushServiceProviderFromComposer($dir);
if (!$services) {
Expand All @@ -81,11 +80,10 @@ protected function findModuleDrushServiceProvider($module, $dir)
}

/**
* Gets one discovered drush.service.yml file
*
* @param string $module Module name
* @param string $dir Full path to module base dir
*
* @return string
* One discovered drush.service.yml file
*/
protected function findDefaultServicesFile($module, $dir)
{
Expand All @@ -97,6 +95,8 @@ protected function findDefaultServicesFile($module, $dir)
}

/**
* Get Drush services section from module's composer.json file
*
* In composer.json, the Drush version constraints will appear
* in the 'extra' section like so:
*
Expand All @@ -113,11 +113,8 @@ protected function findDefaultServicesFile($module, $dir)
* is used.
*
* @param string $dir Full path to module base dir
*
* @return array
* Drush services section from module's composer.json file
*/
protected function findModuleDrushServiceProviderFromComposer($dir)
protected function findModuleDrushServiceProviderFromComposer($dir): array|false
{
$composerJsonPath = "$dir/composer.json";
if (!file_exists($composerJsonPath)) {
Expand Down
10 changes: 5 additions & 5 deletions src/Runtime/LegacyServiceInstantiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ protected function resolveArguments(array $arguments)
*
* @param array $arg Argument to resolve
*
* @return object
* @return mixed
* Argument after it has been resolved by DI container
*/
protected function resolveArgument($arg)
protected function resolveArgument($arg): mixed
{
if (!is_string($arg)) {
return $arg;
Expand Down Expand Up @@ -259,7 +259,7 @@ protected function resolveArgument($arg)
* @param Container $container Drupal DI container
* @param string $arg Argument to resolve
*
* @return object
* @return ?object
* Resolved object from DI container
*/
protected function resolveFromContainer($container, string $arg)
Expand All @@ -283,12 +283,12 @@ protected function resolveFromContainer($container, string $arg)
* those that do not are required.
*
*
* @return bool, string
* @return array{bool, string}
* Boolean indicating whether the object is required to be in the container,
* and a string with the name of the object to look up (passed input with
* any leading ? removed).
*/
protected function isRequired(string $arg)
protected function isRequired(string $arg): array
{
if ($arg[0] === '?') {
return [false, substr($arg, 1)];
Expand Down

0 comments on commit 8b77c9a

Please sign in to comment.