Completed
Push — master ( 5da224...d5db9d )
by Greg
02:47
created

src/Common/CommandArguments.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Robo\Common;
3
4
use Symfony\Component\Process\ProcessUtils;
5
6
/**
7
 * Use this to add arguments and options to the $arguments property.
8
 */
9
trait CommandArguments
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $arguments = '';
15
16
    /**
17
     * Pass argument to executable. Its value will be automatically escaped.
18
     *
19
     * @param string $arg
20
     *
21
     * @return $this
22
     */
23
    public function arg($arg)
24
    {
25
        return $this->args($arg);
26
    }
27
28
    /**
29
     * Pass methods parameters as arguments to executable. Argument values
30
     * are automatically escaped.
31
     *
32
     * @param string|string[] $args
33
     *
34
     * @return $this
35
     */
36
    public function args($args)
37
    {
38
        if (!is_array($args)) {
39
            $args = func_get_args();
40
        }
41
        $this->arguments .= ' ' . implode(' ', array_map('static::escape', $args));
42
        return $this;
43
    }
44
45
    /**
46
     * Pass the provided string in its raw (as provided) form as an argument to executable.
47
     *
48
     * @param string $arg
49
     *
50
     * @return $this
51
     */
52
    public function rawArg($arg)
53
    {
54
        $this->arguments .= " $arg";
55
56
        return $this;
57
    }
58
59
    /**
60
     * Escape the provided value, unless it contains only alphanumeric
61
     * plus a few other basic characters.
62
     *
63
     * @param string $value
64
     *
65
     * @return string
66
     */
67
    public static function escape($value)
68
    {
69
        if (preg_match('/^[a-zA-Z0-9\/\.@~_-]+$/', $value)) {
70
            return $value;
71
        }
72
        return ProcessUtils::escapeArgument($value);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Proces...Utils::escapeArgument() has been deprecated with message: since version 3.3, to be removed in 4.0. Use a command line array or give env vars to the `Process::start/run()` method instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
73
    }
74
75
    /**
76
     * Pass option to executable. Options are prefixed with `--` , value can be provided in second parameter.
77
     * Option values are automatically escaped.
78
     *
79
     * @param string $option
80
     * @param string $value
81
     * @param string $separator
82
     *
83
     * @return $this
84
     */
85
    public function option($option, $value = null, $separator = ' ')
86
    {
87
        if ($option !== null and strpos($option, '-') !== 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
88
            $option = "--$option";
89
        }
90
        $this->arguments .= null == $option ? '' : " " . $option;
91
        $this->arguments .= null == $value ? '' : $separator . static::escape($value);
0 ignored issues
show
It seems like you are loosely comparing $value of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
92
        return $this;
93
    }
94
95
    /**
96
     * Pass multiple options to executable. The associative array contains
97
     * the key:value pairs that become `--key value`, for each item in the array.
98
     * Values are automatically escaped.
99
     */
100
    public function options(array $options, $separator = ' ')
101
    {
102
        foreach ($options as $option => $value) {
103
            $this->option($option, $value, $separator);
104
        }
105
        return $this;
106
    }
107
108
    /**
109
     * Pass an option with multiple values to executable. Value can be a string or array.
110
     * Option values are automatically escaped.
111
     *
112
     * @param string $option
113
     * @param string|array $value
114
     * @param string $separator
115
     *
116
     * @return $this
117
     */
118
    public function optionList($option, $value = array(), $separator = ' ')
119
    {
120
        if (is_array($value)) {
121
            foreach ($value as $item) {
122
                $this->optionList($option, $item, $separator);
123
            }
124
        } else {
125
            $this->option($option, $value, $separator);
126
        }
127
128
        return $this;
129
    }
130
}
131