forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseStrWithResultArgumentRector.php
91 lines (77 loc) · 2.41 KB
/
ParseStrWithResultArgumentRector.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
<?php
declare(strict_types=1);
namespace Rector\Php72\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://3v4l.org/u5pes
* @see https://github.com/gueff/blogimus/commit/04086a10320595470efe446c7ddd90e602aa7228
* @see https://github.com/pxgamer/youtube-dl-php/commit/83cb32b8b36844f2e39f82a862a5ab73da77b608
* @see \Rector\Tests\Php72\Rector\FuncCall\ParseStrWithResultArgumentRector\ParseStrWithResultArgumentRectorTest
*/
final class ParseStrWithResultArgumentRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Use $result argument in parse_str() function',
[
new CodeSample(
<<<'CODE_SAMPLE'
parse_str($this->query);
$data = get_defined_vars();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
parse_str($this->query, $result);
$data = $result;
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, 'parse_str')) {
return null;
}
if (isset($node->args[1])) {
return null;
}
$resultVariable = new Variable('result');
$node->args[1] = new Arg($resultVariable);
$expression = $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
if ($expression === null) {
return null;
}
$nextExpression = $expression->getAttribute(AttributeKey::NEXT_NODE);
if ($nextExpression === null) {
return null;
}
$this->traverseNodesWithCallable($nextExpression, function (Node $node) use ($resultVariable): ?Variable {
if (! $node instanceof FuncCall) {
return null;
}
if (! $this->isName($node, 'get_defined_vars')) {
return null;
}
return $resultVariable;
});
return $node;
}
}