-
Notifications
You must be signed in to change notification settings - Fork 74
/
FunctionToStaticRector.php
127 lines (114 loc) · 4 KB
/
FunctionToStaticRector.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
namespace DrupalRector\Rector\Deprecation;
use DrupalRector\Contract\VersionedConfigurationInterface;
use DrupalRector\Rector\AbstractDrupalCoreRector;
use DrupalRector\Rector\ValueObject\FunctionToStaticConfiguration;
use PhpParser\Node;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* Replaces function calls to static method calls.
*
* Example: \DrupalRector\Rector\Deprecation\FileDirectoryTempOsRector
*
* What is covered:
* - Static replacement
*/
class FunctionToStaticRector extends AbstractDrupalCoreRector
{
/**
* @var array|FunctionToStaticConfiguration[]
*/
protected array $configuration;
/**
* {@inheritdoc}
*/
public function getNodeTypes(): array
{
return [
Node\Expr\FuncCall::class,
];
}
public function configure(array $configuration): void
{
foreach ($configuration as $value) {
if (!($value instanceof FunctionToStaticConfiguration)) {
throw new \InvalidArgumentException(sprintf('Each configuration item must be an instance of "%s"', FunctionToStaticConfiguration::class));
}
}
parent::configure($configuration);
}
public function refactorWithConfiguration(Node $node, VersionedConfigurationInterface $configuration): ?Node
{
assert($node instanceof Node\Expr\FuncCall);
assert($configuration instanceof FunctionToStaticConfiguration);
if ($this->getName($node) === $configuration->getDeprecatedFunctionName()) {
$args = $node->getArgs();
if (count($configuration->getArgumentReorder()) > 0) {
$origArgs = $node->getArgs();
foreach ($configuration->getArgumentReorder() as $oldPosition => $newPosition) {
$args[$newPosition] = $origArgs[$oldPosition];
}
}
$fullyQualified = new Node\Name\FullyQualified($configuration->getClassName());
return new Node\Expr\StaticCall($fullyQualified, $configuration->getMethodName(), $args);
}
return null;
}
/**
* {@inheritdoc}
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated file_directory_os_temp() calls, used in Drupal 8, 9 and 10 deprecations', [
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$dir = file_directory_os_temp();
CODE_BEFORE
,
<<<'CODE_AFTER'
$dir = \Drupal\Component\FileSystem\FileSystem::getOsTemporaryDirectory();
CODE_AFTER
,
[
new FunctionToStaticConfiguration('8.1.0', 'file_directory_os_temp', 'Drupal\Component\FileSystem\FileSystem', 'getOsTemporaryDirectory'),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$settings = [];
$filename = 'simple_filename.yaml';
drupal_rewrite_settings($settings, $filename);
CODE_BEFORE
,
<<<'CODE_AFTER'
$settings = [];
$filename = 'simple_filename.yaml';
SettingsEditor::rewrite($filename, $settings);
CODE_AFTER
,
[
new FunctionToStaticConfiguration('10.1.0', 'drupal_rewrite_settings', 'Drupal\Core\Site\SettingsEditor', 'rewrite', [0 => 1, 1 => 0]),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$settings = [];
$filename = 'simple_filename.yaml';
drupal_rewrite_settings($settings, $filename);
CODE_BEFORE
,
<<<'CODE_AFTER'
$settings = [];
$filename = 'simple_filename.yaml';
SettingsEditor::rewrite($filename, $settings);
CODE_AFTER
,
[
new FunctionToStaticConfiguration('10.1.0', 'drupal_rewrite_settings', 'Drupal\Core\Site\SettingsEditor', 'rewrite', [0 => 1, 1 => 0]),
]
),
]);
}
}