Completed
Push — master ( 7435ca...27fd9f )
by Sebastian
01:26
created

ProcOpen::run()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5.005

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 16
cts 17
cp 0.9412
rs 9.0648
c 0
b 0
f 0
cc 5
nc 3
nop 2
crap 5.005
1
<?php
2
/**
3
 * This file is part of SebastianFeldmann\Cli.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianFeldmann\Cli\Processor;
11
12
use RuntimeException;
13
use SebastianFeldmann\Cli\Command\Result;
14
use SebastianFeldmann\Cli\Processor;
15
16
/**
17
 * Class ProcOpen
18
 *
19
 * @package SebastianFeldmann\Cli
20
 * @author  Sebastian Feldmann <[email protected]>
21
 * @link    https://github.com/sebastianfeldmann/cli
22
 * @since   Class available since Release 0.9.0
23
 */
24
class ProcOpen implements Processor
25
{
26
    /**
27
     * Execute the command
28
     *
29
     * @param  string $cmd
30
     * @param  int[]  $acceptableExitCodes
31
     * @return \SebastianFeldmann\Cli\Command\Result
32
     */
33 3
    public function run(string $cmd, array $acceptableExitCodes = [0]) : Result
34
    {
35 3
        $old            = error_reporting(0);
36
        $descriptorSpec = [
37 3
            ['pipe', 'r'],
38
            ['pipe', 'w'],
39
            ['pipe', 'w'],
40
        ];
41
42 3
        $process = proc_open($cmd, $descriptorSpec, $pipes);
43 3
        if (!is_resource($process)) {
44
            throw new RuntimeException('can\'t execute \'proc_open\'');
45
        }
46
47
        // Loop on process until it exits normally.
48
        do {
49 3
            $status = proc_get_status($process);
50 3
        } while ($status && $status['running']);
0 ignored issues
show
Bug Best Practice introduced by
The expression $status of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
51
52 3
        $code   = $status['exitcode'] ?? -1;
53 3
        $stdOut = stream_get_contents($pipes[1]);
54 3
        $stdErr = stream_get_contents($pipes[2]);
55
56
        // make sure all pipes are closed before calling proc_close
57 3
        foreach ($pipes as $index => $pipe) {
58 3
            fclose($pipe);
59 3
            unset($pipes[$index]);
60
        }
61
62 3
        proc_close($process);
63 3
        error_reporting($old);
64
65 3
        return new Result($cmd, $code, $stdOut, $stdErr, '', $acceptableExitCodes);
66
    }
67
}
68