forked from palantirnet/drupal-rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystemTimeZonesRector.php
104 lines (88 loc) · 3.16 KB
/
SystemTimeZonesRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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\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 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'),
]
),
]);
}
}