Skip to content

Commit 3e960af

Browse files
authored
[DowngradePhp74] Ensure use escapeshellarg() on DowngradeProcOpenArrayCommandArgRector (#199)
1 parent 850b492 commit 3e960af

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

docs/rector_rules_overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,8 @@ Change array command argument on proc_open to implode spaced string
934934
- class: [`Rector\DowngradePhp74\Rector\FuncCall\DowngradeProcOpenArrayCommandArgRector`](../rules/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector.php)
935935

936936
```diff
937-
-return proc_open($command, $descriptorspec, $pipes);
938-
+return proc_open(is_array($command) ? implode(' ', $command) : $command, $descriptorspec, $pipes);
937+
-return proc_open($command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
938+
+return proc_open(is_array($command) ? implode(' ', array_map('escapeshellarg', $command)) : $command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
939939
```
940940

941941
<br>

rules-tests/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector/Fixture/fixture.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Fixture
2020
{
2121
function run(array|string $command)
2222
{
23-
$process = proc_open(is_array($command) ? implode(' ', $command) : $command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
23+
$process = proc_open(is_array($command) ? implode(' ', array_map('escapeshellarg', $command)) : $command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
2424
}
2525
}
2626

rules-tests/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector/Fixture/non_variable.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class NonVariable
66
{
77
function run()
88
{
9-
$process = proc_open(['ls', '-l'], $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
9+
$process = proc_open(['rm', '-r', 'my dir'], $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
1010
}
1111
}
1212

@@ -20,7 +20,7 @@ class NonVariable
2020
{
2121
function run()
2222
{
23-
$process = proc_open(is_array(['ls', '-l']) ? implode(' ', ['ls', '-l']) : ['ls', '-l'], $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
23+
$process = proc_open(is_array(['rm', '-r', 'my dir']) ? implode(' ', array_map('escapeshellarg', ['rm', '-r', 'my dir'])) : ['rm', '-r', 'my dir'], $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
2424
}
2525
}
2626

rules/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public function getRuleDefinition(): RuleDefinition
2525
[
2626
new CodeSample(
2727
<<<'CODE_SAMPLE'
28-
return proc_open($command, $descriptorspec, $pipes);
28+
return proc_open($command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
2929
CODE_SAMPLE
3030
,
3131
<<<'CODE_SAMPLE'
32-
return proc_open(is_array($command) ? implode(' ', $command) : $command, $descriptorspec, $pipes);
32+
return proc_open(is_array($command) ? implode(' ', array_map('escapeshellarg', $command)) : $command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
3333
CODE_SAMPLE
3434
),
3535
]
@@ -65,7 +65,11 @@ public function refactor(Node $node): ?FuncCall
6565
}
6666

6767
$isArrayFuncCall = $this->nodeFactory->createFuncCall('is_array', [new Arg($firstArg->value)]);
68-
$implodeFuncCall = $this->nodeFactory->createFuncCall('implode', [new String_(' '), $firstArg->value]);
68+
$value = $this->nodeFactory->createFuncCall(
69+
'array_map',
70+
[new Arg(new String_('escapeshellarg')), new Arg($firstArg->value)]
71+
);
72+
$implodeFuncCall = $this->nodeFactory->createFuncCall('implode', [new String_(' '), $value]);
6973

7074
$firstArg->value = new Ternary($isArrayFuncCall, $implodeFuncCall, $firstArg->value);
7175

0 commit comments

Comments
 (0)