Skip to content

Commit ccd9262

Browse files
committed
Reformatted code and comments
1 parent d371f08 commit ccd9262

File tree

1 file changed

+78
-50
lines changed

1 file changed

+78
-50
lines changed

src/Command.php

Lines changed: 78 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,51 @@
1212
class Command
1313
{
1414
/**
15-
* @var bool whether to escape any argument passed through `addArg()`. Default is `true`.
15+
* @var bool whether to escape any argument passed through `addArg()`.
16+
* Default is `true`.
1617
*/
1718
public $escapeArgs = true;
1819

1920
/**
20-
* @var bool whether to escape the command passed to `setCommand()` or the constructor.
21-
* This is only useful if `$escapeArgs` is `false`. Default is `false`.
21+
* @var bool whether to escape the command passed to `setCommand()` or the
22+
* constructor. This is only useful if `$escapeArgs` is `false`. Default
23+
* is `false`.
2224
*/
2325
public $escapeCommand = false;
2426

2527
/**
26-
* @var bool whether to use `exec()` instead of `proc_open()`. This can be used on Windows system
27-
* to workaround some quirks there. Note, that any errors from your command will be output directly
28-
* to the PHP output stream. `getStdErr()` will also not work anymore and thus you also won't get
29-
* the error output from `getError()` in this case. You also can't pass any environment
30-
* variables to the command if this is enabled. Default is `false`.
28+
* @var bool whether to use `exec()` instead of `proc_open()`. This can be
29+
* used on Windows system to workaround some quirks there. Note, that any
30+
* errors from your command will be output directly to the PHP output
31+
* stream. `getStdErr()` will also not work anymore and thus you also won't
32+
* get the error output from `getError()` in this case. You also can't pass
33+
* any environment variables to the command if this is enabled. Default is
34+
* `false`.
3135
*/
3236
public $useExec = false;
3337

3438
/**
35-
* @var bool whether to capture stderr (2>&1) when `useExec` is true. This will try to redirect the
36-
* stderr to stdout and provide the complete output of both in `getStdErr()` and `getError()`.
37-
* Default is `true`.
39+
* @var bool whether to capture stderr (2>&1) when `useExec` is true. This
40+
* will try to redirect the stderr to stdout and provide the complete
41+
* output of both in `getStdErr()` and `getError()`. Default is `true`.
3842
*/
3943
public $captureStdErr = true;
4044

4145
/**
42-
* @var string|null the initial working dir for `proc_open()`. Default is `null` for current PHP working dir.
46+
* @var string|null the initial working dir for `proc_open()`. Default is
47+
* `null` for current PHP working dir.
4348
*/
4449
public $procCwd;
4550

4651
/**
47-
* @var array|null an array with environment variables to pass to `proc_open()`. Default is `null` for none.
52+
* @var array|null an array with environment variables to pass to
53+
* `proc_open()`. Default is `null` for none.
4854
*/
4955
public $procEnv;
5056

5157
/**
52-
* @var array|null an array of other_options for `proc_open()`. Default is `null` for none.
58+
* @var array|null an array of other_options for `proc_open()`. Default is
59+
* `null` for none.
5360
*/
5461
public $procOptions;
5562

@@ -63,7 +70,8 @@ class Command
6370
public $nonBlockingMode;
6471

6572
/**
66-
* @var null|string the locale to temporarily set before calling `escapeshellargs()`. Default is `null` for none.
73+
* @var null|string the locale to temporarily set before calling
74+
* `escapeshellargs()`. Default is `null` for none.
6775
*/
6876
public $locale;
6977

@@ -113,7 +121,8 @@ class Command
113121
protected $_executed = false;
114122

115123
/**
116-
* @param string|array $options either a command string or an options array (see setOptions())
124+
* @param string|array $options either a command string or an options array
125+
* @see setOptions
117126
*/
118127
public function __construct($options = null)
119128
{
@@ -125,9 +134,10 @@ public function __construct($options = null)
125134
}
126135

127136
/**
128-
* @param array $options array of name => value options that should be applied to the object
129-
* You can also pass options that use a setter, e.g. you can pass a `fileName` option which
130-
* will be passed to `setFileName()`.
137+
* @param array $options array of name => value options that should be
138+
* applied to the object You can also pass options that use a setter, e.g.
139+
* you can pass a `fileName` option which will be passed to
140+
* `setFileName()`.
131141
* @throws \Exception
132142
* @return static for method chaining
133143
*/
@@ -149,9 +159,10 @@ public function setOptions($options)
149159
}
150160

