Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rector for system_time_zones #271

Merged
merged 3 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/drupal-10/drupal-10.1-deprecations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'),
]);
};
25 changes: 23 additions & 2 deletions docs/rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# 50 Rules Overview
# 51 Rules Overview

<br>

## Categories

- [Drupal10](#drupal10) (1)
- [Drupal10](#drupal10) (2)

- [Drupal8](#drupal8) (19)

Expand All @@ -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);
```

<br>

### WatchdogExceptionRector

Fixes deprecated watchdog_exception('update', `$exception)` calls
Expand Down
104 changes: 104 additions & 0 deletions src/Drupal10/Rector/Deprecation/SystemTimeZonesRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace DrupalRector\Drupal10\Rector\Deprecation;

use DrupalRector\Contract\VersionedConfigurationInterface;
use DrupalRector\Rector\AbstractDrupalCoreRector;
use DrupalRector\Rector\ValueObject\DrupalIntroducedVersionConfiguration;
use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

class SystemTimeZonesRector extends AbstractDrupalCoreRector
{
/**
* @var array|DrupalIntroducedVersionConfiguration[]
*/
protected array $configuration;

/**
* @var \Rector\Core\PhpParser\Node\Value\ValueResolver
*/
private ValueResolver $valueResolver;

public function __construct(ValueResolver $valueResolver)
{
$this->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'),
]
),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DrupalRector\Tests\Drupal10\Rector\Deprecation\SystemTimeZonesRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

class SystemTimeZonesRectorTest extends AbstractRectorTestCase
{
/**
* @covers ::refactor
*
* @dataProvider provideData
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<<string>>
*/
public static function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__.'/fixture');
}

public function provideConfigFilePath(): string
{
// must be implemented
return __DIR__.'/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use DrupalRector\Drupal10\Rector\Deprecation\SystemTimeZonesRector;
use DrupalRector\Rector\ValueObject\DrupalIntroducedVersionConfiguration;
use DrupalRector\Tests\Rector\Deprecation\DeprecationBase;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
DeprecationBase::addClass(SystemTimeZonesRector::class, $rectorConfig, false, [
new DrupalIntroducedVersionConfiguration('10.1.0'),
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

function simple_example() {
system_time_zones();

system_time_zones(FALSE, TRUE);

system_time_zones(NULL, FALSE);

system_time_zones(TRUE, FALSE);
}
?>

-----
<?php

function simple_example() {
\Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => 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));
}
?>