forked from rectorphp/rector-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringToArrayArgumentProcessRector.php
More file actions
161 lines (138 loc) · 4.93 KB
/
StringToArrayArgumentProcessRector.php
File metadata and controls
161 lines (138 loc) · 4.93 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
155
156
157
158
159
160
161
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony42\Rector\New_;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use Rector\PhpParser\NodeTransformer;
use Rector\Rector\AbstractRector;
use Rector\Util\Reflection\PrivatesAccessor;
use Symfony\Component\Console\Input\StringInput;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://github.com/symfony/symfony/pull/27821/files
*
* @see \Rector\Symfony\Tests\Symfony42\Rector\New_\StringToArrayArgumentProcessRector\StringToArrayArgumentProcessRectorTest
*/
final class StringToArrayArgumentProcessRector extends AbstractRector
{
/**
* @var string[]
*/
private const EXCLUDED_PROCESS_METHOD_CALLS = ['setWorkingDirectory', 'addOutput', 'addErrorOutput', 'setInput'];
public function __construct(
private readonly NodeTransformer $nodeTransformer
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes Process string argument to an array',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\Process\Process;
$process = new Process('ls -l');
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\Process\Process;
$process = new Process(['ls', '-l']);
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [New_::class, MethodCall::class];
}
/**
* @param New_|MethodCall $node
*/
public function refactor(Node $node): ?Node
{
$expr = $node instanceof New_ ? $node->class : $node->var;
if ($this->isObjectType($expr, new ObjectType('Symfony\Component\Process\Process'))) {
return $this->processArgumentPosition($node, 0);
}
if ($this->isObjectType($expr, new ObjectType('Symfony\Component\Console\Helper\ProcessHelper'))) {
return $this->processArgumentPosition($node, 1);
}
return null;
}
private function processArgumentPosition(New_|MethodCall $node, int $argumentPosition): ?Node
{
if (! isset($node->args[$argumentPosition])) {
return null;
}
$activeArg = $node->args[$argumentPosition];
if (! $activeArg instanceof Arg) {
return null;
}
$activeArgValue = $activeArg->value;
if ($activeArgValue instanceof Array_) {
return null;
}
if ($node instanceof MethodCall && $this->shouldSkipProcessMethodCall($node)) {
return null;
}
// type analyzer
$activeValueType = $this->getType($activeArgValue);
if (! $activeValueType instanceof StringType) {
return null;
}
$hasChanged = $this->processStringType($node, $argumentPosition, $activeArgValue);
if (! $hasChanged) {
return null;
}
return $node;
}
private function shouldSkipProcessMethodCall(MethodCall $methodCall): bool
{
$methodName = (string) $this->nodeNameResolver->getName($methodCall->name);
return in_array($methodName, self::EXCLUDED_PROCESS_METHOD_CALLS, true);
}
private function processStringType(New_|MethodCall $expr, int $argumentPosition, Expr $firstArgumentExpr): bool
{
if ($firstArgumentExpr instanceof Concat) {
$arrayNode = $this->nodeTransformer->transformConcatToStringArray($firstArgumentExpr);
$expr->args[$argumentPosition] = new Arg($arrayNode);
return true;
}
$args = $expr->getArgs();
$hasChanged = false;
if ($firstArgumentExpr instanceof FuncCall && $this->isName($firstArgumentExpr, 'sprintf')) {
$arrayNode = $this->nodeTransformer->transformSprintfToArray($firstArgumentExpr);
if ($arrayNode instanceof Array_) {
$args[$argumentPosition]->value = $arrayNode;
$hasChanged = true;
}
} elseif ($firstArgumentExpr instanceof String_) {
$parts = $this->splitProcessCommandToItems($firstArgumentExpr->value);
$args[$argumentPosition]->value = $this->nodeFactory->createArray($parts);
$hasChanged = true;
}
return $hasChanged;
}
/**
* @return string[]
*/
private function splitProcessCommandToItems(string $process): array
{
$privatesAccessor = new PrivatesAccessor();
return $privatesAccessor->callPrivateMethod(new StringInput(''), 'tokenize', [$process]);
}
}