forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMysqlFuncCallToMysqliRector.php
113 lines (94 loc) · 3.24 KB
/
MysqlFuncCallToMysqliRector.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
<?php
declare(strict_types=1);
namespace Rector\MysqlToMysqli\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Rector\AbstractRector;
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\FuncCall\MysqlFuncCallToMysqliRector\MysqlFuncCallToMysqliRectorTest
*/
final class MysqlFuncCallToMysqliRector extends AbstractRector
{
/**
* @var string
*/
private const MYSQLI_QUERY = 'mysqli_query';
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Converts more complex mysql functions to mysqli',
[
new CodeSample(
<<<'CODE_SAMPLE'
mysql_drop_db($database);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
mysqli_query('DROP DATABASE ' . $database);
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if ($this->isName($node, 'mysql_create_db')) {
return $this->processMysqlCreateDb($node);
}
if ($this->isName($node, 'mysql_drop_db')) {
return $this->processMysqlDropDb($node);
}
if ($this->isName($node, 'mysql_list_dbs')) {
$node->name = new Name(self::MYSQLI_QUERY);
$node->args[0] = new Arg(new String_('SHOW DATABASES'));
}
if ($this->isName($node, 'mysql_list_fields')) {
$node->name = new Name(self::MYSQLI_QUERY);
$node->args[0]->value = $this->joinStringWithNode('SHOW COLUMNS FROM', $node->args[1]->value);
unset($node->args[1]);
}
if ($this->isName($node, 'mysql_list_tables')) {
$node->name = new Name(self::MYSQLI_QUERY);
$node->args[0]->value = $this->joinStringWithNode('SHOW TABLES FROM', $node->args[0]->value);
}
return $node;
}
private function processMysqlCreateDb(FuncCall $funcCall): FuncCall
{
$funcCall->name = new Name(self::MYSQLI_QUERY);
$funcCall->args[0]->value = $this->joinStringWithNode('CREATE DATABASE', $funcCall->args[0]->value);
return $funcCall;
}
private function processMysqlDropDb(FuncCall $funcCall): FuncCall
{
$funcCall->name = new Name(self::MYSQLI_QUERY);
$funcCall->args[0]->value = $this->joinStringWithNode('DROP DATABASE', $funcCall->args[0]->value);
return $funcCall;
}
private function joinStringWithNode(string $string, Expr $expr): Expr
{
if ($expr instanceof String_) {
return new String_($string . ' ' . $expr->value);
}
return new Concat(new String_($string . ' '), $expr);
}
}