CommandToExec   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getCommandString() 0 10 2
A run() 0 11 1
A runCommand() 0 13 3
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
            if (preg_match('#^[a-zA-Z0-9_-]*$#', $item)) {
33
                return $item;
34
            }
35
            return escapeshellarg($item);
36
        }, $this->arguments);
37
        return $this->execPath . ' ' . implode(' ', $escapedArgs);
38
    }
39
40
    /**
41
     * Run our command. Set up the environment, as needed, ensuring that
42
     * it is restored at the end of the run.
43
     */
44
    public function run($stdoutFile = '')
45
    {
46
        $commandString = $this->getCommandString();
47
        print ">> Running: $commandString\n";
48
        $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...
49
        $origDir = FileSystemUtils::applyDir($this->dir);
50
        $exitCode = static::runCommand($commandString, $stdoutFile);
51
        $origEnv->apply();
52
        FileSystemUtils::applyDir($origDir);
53
        return $exitCode;
54
    }
55
56
    /**
57
     * Run a single command.
58
     *
59
     * @param string $commandString
60
     * @return integer
61
     */
62
    public static function runCommand($commandString, $stdoutFile = '')
63
    {
64
        $stdout = STDOUT;
65
        $stderr = STDERR;
66
        if (!empty($stdoutFile)) {
67
            $stdout = array("file", $stdoutFile, "a");
68
            $stderr = $stdout;
69
        }
70
        $process = proc_open($commandString, array(0 => STDIN, 1 => $stdout, 2 => $stderr), $pipes);
71
        $procStatus = proc_get_status($process);
72
        $exitCode = proc_close($process);
73
        return ($procStatus["running"] ? $exitCode : $procStatus["exitcode"]);
74
    }
75
}
76