-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathExecuteQueryParamsToBindValueRector.php
More file actions
154 lines (128 loc) · 4.22 KB
/
ExecuteQueryParamsToBindValueRector.php
File metadata and controls
154 lines (128 loc) · 4.22 KB
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
<?php
declare(strict_types=1);
namespace Rector\Doctrine\Dbal40\Rector\StmtsAwareInterface;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Plus;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\NodeFinder;
use PHPStan\Type\ObjectType;
use Rector\Doctrine\Enum\DoctrineClass;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Doctrine\Tests\Dbal40\Rector\StmtsAwareInterface\ExecuteQueryParamsToBindValueRector\ExecuteQueryParamsToBindValueRectorTest
*/
final class ExecuteQueryParamsToBindValueRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change executeQuery() with parameters to bindValue() with explicit values', [
new CodeSample(
<<<'CODE_SAMPLE'
use Doctrine\DBAL\Statement;
class SomeClass
{
public function run(Statement $statement, array $params): void
{
$result = $statement->executeQuery($params);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Doctrine\DBAL\Statement;
class SomeClass
{
public function run(Statement $statement, array $params): void
{
foreach ($params as $key => $parameter) {
$statement->bindValue(is_int($key) ? $key + 1 : $key, $parameter);
}
$result = $statement->executeQuery();
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return NodeGroup::STMTS_AWARE;
}
/**
* @param StmtsAware $node
*/
public function refactor(Node $node): ?Node
{
if ($node->stmts === null) {
return null;
}
$nodeFinder = new NodeFinder();
$hasChanged = false;
$objectType = new ObjectType(DoctrineClass::DBAL_STATEMENT);
foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Expression) {
continue;
}
$executeQueryMethodCall = $nodeFinder->findFirst($stmt, function (Node $node) use ($objectType): bool {
if (! $node instanceof MethodCall) {
return false;
}
if (! $this->isObjectType($node->var, $objectType)) {
return false;
}
if (! $this->isName($node->name, 'executeQuery')) {
return false;
}
return count($node->getArgs()) === 1;
});
if (! $executeQueryMethodCall instanceof MethodCall) {
continue;
}
// remove args
$stmtsExpr = $executeQueryMethodCall->getArgs()[0]
->value;
$executeQueryMethodCall->args = [];
$hasChanged = true;
$bindValueForeach = $this->createBindValueForeach($executeQueryMethodCall->var, $stmtsExpr);
array_splice($node->stmts, $key, 1, [$bindValueForeach, $stmt]);
}
if ($hasChanged) {
return $node;
}
return null;
}
private function createBindValueForeach(Expr $statementExpr, Expr $stmtsExpr): Foreach_
{
$keyVariable = new Variable('key');
$parameterVariable = new Variable('parameter');
$foreach = new Foreach_($stmtsExpr, $parameterVariable, [
'keyVar' => $keyVariable,
]);
$ternary = new Ternary(
new FuncCall(new Name('is_int'), [new Arg($keyVariable)]),
new Plus($keyVariable, new Int_(1)),
$keyVariable
);
$bindValueMethodCall = new MethodCall($statementExpr, 'bindValue', [
new Arg($ternary),
new Arg($parameterVariable),
]);
$foreach->stmts[] = new Expression($bindValueMethodCall);
return $foreach;
}
}