151161
/**
152-
* @param string $command the command or full command string to execute, like 'gzip' or 'gzip -d'.
153-
* You can still call addArg() to add more arguments to the command. If $escapeCommand was set to true,
154-
* the command gets escaped through escapeshellcmd().
162+
* @param string $command the command or full command string to execute,
163+
* like 'gzip' or 'gzip -d'. You can still call addArg() to add more
164+
* arguments to the command. If $escapeCommand was set to true, the command
165+
* gets escaped with escapeshellcmd().
155166
* @return static for method chaining
156167
*/
157168
public function setCommand($command)
@@ -160,10 +171,12 @@ public function setCommand($command)
160171
$command = escapeshellcmd($command);
161172
}
162173
if ($this->getIsWindows()) {
163-
// Make sure to switch to correct drive like "E:" first if we have a full path in command
174+
// Make sure to switch to correct drive like "E:" first if we have
175+
// a full path in command
164176
if (isset($command[1]) && $command[1]===':') {
165177
$position = 1;
166-
// Could be a quoted absolute path because of spaces. i.e. "C:\Program Files (x86)\file.exe"
178+
// Could be a quoted absolute path because of spaces.
179+
// i.e. "C:\Program Files (x86)\file.exe"
167180
} elseif (isset($command[2]) && $command[2]===':') {
168181
$position = 2;
169182
} else {
@@ -172,18 +185,23 @@ public function setCommand($command)
172185

173186
// Absolute path. If it's a relative path, let it slide.
174187
if ($position) {
175-
$command = sprintf($command[$position - 1].': && cd %s && %s', escapeshellarg(dirname($command)), escapeshellarg(basename($command)));
188+
$command = sprintf(
189+
$command[$position - 1] . ': && cd %s && %s',
190+
escapeshellarg(dirname($command)),
191+
escapeshellarg(basename($command))
192+
);
176193
}
177194
}
178195
$this->_command = $command;
179196
return $this;
180197
}
181198

182199
/**
183-
* @param string|resource $stdIn If set, the string will be piped to the command via standard input.
184-
* This enables the same functionality as piping on the command line.
185-
* It can also be a resource like a file handle or a stream in which case its content will be piped
186-
* into the command like an input redirection.
200+
* @param string|resource $stdIn If set, the string will be piped to the
201+
* command via standard input. This enables the same functionality as
202+
* piping on the command line. It can also be a resource like a file
203+
* handle or a stream in which case its content will be piped into the
204+
* command like an input redirection.
187205
* @return static for method chaining
188206
*/
189207
public function setStdIn($stdIn) {
@@ -192,16 +210,18 @@ public function setStdIn($stdIn) {
192210
}
193211

194212
/**
195-
* @return string|null the command that was set through setCommand() or passed to the constructor. Null if none.
213+
* @return string|null the command that was set through setCommand() or
214+
* passed to the constructor. `null` if none.
196215
*/
197216
public function getCommand()
198217
{
199218
return $this->_command;
200219
}
201220

202221
/**
203-
* @return string|bool the full command string to execute. If no command was set with setCommand()
204-
* or passed to the constructor it will return false.
222+
* @return string|bool the full command string to execute. If no command
223+
* was set with setCommand() or passed to the constructor it will return
224+
* `false`.
205225
*/
206226
public function getExecCommand()
207227
{
@@ -218,7 +238,8 @@ public function getExecCommand()
218238
}
219239

220240
/**
221-
* @param string $args the command arguments as string. Note that these will not get escaped!
241+
* @param string $args the command arguments as string. Note that these
242+
* will not get escaped!
222243
* @return static for method chaining
223244
*/
224245
public function setArgs($args)
@@ -228,33 +249,38 @@ public function setArgs($args)
228249
}
229250

230251
/**
231-
* @return string the command args that where set through setArgs() or added with addArg() separated by spaces
252+
* @return string the command args that where set with setArgs() or added
253+
* with addArg() separated by spaces
232254
*/
233255
public function getArgs()
234256
{
235257
return implode(' ', $this->_args);
236258
}
237259

