Repository navigation
Expand file tree
/
Copy pathDowngradeProcOpenArrayCommandArgRector.php
More file actions
86 lines (73 loc) · 2.66 KB
/
Copy pathDowngradeProcOpenArrayCommandArgRector.php
File metadata and controls
86 lines (73 loc) · 2.66 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
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp74\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
/**
* @see \Rector\Tests\DowngradePhp74\Rector\FuncCall\DowngradeProcOpenArrayCommandArgRector\DowngradeProcOpenArrayCommandArgRectorTest
*/
final class DowngradeProcOpenArrayCommandArgRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change array command argument on proc_open to implode spaced string',
[
new CodeSample(
<<<'CODE_SAMPLE'
return proc_open($command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
return proc_open(is_array($command) ? implode(' ', array_map('escapeshellarg', $command)) : $command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?FuncCall
{
if (! $this->isName($node, 'proc_open')) {
return null;
}
if ($node->isFirstClassCallable()) {
return null;
}
$firstArg = $node->getArgs()[0];
// already wrapped by this rule on a previous pass; the wrapped ternary type is string|array,
// so the string check below does not catch it and the rule would keep nesting the wrap
if ($firstArg->value instanceof Ternary
&& $firstArg->value->cond instanceof FuncCall
&& $this->isName($firstArg->value->cond, 'is_array')) {
return null;
}
$commandType = $this->getType($firstArg->value);
if ($commandType->isString()->yes()) {
return null;
}
$isArrayFuncCall = $this->nodeFactory->createFuncCall('is_array', [new Arg($firstArg->value)]);
$value = $this->nodeFactory->createFuncCall(
'array_map',
[new Arg(new String_('escapeshellarg')), new Arg($firstArg->value)]
);
$implodeFuncCall = $this->nodeFactory->createFuncCall('implode', [new String_(' '), $value]);
$firstArg->value = new Ternary($isArrayFuncCall, $implodeFuncCall, $firstArg->value);
return $node;
}
}