Completed
Push — master ( e2a448...4810fe )
by Greg
02:48
created

CommandToExec::mkdirParents()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 3
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 $command;
11
    protected $arguments;
12
    protected $env;
13
    protected $dir;
14
15
    /**
16
     * Hold some command values to later exec
17
     */
18
    public function __construct($command, $arguments, $env = array(), $dir = '')
19
    {
20
        $this->command = $command;
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 escapeshellarg($item);
33
        }, $this->arguments);
34
        return $this->command . ' ' . implode(' ', $escapedArgs);
35
    }
36
37
    /**
38
     * Run our command. Set up the environment, as needed, ensuring that
39
     * it is restored at the end of the run.
40
     */
41
    public function run($stdoutFile = '')
42
    {
43
        $commandString = $this->getCommandString();
44
        $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...
45
        $origDir = FileSystemUtils::applyDir($this->dir);
46
        $exitCode = static::runCommand($commandString, $stdoutFile);
47
        $origEnv->apply();
48
        FileSystemUtils::applyDir($origDir);
49
        return $exitCode;
50
    }
51
52
    /**
53
     * Run a single command.
54
     *
55
     * @param string $commandString
56
     * @return integer
57
     */
58
    public static function runCommand($commandString, $stdoutFile = '')
59
    {
60
        $stdout = STDOUT;
61
        $stderr = STDERR;
62
        if (!empty($stdoutFile)) {
63
            $stdout = array("file", $stdoutFile, "a");
64
            $stderr = $stdout;
65
        }
66
        $process = proc_open($commandString, array(0 => STDIN, 1 => $stdout, 2 => $stderr), $pipes);
67
        $procStatus = proc_get_status($process);
68
        $exitCode = proc_close($process);
69
        return ($procStatus["running"] ? $exitCode : $procStatus["exitcode"]);
70
    }
71
}
72