-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathUiHelperTraitDrupalPostFormRector.php
110 lines (92 loc) · 3.33 KB
/
UiHelperTraitDrupalPostFormRector.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
<?php
declare(strict_types=1);
namespace DrupalRector\Drupal9\Rector\Deprecation;
use PhpParser\Node;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class UiHelperTraitDrupalPostFormRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated UiHelperTrait::drupalPostForm() calls', [
new CodeSample(
<<<'CODE_BEFORE'
$edit = [];
$edit['action'] = 'action_goto_action';
$this->drupalPostForm('admin/config/system/actions', $edit, 'Create');
$edit['action'] = 'action_goto_action_1';
$this->drupalPostForm(null, $edit, 'Edit');
CODE_BEFORE
,
<<<'CODE_AFTER'
$edit = [];
$edit['action'] = 'action_goto_action';
$this->drupalGet('admin/config/system/actions');
$this->submitForm($edit, 'Create');
$edit['action'] = 'action_goto_action_1';
$this->submitForm($edit, 'Edit');
CODE_AFTER
),
]);
}
public function getNodeTypes(): array
{
return [
Node\Stmt\Expression::class,
];
}
/**
* @param Node\Expr\MethodCall $node
*
* @throws ShouldNotHappenException
*
* @return array<int, ?\PhpParser\Node\Arg>
*/
private function safeArgDestructure(Node\Expr\MethodCall $node): array
{
$count = count($node->args);
if ($count === 3) {
[$path, $edit, $button] = $node->args;
return [$path, $edit, $button, null, null];
}
if ($count === 4) {
[$path, $edit, $button, $options] = $node->args;
return [$path, $edit, $button, $options, null];
}
if ($count === 5) {
return $node->args;
}
throw new ShouldNotHappenException('Unexpected argument count for drupalPostForm');
}
public function refactor(Node $node)
{
assert($node instanceof Node\Stmt\Expression);
if (!($node->expr instanceof Node\Expr\MethodCall)) {
return null;
}
if ($this->getName($node->expr->name) === 'drupalPostForm') {
/** @var Node\Stmt[] $nodes */
$nodes = [];
[$path, $edit, $button, $options, $htmlId] = $this->safeArgDestructure($node->expr);
if ($htmlId === null) {
$submitFormNode = $this->nodeFactory->createLocalMethodCall('submitForm', [$edit, $button]);
} else {
$submitFormNode = $this->nodeFactory->createLocalMethodCall('submitForm', [$edit, $button, $htmlId]);
}
$pathValue = $path->value;
if (!$pathValue instanceof Node\Expr\ConstFetch || strtolower((string) $pathValue->name) !== 'null') {
if ($options === null) {
$drupalGetNode = $this->nodeFactory->createLocalMethodCall('drupalGet', [$path]);
} else {
$drupalGetNode = $this->nodeFactory->createLocalMethodCall('drupalGet', [$path, $options]);
}
$nodes[] = new Node\Stmt\Expression($drupalGetNode);
}
$nodes[] = new Node\Stmt\Expression($submitFormNode);
return $nodes;
}
return null;
}
}