Exec::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
3
namespace N98\Util;
4
5
use RuntimeException;
6
7
/**
8
 * Class Exec
9
 * @package N98\Util
10
 */
11
class Exec
12
{
13
    /**
14
     * @var string
15
     */
16
    const REDIRECT_STDERR_TO_STDOUT = ' 2>&1';
17
18
    /**
19
     * @var int (0-255)
20
     */
21
    const CODE_CLEAN_EXIT = 0;
22
23
    /**
24
     * @param string $command
25
     * @param string $commandOutput
0 ignored issues
show
Documentation introduced by
Should the type for parameter $commandOutput not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
26
     * @param int $returnCode
0 ignored issues
show
Documentation introduced by
Should the type for parameter $returnCode not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
27
     */
28
    public static function run($command, &$commandOutput = null, &$returnCode = null)
29
    {
30
        $command = $command . self::REDIRECT_STDERR_TO_STDOUT;
31
32
        exec($command, $commandOutput, $returnCode);
33
        $commandOutput = self::parseCommandOutput($commandOutput);
34
35
        if ($returnCode !== self::CODE_CLEAN_EXIT) {
36
            throw new RuntimeException($commandOutput);
37
        }
38
    }
39
40
    /**
41
     * Exec class is allowed to run
42
     *
43
     * @return bool
44
     */
45
    public static function allowed()
46
    {
47
        return function_exists('exec');
48
    }
49
50
    /**
51
     * @param $commandOutput
52
     * @return string
53
     */
54
    private static function parseCommandOutput($commandOutput)
55
    {
56
        return implode(PHP_EOL, $commandOutput);
57
    }
58
}
59