diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index 7fcf9095..bc84878f 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -934,8 +934,8 @@ Change array command argument on proc_open to implode spaced string - class: [`Rector\DowngradePhp74\Rector\FuncCall\DowngradeProcOpenArrayCommandArgRector`](../rules/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector.php) ```diff --return proc_open($command, $descriptorspec, $pipes); -+return proc_open(is_array($command) ? implode(' ', $command) : $command, $descriptorspec, $pipes); +-return proc_open($command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]); ++return proc_open(is_array($command) ? implode(' ', array_map('escapeshellarg', $command)) : $command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]); ```
diff --git a/rules-tests/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector/Fixture/fixture.php.inc index 3879a927..61bfb42e 100644 --- a/rules-tests/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector/Fixture/fixture.php.inc +++ b/rules-tests/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector/Fixture/fixture.php.inc @@ -20,7 +20,7 @@ class Fixture { function run(array|string $command) { - $process = proc_open(is_array($command) ? implode(' ', $command) : $command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]); + $process = proc_open(is_array($command) ? implode(' ', array_map('escapeshellarg', $command)) : $command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]); } } diff --git a/rules-tests/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector/Fixture/non_variable.php.inc b/rules-tests/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector/Fixture/non_variable.php.inc index 3ea6f991..1e555e46 100644 --- a/rules-tests/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector/Fixture/non_variable.php.inc +++ b/rules-tests/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector/Fixture/non_variable.php.inc @@ -6,7 +6,7 @@ class NonVariable { function run() { - $process = proc_open(['ls', '-l'], $descriptorspec, $pipes, null, null, ['suppress_errors' => true]); + $process = proc_open(['rm', '-r', 'my dir'], $descriptorspec, $pipes, null, null, ['suppress_errors' => true]); } } @@ -20,7 +20,7 @@ class NonVariable { function run() { - $process = proc_open(is_array(['ls', '-l']) ? implode(' ', ['ls', '-l']) : ['ls', '-l'], $descriptorspec, $pipes, null, null, ['suppress_errors' => true]); + $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]); } } diff --git a/rules/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector.php b/rules/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector.php index e6c57567..4b26623d 100644 --- a/rules/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector.php +++ b/rules/DowngradePhp74/Rector/FuncCall/DowngradeProcOpenArrayCommandArgRector.php @@ -25,11 +25,11 @@ public function getRuleDefinition(): RuleDefinition [ new CodeSample( <<<'CODE_SAMPLE' -return proc_open($command, $descriptorspec, $pipes); +return proc_open($command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]); CODE_SAMPLE , <<<'CODE_SAMPLE' -return proc_open(is_array($command) ? implode(' ', $command) : $command, $descriptorspec, $pipes); +return proc_open(is_array($command) ? implode(' ', array_map('escapeshellarg', $command)) : $command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]); CODE_SAMPLE ), ] @@ -65,7 +65,11 @@ public function refactor(Node $node): ?FuncCall } $isArrayFuncCall = $this->nodeFactory->createFuncCall('is_array', [new Arg($firstArg->value)]); - $implodeFuncCall = $this->nodeFactory->createFuncCall('implode', [new String_(' '), $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);