Skip to content

Commit

Permalink
Rector fixes - DEAD_CODE and PHP81 (#5936)
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzman committed Mar 26, 2024
1 parent d43485b commit 68e5d4b
Show file tree
Hide file tree
Showing 53 changed files with 178 additions and 191 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"jetbrains/phpstorm-attributes": "^1.0",
"mglaman/phpstan-drupal": "^1.2",
"phpunit/phpunit": "^9",
"rector/rector": "^0.12",
"rector/rector": "^1",
"squizlabs/php_codesniffer": "^3.7"
},
"conflict": {
Expand Down
34 changes: 15 additions & 19 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

return static function (RectorConfig $config): void {
$config->importNames();
$config->disableImportShortClasses();
$config->importShortClasses(false);

$config->paths([
__DIR__ . '/src',
Expand All @@ -37,5 +37,7 @@
SimplifyIfElseToTernaryRector::class,
FinalizePublicClassConstantRector::class,
NullToStrictStringFuncCallArgRector::class,
\Rector\Php81\Rector\Array_\FirstClassCallableRector::class,
\Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector::class
]);
};
2 changes: 1 addition & 1 deletion src/Boot/BootstrapManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ protected function maxPhaseLimit($bootstrap_str)
{
$bootstrap_words = explode(' ', $bootstrap_str);
array_shift($bootstrap_words);
if (empty($bootstrap_words)) {
if ($bootstrap_words === []) {
return null;
}
$stop_phase_name = array_shift($bootstrap_words);
Expand Down
17 changes: 9 additions & 8 deletions src/Commands/AutowireTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drush\Commands;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;

Expand All @@ -16,14 +17,14 @@
trait AutowireTrait
{
/**
* Instantiates a new instance of the implementing class using autowiring.
*
* @param \Psr\Container\ContainerInterface $container
* The service container this instance should use.
*
* @return static
*/
public static function create(\Psr\Container\ContainerInterface $container)
* Instantiates a new instance of the implementing class using autowiring.
*
* @param ContainerInterface $container
* The service container this instance should use.
*
* @return static
*/
public static function create(ContainerInterface $container)
{
$args = [];

Expand Down
13 changes: 5 additions & 8 deletions src/Commands/ConfiguresPrompts.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ trait ConfiguresPrompts
/**
* Configure the prompt fallbacks.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param InputInterface $input
* @return void
*/
protected function configurePrompts(InputInterface $input)
{
Prompt::setOutput($this->output);

Prompt::cancelUsing(function () {
Prompt::cancelUsing(function (): never {
Runtime::setCompleted();
exit(1);
});

Prompt::interactive(($input->isInteractive() && defined('STDIN') && stream_isatty(STDIN)) || $this->runningUnitTests());

Prompt::fallbackWhen(!$input->isInteractive() || strtoupper(substr(PHP_OS, 0, 3)) == "WIN" || $this->runningUnitTests());
Prompt::fallbackWhen(!$input->isInteractive() || strtoupper(substr(PHP_OS, 0, 3)) === "WIN" || $this->runningUnitTests());

TextPrompt::fallbackUsing(fn (TextPrompt $prompt) => $this->promptUntilValid(
fn () => (new SymfonyStyle($this->input, $this->output))->ask($prompt->label, $prompt->default ?: null) ?? '',
Expand Down Expand Up @@ -180,10 +180,7 @@ protected function restorePrompts()

protected function runningUnitTests(): bool
{
if (! defined('PHPUNIT_COMPOSER_INSTALL') && ! defined('__PHPUNIT_PHAR__')) {
// is not PHPUnit run
return false;
}
return true;
// is not PHPUnit run
return !(! defined('PHPUNIT_COMPOSER_INSTALL') && ! defined('__PHPUNIT_PHAR__'));
}
}
2 changes: 1 addition & 1 deletion src/Commands/config/ConfigCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function set($config_name, $key, $value, $options = ['input-format' => 's
}

// Special handling for null.
if (strtolower($data) == 'null') {
if (strtolower($data) === 'null') {
$data = null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/config/ConfigExportCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function __construct(protected ConfigManagerInterface $configManager, pro

public static function create(ContainerInterface $container): self
{
$commandHandler = new static(
$commandHandler = new self(
$container->get('config.manager'),
$container->get('config.storage')
);
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/core/BrowseCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class BrowseCommands extends DrushCommands
const BROWSE = 'browse';

public function __construct(
private SiteAliasManagerInterface $siteAliasManager
private readonly SiteAliasManagerInterface $siteAliasManager
) {
parent::__construct();
}
Expand Down
13 changes: 5 additions & 8 deletions src/Commands/core/CacheCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,11 @@ protected function setPrepareData($data, $options)
$data = $this->stdin()->contents();
}

// Now, we parse the object.
switch ($options['input-format']) {
case 'json':
$data = json_decode($data, true);
if ($data === false) {
throw new \Exception('Unable to parse JSON.');
}
break;
if ($options['input-format'] === 'json') {
$data = json_decode($data, true);
if ($data === false) {
throw new \Exception('Unable to parse JSON.');
}
}

if ($options['cache-get']) {
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/core/CacheRebuildCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ final class CacheRebuildCommands extends DrushCommands
const REBUILD = 'cache:rebuild';

public function __construct(
private BootstrapManager $bootstrapManager,
private ClassLoader $autoloader
private readonly BootstrapManager $bootstrapManager,
private readonly ClassLoader $autoloader
) {
parent::__construct();
}
Expand Down
22 changes: 15 additions & 7 deletions src/Commands/core/CliCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

namespace Drush\Commands\core;

use Drupal\Core\Entity\ContentEntityInterface;
use Drush\Psysh\Caster;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\ConfigBase;
use Drupal\Component\DependencyInjection\Container;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drush\Attributes as CLI;
use Drush\Boot\DrupalBootLevels;
Expand Down Expand Up @@ -169,13 +177,13 @@ protected function getDrushCommands(): array
protected function getCasters(): array
{
return [
\Drupal\Core\Entity\ContentEntityInterface::class => \Drush\Psysh\Caster::castContentEntity(...),
\Drupal\Core\Field\FieldItemListInterface::class => \Drush\Psysh\Caster::castFieldItemList(...),
\Drupal\Core\Field\FieldItemInterface::class => \Drush\Psysh\Caster::castFieldItem(...),
\Drupal\Core\Config\Entity\ConfigEntityInterface::class => \Drush\Psysh\Caster::castConfigEntity(...),
\Drupal\Core\Config\ConfigBase::class => \Drush\Psysh\Caster::castConfig(...),
\Drupal\Component\DependencyInjection\Container::class => \Drush\Psysh\Caster::castContainer(...),
\Drupal\Component\Render\MarkupInterface::class => \Drush\Psysh\Caster::castMarkup(...),
ContentEntityInterface::class => Caster::castContentEntity(...),
FieldItemListInterface::class => Caster::castFieldItemList(...),
FieldItemInterface::class => Caster::castFieldItem(...),
ConfigEntityInterface::class => Caster::castConfigEntity(...),
ConfigBase::class => Caster::castConfig(...),
Container::class => Caster::castContainer(...),
MarkupInterface::class => Caster::castMarkup(...),
];
}

Expand Down
6 changes: 2 additions & 4 deletions src/Commands/core/DeployHookCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(
*/
public static function getRegistry(): UpdateRegistry
{
$registry = new class (
return new class (
\Drupal::getContainer()->getParameter('app.root'),
\Drupal::getContainer()->getParameter('site.path'),
\Drupal::service('module_handler')->getModuleList(),
Expand All @@ -63,8 +63,6 @@ public function __construct(
$this->updateType = 'deploy';
}
};

return $registry;
}

/**
Expand Down Expand Up @@ -219,7 +217,7 @@ public static function updateDoOneDeployHook(string $function, array $context):

$variables = Error::decodeException($e);
$variables = array_filter($variables, function ($key) {
return $key[0] == '@' || $key[0] == '%';
return $key[0] === '@' || $key[0] === '%';
}, ARRAY_FILTER_USE_KEY);
// On windows there is a problem with json encoding a string with backslashes.
$variables['%file'] = strtr($variables['%file'], [DIRECTORY_SEPARATOR => '/']);
Expand Down
10 changes: 5 additions & 5 deletions src/Commands/core/DrupalCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ public function requirements($options = ['format' => 'table', 'severity' => -1,
foreach ($requirements as $key => $info) {
$severity = array_key_exists('severity', $info) ? $info['severity'] : -1;
$rows[$key] = [
'title' => self::styleRow((string) $info['title'], $options['format'], $severity),
'value' => self::styleRow(DrupalUtil::drushRender($info['value'] ?? ''), $options['format'], $severity),
'description' => self::styleRow(DrupalUtil::drushRender($info['description'] ?? ''), $options['format'], $severity),
'title' => $this->styleRow((string) $info['title'], $options['format'], $severity),
'value' => $this->styleRow(DrupalUtil::drushRender($info['value'] ?? ''), $options['format'], $severity),
'description' => $this->styleRow(DrupalUtil::drushRender($info['description'] ?? ''), $options['format'], $severity),
'sid' => $severity,
'severity' => self::styleRow(@$severities[$severity], $options['format'], $severity)
'severity' => $this->styleRow(@$severities[$severity], $options['format'], $severity)
];
if ($severity < $min_severity) {
unset($rows[$key]);
Expand Down Expand Up @@ -171,7 +171,7 @@ public function route($options = ['name' => self::REQ, 'path' => self::REQ, 'for
return $items;
}

private static function styleRow($content, $format, $severity): ?string
private function styleRow($content, $format, $severity): ?string
{
if (
!in_array($format, [
Expand Down
8 changes: 3 additions & 5 deletions src/Commands/core/EditCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ public function __construct(
*/
public static function create(ContainerInterface $container, DrushContainer $drush_container): self
{
$commandHandler = new static(
return new self(
$drush_container->get('site.alias.manager'),
);

return $commandHandler;
}

/**
Expand Down Expand Up @@ -87,14 +85,14 @@ public function load($headers = true): array
{
$php_header = $rcs_header = $aliases_header = $drupal_header = $bash_header = $drupal = [];
$php = $this->phpIniFiles();
if (!empty($php)) {
if ($php !== []) {
if ($headers) {
$php_header = ['phpini' => '-- PHP ini files --'];
}
}

$bash = $this->bashFiles();
if (!empty($bash)) {
if ($bash !== []) {
if ($headers) {
$bash_header = ['bash' => '-- Bash files --'];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/core/EntityCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function delete(string $entity_type, $ids = null, array $options = ['bund
$result = $query->execute();

// Don't delete uid=1, uid=0.
if ($entity_type == 'user') {
if ($entity_type === 'user') {
unset($result[0], $result[1]);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Commands/core/EntityCreateCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drush\Commands\core;

use Drupal\Core\Entity\EntityInterface;
use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\AnnotatedCommand\Hooks\HookManager;
use Consolidation\SiteProcess\Util\Escape;
Expand Down Expand Up @@ -121,7 +122,7 @@ private function edit($editor, string $path): string
/**
* Build initial YAML including comments with authoring hints.
*
* @param \Drupal\Core\Field\FieldDefinitionInterface[] $instances
* @param FieldDefinitionInterface[] $instances
*/
private function getInitialYaml(array $instances, ContentEntityInterface $entity, array $options): string
{
Expand Down Expand Up @@ -211,7 +212,7 @@ private function filterViolations(EntityConstraintViolationListInterface &$viola
}
}

protected function setValue(\Drupal\Core\Entity\EntityInterface $entity, int|string $name, mixed $value): void
protected function setValue(EntityInterface $entity, int|string $name, mixed $value): void
{
switch ($entity->get($name)->getFieldDefinition()->getType()) {
case 'timestamp':
Expand Down

0 comments on commit 68e5d4b

Please sign in to comment.