Completed
Pull Request — master (#22)
by Greg
01:36
created

CommandToExec::escapeOne()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Consolidation\Cgr;
4
5
/**
6
 * Hold a command string + envrionment to execute.
7
 */
8
class CommandToExec
9
{
10
    protected $execPath;
11
    protected $arguments;
12
    protected $env;
13
    protected $dir;
14
15
    /**
16
     * Hold some command values to later exec
17
     */
18
    public function __construct($execPath, $arguments, $env = array(), $dir = '')
19
    {
20
        $this->execPath = $execPath;
21
        $this->arguments = $arguments;
22
        $this->env = new Env($env);
23
        $this->dir = $dir;
24
    }
25
26
    /**
27
     * Generate a single command string.
28
     */
29
    public function getCommandString()
30
    {
31
        $escapedArgs = array_map(function ($item) {
32
            return static::escapeOne($item);
33
        }, $this->arguments);
34
        return $this->execPath . ' ' . implode(' ', $escapedArgs);
35
    }
36
37
    public static function escapeOne($item)
38
    {
39
        if (preg_match('#^[a-zA-Z0-9_-]*$#', $item)) {
40
            return $item;
41
        }
42
        return escapeshellarg($item);
43
    }
44
45
    /**
46
     * Run our command. Set up the environment, as needed, ensuring that
47
     * it is restored at the end of the run.
48
     */
49
    public function run($stdoutFile = '')
50
    {
51
        $commandString = $this->getCommandString();
52
        print ">> Running: $commandString\n";
53
        $origEnv = $this->env->apply($this->env);
0 ignored issues
show
Unused Code introduced by
The call to Env::apply() has too many arguments starting with $this->env.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
54
        $origDir = FileSystemUtils::applyDir($this->dir);
55
        $exitCode = static::runCommand($commandString, $stdoutFile);
56
        $origEnv->apply();
57
        FileSystemUtils::applyDir($origDir);
58
        return $exitCode;
59
    }
60
61
    /**
62
     * Run a single command.
63
     *
64
     * @param string $commandString
65
     * @return integer
66
     */
67
    public static function runCommand($commandString, $stdoutFile = '')
68
    {
69
        $stdout = STDOUT;
70
        $stderr = STDERR;
71
        if (!empty($stdoutFile)) {
72
            $stdout = array("file", $stdoutFile, "a");
73
            $stderr = $stdout;
74
        }
75
        $process = proc_open($commandString, array(0 => STDIN, 1 => $stdout, 2 => $stderr), $pipes);
76
        $procStatus = proc_get_status($process);
77
        $exitCode = proc_close($process);
78
        return ($procStatus["running"] ? $exitCode : $procStatus["exitcode"]);
79
    }
80
}
81