238260
/**
239-
* @param string $key the argument key to add e.g. `--feature` or `--name=`. If the key does not end with
240-
* and `=`, the $value will be separated by a space, if any. Keys are not escaped unless $value is null
261+
* @param string $key the argument key to add e.g. `--feature` or
262+
* `--name=`. If the key does not end with and `=`, the $value will be
263+
* separated by a space, if any. Keys are not escaped unless $value is null
241264
* and $escape is `true`.
242-
* @param string|array|null $value the optional argument value which will get escaped if $escapeArgs is true.
243-
* An array can be passed to add more than one value for a key, e.g. `addArg('--exclude', array('val1','val2'))`
244-
* which will create the option `--exclude 'val1' 'val2'`.
245-
* @param bool|null $escape if set, this overrides the $escapeArgs setting and enforces escaping/no escaping
265+
* @param string|array|null $value the optional argument value which will
266+
* get escaped if $escapeArgs is true. An array can be passed to add more
267+
* than one value for a key, e.g. `addArg('--exclude',
268+
* array('val1','val2'))` which will create the option `--exclude 'val1'
269+
* 'val2'`.
270+
* @param bool|null $escape if set, this overrides the $escapeArgs setting
271+
* and enforces escaping/no escaping
246272
* @return static for method chaining
247273
*/
248274
public function addArg($key, $value = null, $escape = null)
249275
{
250-
$doEscape = $escape!==null ? $escape : $this->escapeArgs;
251-
$useLocale = $doEscape && $this->locale!==null;
276+
$doEscape = $escape !== null ? $escape : $this->escapeArgs;
277+
$useLocale = $doEscape && $this->locale !== null;
252278

253279
if ($useLocale) {
254280
$locale = setlocale(LC_CTYPE, 0); // Returns current locale setting
255281
setlocale(LC_CTYPE, $this->locale);
256282
}
257-
if ($value===null) {
283+
if ($value === null) {
258284
// Only escape single arguments if explicitely requested
259285
$this->_args[] = $escape ? escapeshellarg($key) : $key;
260286
} else {
@@ -264,9 +290,10 @@ public function addArg($key, $value = null, $escape = null)
264290
foreach ($value as $v) {
265291
$params[] = $doEscape ? escapeshellarg($v) : $v;
266292
}
267-
$this->_args[] = $key.$separator.implode(' ',$params);
293+
$this->_args[] = $key . $separator.implode(' ',$params);
268294
} else {
269-
$this->_args[] = $key.$separator.($doEscape ? escapeshellarg($value) : $value);
295+
$this->_args[] = $key . $separator .
296+
($doEscape ? escapeshellarg($value) : $value);
270297
}
271298
}
272299
if ($useLocale) {
@@ -287,7 +314,8 @@ public function getOutput($trim = true)
287314

288315
/**
289316
* @param bool $trim whether to `trim()` the return value. The default is `true`.
290-
* @return string the error message, either stderr or internal message. Empty if none.
317+
* @return string the error message, either stderr or an internal message.
318+
* Empty string if none.
291319
*/
292320
public function getError($trim = true)
293321
{
@@ -322,8 +350,8 @@ public function getExecuted()
322350
/**
323351
* Execute the command
324352
*
325-
* @return bool whether execution was successful. If false, error details can be obtained through
326-
* getError(), getStdErr() and getExitCode().
353+
* @return bool whether execution was successful. If `false`, error details
354+
* can be obtained from getError(), getStdErr() and getExitCode().
327355
*/
328356
public function execute()
329357
{
@@ -337,7 +365,7 @@ public function execute()
337365
$execCommand = $this->captureStdErr ? "$command 2>&1" : $command;
338366
exec($execCommand, $output, $this->_exitCode);
339367
$this->_stdOut = implode("\n", $output);
340-
if ($this->_exitCode!==0) {
368+
if ($this->_exitCode !== 0) {
341369
$this->_stdErr = $this->_stdOut;
342370
$this->_error = empty($this->_stdErr) ? 'Command failed' : $this->_stdErr;
343371
return false;
@@ -460,6 +488,6 @@ public function getIsWindows()
460488
*/
461489
public function __toString()
462490
{
463-
return (string)$this->getExecCommand();
491+
return (string) $this->getExecCommand();
464492
}
465493
}

0 commit comments

Comments
 (0)