forked from PHP-CS-Fixer/PHP-CS-Fixer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RegularCallableCallFixer.php
256 lines (204 loc) · 9.03 KB
/
RegularCallableCallFixer.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Fixer\FunctionNotation;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*/
final class RegularCallableCallFixer extends AbstractFixer
{
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Callables must be called without using `call_user_func*` when possible.',
[
new CodeSample(
'<?php
call_user_func("var_dump", 1, 2);
call_user_func("Bar\Baz::d", 1, 2);
call_user_func_array($callback, [1, 2]);
'
),
new CodeSample(
'<?php
call_user_func(function ($a, $b) { var_dump($a, $b); }, 1, 2);
call_user_func(static function ($a, $b) { var_dump($a, $b); }, 1, 2);
'
),
],
null,
'Risky when the `call_user_func` or `call_user_func_array` function is overridden or when are used in constructions that should be avoided, like `call_user_func_array(\'foo\', [\'bar\' => \'baz\'])` or `call_user_func($foo, $foo = \'bar\')`.'
);
}
/**
* {@inheritdoc}
*
* Must run before NativeFunctionInvocationFixer.
* Must run after NoBinaryStringFixer, NoUselessConcatOperatorFixer.
*/
public function getPriority(): int
{
return 2;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_STRING);
}
public function isRisky(): bool
{
return true;
}
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
$functionsAnalyzer = new FunctionsAnalyzer();
$argumentsAnalyzer = new ArgumentsAnalyzer();
for ($index = $tokens->count() - 1; $index > 0; --$index) {
if (!$tokens[$index]->equalsAny([[T_STRING, 'call_user_func'], [T_STRING, 'call_user_func_array']], false)) {
continue;
}
if (!$functionsAnalyzer->isGlobalFunctionCall($tokens, $index)) {
continue; // redeclare/override
}
$openParenthesis = $tokens->getNextMeaningfulToken($index);
$closeParenthesis = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesis);
$arguments = $argumentsAnalyzer->getArguments($tokens, $openParenthesis, $closeParenthesis);
if (1 > \count($arguments)) {
return; // no arguments!
}
$this->processCall($tokens, $index, $arguments);
}
}
/**
* @param non-empty-array<int, int> $arguments
*/
private function processCall(Tokens $tokens, int $index, array $arguments): void
{
$firstArgIndex = $tokens->getNextMeaningfulToken(
$tokens->getNextMeaningfulToken($index)
);
/** @var Token $firstArgToken */
$firstArgToken = $tokens[$firstArgIndex];
if ($firstArgToken->isGivenKind(T_CONSTANT_ENCAPSED_STRING)) {
$afterFirstArgIndex = $tokens->getNextMeaningfulToken($firstArgIndex);
if (!$tokens[$afterFirstArgIndex]->equalsAny([',', ')'])) {
return; // first argument is an expression like `call_user_func("foo"."bar", ...)`, not supported!
}
$firstArgTokenContent = $firstArgToken->getContent();
if (!$this->isValidFunctionInvoke($firstArgTokenContent)) {
return;
}
$newCallTokens = Tokens::fromCode('<?php '.substr(str_replace('\\\\', '\\', $firstArgToken->getContent()), 1, -1).'();');
$newCallTokensSize = $newCallTokens->count();
$newCallTokens->clearAt(0);
$newCallTokens->clearRange($newCallTokensSize - 3, $newCallTokensSize - 1);
$newCallTokens->clearEmptyTokens();
$this->replaceCallUserFuncWithCallback($tokens, $index, $newCallTokens, $firstArgIndex, $firstArgIndex);
} elseif (
$firstArgToken->isGivenKind(T_FUNCTION)
|| (
$firstArgToken->isGivenKind(T_STATIC)
&& $tokens[$tokens->getNextMeaningfulToken($firstArgIndex)]->isGivenKind(T_FUNCTION)
)
) {
$firstArgEndIndex = $tokens->findBlockEnd(
Tokens::BLOCK_TYPE_CURLY_BRACE,
$tokens->getNextTokenOfKind($firstArgIndex, ['{'])
);
$newCallTokens = $this->getTokensSubcollection($tokens, $firstArgIndex, $firstArgEndIndex);
$newCallTokens->insertAt($newCallTokens->count(), new Token(')'));
$newCallTokens->insertAt(0, new Token('('));
$this->replaceCallUserFuncWithCallback($tokens, $index, $newCallTokens, $firstArgIndex, $firstArgEndIndex);
} elseif ($firstArgToken->isGivenKind(T_VARIABLE)) {
$firstArgEndIndex = reset($arguments);
// check if the same variable is used multiple times and if so do not fix
foreach ($arguments as $argumentStart => $argumentEnd) {
if ($firstArgEndIndex === $argumentEnd) {
continue;
}
for ($i = $argumentStart; $i <= $argumentEnd; ++$i) {
if ($tokens[$i]->equals($firstArgToken)) {
return;
}
}
}
// check if complex statement and if so wrap the call in () if on PHP 7 or up, else do not fix
$newCallTokens = $this->getTokensSubcollection($tokens, $firstArgIndex, $firstArgEndIndex);
$complex = false;
for ($newCallIndex = \count($newCallTokens) - 1; $newCallIndex >= 0; --$newCallIndex) {
if ($newCallTokens[$newCallIndex]->isGivenKind([T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, T_VARIABLE])) {
continue;
}
$blockType = Tokens::detectBlockType($newCallTokens[$newCallIndex]);
if (null !== $blockType && (Tokens::BLOCK_TYPE_ARRAY_INDEX_CURLY_BRACE === $blockType['type'] || Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE === $blockType['type'])) {
$newCallIndex = $newCallTokens->findBlockStart($blockType['type'], $newCallIndex);
continue;
}
$complex = true;
break;
}
if ($complex) {
$newCallTokens->insertAt($newCallTokens->count(), new Token(')'));
$newCallTokens->insertAt(0, new Token('('));
}
$this->replaceCallUserFuncWithCallback($tokens, $index, $newCallTokens, $firstArgIndex, $firstArgEndIndex);
}
}
private function replaceCallUserFuncWithCallback(Tokens $tokens, int $callIndex, Tokens $newCallTokens, int $firstArgStartIndex, int $firstArgEndIndex): void
{
$tokens->clearRange($firstArgStartIndex, $firstArgEndIndex);
$afterFirstArgIndex = $tokens->getNextMeaningfulToken($firstArgEndIndex);
$afterFirstArgToken = $tokens[$afterFirstArgIndex];
if ($afterFirstArgToken->equals(',')) {
$useEllipsis = $tokens[$callIndex]->equals([T_STRING, 'call_user_func_array'], false);
if ($useEllipsis) {
$secondArgIndex = $tokens->getNextMeaningfulToken($afterFirstArgIndex);
$tokens->insertAt($secondArgIndex, new Token([T_ELLIPSIS, '...']));
}
$tokens->clearAt($afterFirstArgIndex);
$tokens->removeTrailingWhitespace($afterFirstArgIndex);
}
$tokens->overrideRange($callIndex, $callIndex, $newCallTokens);
$prevIndex = $tokens->getPrevMeaningfulToken($callIndex);
if ($tokens[$prevIndex]->isGivenKind(T_NS_SEPARATOR)) {
$tokens->clearTokenAndMergeSurroundingWhitespace($prevIndex);
}
}
private function getTokensSubcollection(Tokens $tokens, int $indexStart, int $indexEnd): Tokens
{
$size = $indexEnd - $indexStart + 1;
$subCollection = new Tokens($size);
for ($i = 0; $i < $size; ++$i) {
/** @var Token $toClone */
$toClone = $tokens[$i + $indexStart];
$subCollection[$i] = clone $toClone;
}
return $subCollection;
}
private function isValidFunctionInvoke(string $name): bool
{
if (\strlen($name) < 3 || 'b' === $name[0] || 'B' === $name[0]) {
return false;
}
$name = substr($name, 1, -1);
if ($name !== trim($name)) {
return false;
}
return true;
}
}