From 41fc29fe9a846c5711939af9b787635261b7c725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Brala?= Date: Fri, 24 Nov 2023 19:16:51 +0100 Subject: [PATCH] Add rector for system_time_zones (#271) * Add rector for system_time_zones * Fix styl * Remove extra comment --- config/drupal-10/drupal-10.1-deprecations.php | 6 + docs/rules_overview.md | 25 ++++- .../Deprecation/SystemTimeZonesRector.php | 104 ++++++++++++++++++ .../SystemTimeZonesRectorTest.php | 35 ++++++ .../config/configured_rule.php | 14 +++ .../fixture/system_time_zones.php.inc | 26 +++++ 6 files changed, 208 insertions(+), 2 deletions(-) create mode 100644 src/Drupal10/Rector/Deprecation/SystemTimeZonesRector.php create mode 100644 tests/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector/SystemTimeZonesRectorTest.php create mode 100644 tests/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector/config/configured_rule.php create mode 100644 tests/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector/fixture/system_time_zones.php.inc diff --git a/config/drupal-10/drupal-10.1-deprecations.php b/config/drupal-10/drupal-10.1-deprecations.php index d3668867..0cbe4940 100644 --- a/config/drupal-10/drupal-10.1-deprecations.php +++ b/config/drupal-10/drupal-10.1-deprecations.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use DrupalRector\Drupal10\Rector\Deprecation\SystemTimeZonesRector; use DrupalRector\Drupal10\Rector\Deprecation\WatchdogExceptionRector; use DrupalRector\Rector\Deprecation\FunctionToStaticRector; use DrupalRector\Rector\ValueObject\DrupalIntroducedVersionConfiguration; @@ -23,4 +24,9 @@ $rectorConfig->ruleWithConfiguration(WatchdogExceptionRector::class, [ new DrupalIntroducedVersionConfiguration('10.1.0'), ]); + + // https://www.drupal.org/node/3023528 + $rectorConfig->ruleWithConfiguration(SystemTimeZonesRector::class, [ + new DrupalIntroducedVersionConfiguration('10.1.0'), + ]); }; diff --git a/docs/rules_overview.md b/docs/rules_overview.md index 0398f4cf..7dcc7772 100644 --- a/docs/rules_overview.md +++ b/docs/rules_overview.md @@ -1,10 +1,10 @@ -# 50 Rules Overview +# 51 Rules Overview
## Categories -- [Drupal10](#drupal10) (1) +- [Drupal10](#drupal10) (2) - [Drupal8](#drupal8) (19) @@ -16,6 +16,27 @@ ## Drupal10 +### SystemTimeZonesRector + +Fixes deprecated `system_time_zones()` calls + +:wrench: **configure it!** + +- class: [`DrupalRector\Drupal10\Rector\Deprecation\SystemTimeZonesRector`](../src/Drupal10/Rector/Deprecation/SystemTimeZonesRector.php) + +```diff +-system_time_zones(); +-system_time_zones(FALSE, TRUE); +-system_time_zones(NULL, FALSE); +-system_time_zones(TRUE, FALSE); ++\Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(); ++\Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsListByRegion(); ++\Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(NULL); ++\Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(TRUE); +``` + +
+ ### WatchdogExceptionRector Fixes deprecated watchdog_exception('update', `$exception)` calls diff --git a/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector.php b/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector.php new file mode 100644 index 00000000..e154e402 --- /dev/null +++ b/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector.php @@ -0,0 +1,104 @@ +valueResolver = $valueResolver; + } + + /** + * {@inheritdoc} + */ + public function getNodeTypes(): array + { + return [ + Node\Expr\FuncCall::class, + ]; + } + + public function configure(array $configuration): void + { + foreach ($configuration as $value) { + if (!($value instanceof DrupalIntroducedVersionConfiguration)) { + throw new \InvalidArgumentException(sprintf('Each configuration item must be an instance of "%s"', DrupalIntroducedVersionConfiguration::class)); + } + } + + parent::configure($configuration); + } + + public function refactorWithConfiguration(Node $node, VersionedConfigurationInterface $configuration) + { + if (!$node instanceof Node\Expr\FuncCall || $this->getName($node) !== 'system_time_zones') { + return null; + } + + $args = $node->getArgs(); + + if (count($args) == 2 && $args[1]->value instanceof ConstFetch && $this->valueResolver->isTrue($args[1]->value)) { + return $this->nodeFactory->createStaticCall('Drupal\Core\Datetime\TimeZoneFormHelper', 'getOptionsListByRegion'); + } + + if (count($args) == 0) { + return $this->nodeFactory->createStaticCall('Drupal\Core\Datetime\TimeZoneFormHelper', 'getOptionsList'); + } + + if (count($args) <= 2) { + return $this->nodeFactory->createStaticCall('Drupal\Core\Datetime\TimeZoneFormHelper', 'getOptionsList', [$args[0]]); + } + + return null; + } + + /** + * {@inheritdoc} + */ + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition('Fixes deprecated system_time_zones() calls', [ + new ConfiguredCodeSample( + <<<'CODE_BEFORE' +system_time_zones(); +system_time_zones(FALSE, TRUE); +system_time_zones(NULL, FALSE); +system_time_zones(TRUE, FALSE); +CODE_BEFORE + , + <<<'CODE_AFTER' +\Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(); +\Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsListByRegion(); +\Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(NULL); +\Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(TRUE); +CODE_AFTER + , + [ + new DrupalIntroducedVersionConfiguration('10.1.0'), + ] + ), + ]); + } +} diff --git a/tests/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector/SystemTimeZonesRectorTest.php b/tests/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector/SystemTimeZonesRectorTest.php new file mode 100644 index 00000000..04a0f300 --- /dev/null +++ b/tests/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector/SystemTimeZonesRectorTest.php @@ -0,0 +1,35 @@ +doTestFile($filePath); + } + + /** + * @return Iterator<> + */ + public static function provideData(): \Iterator + { + return self::yieldFilesFromDirectory(__DIR__.'/fixture'); + } + + public function provideConfigFilePath(): string + { + // must be implemented + return __DIR__.'/config/configured_rule.php'; + } +} diff --git a/tests/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector/config/configured_rule.php b/tests/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector/config/configured_rule.php new file mode 100644 index 00000000..ab29fabf --- /dev/null +++ b/tests/src/Drupal10/Rector/Deprecation/SystemTimeZonesRector/config/configured_rule.php @@ -0,0 +1,14 @@ + + +----- + system_time_zones(), fn() => \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList()); + + \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => system_time_zones(FALSE, TRUE), fn() => \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsListByRegion()); + + \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => system_time_zones(NULL, FALSE), fn() => \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(NULL)); + + \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => system_time_zones(TRUE, FALSE), fn() => \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(TRUE)); +} +?>