Completed
Push — master ( 9a1463...3718bd )
by Basenko
04:21
created

External   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 58
ccs 22
cts 30
cp 0.7332
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A title() 0 6 1
B run() 0 35 9
1
<?php
2
3
namespace MadWeb\Initializer\Actions;
4
5
use Illuminate\Console\Command;
6
use RuntimeException;
7
use Symfony\Component\Process\Process;
8
9
class External extends Action
10
{
11
    private $command;
12
13
    private $arguments;
14
15 18
    public function __construct(Command $artisanCommand, string $command, array $arguments = [])
16
    {
17 18
        parent::__construct($artisanCommand);
18
19 18
        $this->artisanCommand = $artisanCommand;
0 ignored issues
show
Bug introduced by
The property artisanCommand does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
20 18
        $this->command = $command;
21 18
        $this->arguments = $arguments;
22 18
    }
23
24 18
    public function title(): string
25
    {
26 18
        $argString = implode(' ', $this->arguments);
27
28 18
        return "<comment>Running external command:</comment> $this->command $argString";
29
    }
30
31 18
    public function run(): bool
32
    {
33 18
        $Process = new Process(empty($this->arguments)
0 ignored issues
show
Bug introduced by
It seems like empty($this->arguments) ...and), $this->arguments) can also be of type string; however, Symfony\Component\Process\Process::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
34 12
            ? $this->command
35 18
            : array_merge([$this->command], $this->arguments));
36 18
        $Process->setTimeout(null);
37
38 18
        $isVerbose = $this->artisanCommand->getOutput()->isVerbose();
0 ignored issues
show
Bug introduced by
The property artisanCommand does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
39
40 18
        if ($isVerbose) {
41
            $this->artisanCommand->getOutput()->newLine();
0 ignored issues
show
Bug introduced by
The property artisanCommand does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
42
            if (Process::isTtySupported()) {
43
                $Process->setTty(true);
44
            } elseif (Process::isPtySupported()) {
45
                $Process->setPty(true);
46
            }
47
        }
48
49
        $Process->run($isVerbose ? function ($type, $buffer) {
50
            if (Process::ERR === $type) {
51
                $this->artisanCommand->error($buffer);
0 ignored issues
show
Bug introduced by
The property artisanCommand does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
52
            } else {
53
                $this->artisanCommand->line($buffer);
0 ignored issues
show
Bug introduced by
The property artisanCommand does not seem to exist. Did you mean command?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
54
            }
55 18
        } : null);
56
57 18
        $error = $Process->getErrorOutput();
58 18
        $exitCode = $Process->getExitCode();
59
60 18
        if ($error and $exitCode > 0) {
61 6
            throw new RuntimeException(trim($error));
62
        }
63
64 12
        return ! $exitCode;
65
    }
66
}
67