forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenGetAllToObjectRector.php
177 lines (156 loc) · 5.06 KB
/
TokenGetAllToObjectRector.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
<?php
declare(strict_types=1);
namespace Rector\Php80\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\NodeManipulator\TokenManipulator;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://wiki.php.net/rfc/token_as_object
*
* @see \Rector\Tests\Php80\Rector\FuncCall\TokenGetAllToObjectRector\TokenGetAllToObjectRectorTest
*/
final class TokenGetAllToObjectRector extends AbstractRector
{
/**
* @var TokenManipulator
*/
private $tokenManipulator;
public function __construct(TokenManipulator $ifArrayTokenManipulator)
{
$this->tokenManipulator = $ifArrayTokenManipulator;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Complete missing constructor dependency instance by type',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run()
{
$tokens = token_get_all($code);
foreach ($tokens as $token) {
if (is_array($token)) {
$name = token_name($token[0]);
$text = $token[1];
} else {
$name = null;
$text = $token;
}
}
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run()
{
$tokens = \PhpToken::getAll($code);
foreach ($tokens as $phpToken) {
$name = $phpToken->getTokenName();
$text = $phpToken->text;
}
}
}
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->nodeNameResolver->isName($node, 'token_get_all')) {
return null;
}
$this->refactorTokensVariable($node);
return $this->nodeFactory->createStaticCall('PhpToken', 'getAll', $node->args);
}
private function refactorTokensVariable(FuncCall $funcCall): void
{
$assign = $funcCall->getAttribute(AttributeKey::PARENT_NODE);
if (! $as#stanceof Assign) {
return;
}
/** @var ClassMethod|Function_|null $classMethodOrFunction */
$classMethodOrFunction = $this->betterNodeFinder->findParentTypes(
$funcCall,
[ClassMethod::class, Function_::class]
);
if ($classMethodOrFunction === null) {
return;
}
// dummy approach, improve when needed
$this->replaceGetNameOrGetValue($classMethodOrFunction, $assign->var);
}
/**
* @param ClassMethod|Function_ $functionLike
*/
private function replaceGetNameOrGetValue(FunctionLike $functionLike, Expr $assignedExpr): void
{
$tokensForeaches = $this->findForeachesOverTokenVariable($functionLike, $assignedExpr);
foreach ($tokensForeaches as $tokenForeach) {
$this->refactorTokenInForeach($tokenForeach);
}
}
/**
* @param ClassMethod|Function_ $functionLike
* @return Foreach_[]
*/
private function findForeachesOverTokenVariable(FunctionLike $functionLike, Expr $assignedExpr): array
{
return $this->betterNodeFinder->find((array) $functionLike->stmts, function (Node $node) use (
$assignedExpr
): bool {
if (! $node instanceof Foreach_) {
return false;
}
return $this->nodeComparator->areNodesEqual($node->expr, $assignedExpr);
});
}
private function refactorTokenInForeach(Foreach_ $tokensForeach): void
{
$singleToken = $tokensForeach->valueVar;
if (! $singleToken instanceof Variable) {
return;
}
$this->traverseNodesWithCallable($tokensForeach, function (Node $node) use ($singleToken) {
$this->tokenManipulator->refactorArrayToken([$node], $singleToken);
$this->tokenManipulator->refactorNonArrayToken([$node], $singleToken);
$this->tokenManipulator->refactorTokenIsKind([$node], $singleToken);
$this->tokenManipulator->removeIsArray([$node], $singleToken);
// drop if "If_" node not needed
if ($node instanceof If_ && $node->else !== null) {
if (! $this->nodeComparator->areNodesEqual($node->stmts, $node->else->stmts)) {
return null;
}
$this->unwrapStmts($node->stmts, $node);
$this->removeNode($node);
}
});
}
}