forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMysqlAssignToMysqliRector.php
196 lines (158 loc) · 6.15 KB
/
MysqlAssignToMysqliRector.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
declare(strict_types=1);
namespace Rector\MysqlToMysqli\Rector\Assign;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\LNumber;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://www.phpclasses.org/blog/package/9199/post/3-Smoothly-Migrate-your-PHP-Code-using-the-Old-MySQL-extension-to-MySQLi.html
* @see \Rector\Tests\MysqlToMysqli\Rector\Assign\MysqlAssignToMysqliRector\MysqlAssignToMysqliRectorTest
*/
final class MysqlAssignToMysqliRector extends AbstractRector
{
/**
* @var array<string, string>
*/
private const FIELD_TO_FIELD_DIRECT = [
'mysql_field_len' => 'length',
'mysql_field_name' => 'name',
'mysql_field_table' => 'table',
];
/**
* @var string
*/
private const MYSQLI_DATA_SEEK = 'mysqli_data_seek';
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Converts more complex mysql functions to mysqli',
[
new CodeSample(
<<<'CODE_SAMPLE'
$data = mysql_db_name($result, $row);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
mysqli_data_seek($result, $row);
$fetch = mysql_fetch_row($result);
$data = $fetch[0];
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Assign::class];
}
/**
* @param Assign $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->expr instanceof FuncCall) {
return null;
}
/** @var FuncCall $funcCallNode */
$funcCallNode = $node->expr;
if ($this->isName($funcCallNode, 'mysql_tablename')) {
return $this->processMysqlTableName($node, $funcCallNode);
}
if ($this->isName($funcCallNode, 'mysql_db_name')) {
return $this->processMysqlDbName($node, $funcCallNode);
}
if ($this->isName($funcCallNode, 'mysql_db_query')) {
return $this->processMysqliSelectDb($node, $funcCallNode);
}
if ($this->isName($funcCallNode, 'mysql_fetch_field')) {
return $this->processMysqlFetchField($node, $funcCallNode);
}
if ($this->isName($funcCallNode, 'mysql_result')) {
return $this->processMysqlResult($node, $funcCallNode);
}
return $this->processFieldToFieldDirect($node, $funcCallNode);
}
private function processMysqlTableName(Assign $assign, FuncCall $funcCall): FuncCall
{
$funcCall->name = new Name(self::MYSQLI_DATA_SEEK);
$newFuncCall = new FuncCall(new Name('mysql_fetch_array'), [$funcCall->args[0]]);
$newAssignNode = new Assign($assign->var, new ArrayDimFetch($newFuncCall, new LNumber(0)));
$this->addNodeAfterNode($newAssignNode, $assign);
return $funcCall;
}
private function processMysqlDbName(Assign $assign, FuncCall $funcCall): FuncCall
{
$funcCall->name = new Name(self::MYSQLI_DATA_SEEK);
$mysqlFetchRowFuncCall = new FuncCall(new Name('mysqli_fetch_row'), [$funcCall->args[0]]);
$fetchVariable = new Variable('fetch');
$newAssignNode = new Assign($fetchVariable, $mysqlFetchRowFuncCall);
$this->addNodeAfterNode($newAssignNode, $assign);
$newAssignNode = new Assign($assign->var, new ArrayDimFetch($fetchVariable, new LNumber(0)));
$this->addNodeAfterNode($newAssignNode, $assign);
return $funcCall;
}
private function processMysqliSelectDb(Assign $assign, FuncCall $funcCall): FuncCall
{
$funcCall->name = new Name('mysqli_select_db');
$newAssignNode = new Assign($assign->var, new FuncCall(new Name('mysqli_query'), [$funcCall->args[1]]));
$this->addNodeAfterNode($newAssignNode, $assign);
unset($funcCall->args[1]);
return $funcCall;
}
private function processMysqlFetchField(Assign $assign, FuncCall $funcCall): Assign
{
if (isset($funcCall->args[1])) {
$funcCall->name = new Name('mysqli_fetch_field_direct');
} else {
$funcCall->name = new Name('mysqli_fetch_field');
}
return $assign;
}
private function processMysqlResult(Assign $assign, FuncCall $funcCall): FuncCall
{
$fetchField = null;
if (isset($funcCall->args[2])) {
$fetchField = $funcCall->args[2]->value;
unset($funcCall->args[2]);
}
$funcCall->name = new Name(self::MYSQLI_DATA_SEEK);
$mysqlFetchArrayFuncCall = new FuncCall(new Name('mysqli_fetch_array'), [$funcCall->args[0]]);
$fetchVariable = new Variable('fetch');
$newAssignNode = new Assign($fetchVariable, $mysqlFetchArrayFuncCall);
$this->addNodeAfterNode($newAssignNode, $assign);
$newAssignNode = new Assign($assign->var, new ArrayDimFetch($fetchVariable, $fetchField ?? new LNumber(0)));
$this->addNodeAfterNode($newAssignNode, $assign);
return $funcCall;
}
private function processFieldToFieldDirect(Assign $assign, FuncCall $funcCall): ?Assign
{
foreach (self::FIELD_TO_FIELD_DIRECT as $funcName => $property) {
if ($this->isName($funcCall, $funcName)) {
$parentNode = $funcCall->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof PropertyFetch) {
continue;
}
if ($parentNode instanceof StaticPropertyFetch) {
continue;
}
$funcCall->name = new Name('mysqli_fetch_field_direct');
$assign->expr = new PropertyFetch($funcCall, $property);
return $assign;
}
}
return null;
}
}