Completed
Push — master ( 6b5da6...9a1463 )
by Basenko
04:09
created

External::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace MadWeb\Initializer\ExecutorActions;
4
5
use Illuminate\Console\Command;
6
use Symfony\Component\Process\Process;
7
8
class External
9
{
10
    private $artisanCommand;
11
12
    private $command;
13
14
    private $arguments;
15
16 12
    public function __construct(Command $artisanCommand, string $command, array $arguments = [])
17
    {
18 12
        $this->artisanCommand = $artisanCommand;
19 12
        $this->command = $command;
20 12
        $this->arguments = $arguments;
21 12
    }
22
23 12
    private function title()
24
    {
25 12
        $argString = implode(' ', $this->arguments);
26
27 12
        return "<comment>Running external command:</comment> $this->command $argString";
28
    }
29
30 12
    public function __invoke(): bool
31
    {
32
        return $this->artisanCommand->task($this->title(), function () {
33 12
            $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 6
                ? $this->command
35 12
                : array_merge([$this->command], $this->arguments));
36 12
            $Process->setTimeout(null);
37
38 12
            $isVerbose = $this->artisanCommand->getOutput()->isVerbose();
39
40 12
            if ($isVerbose) {
41
                $this->artisanCommand->getOutput()->newLine();
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);
52
                } else {
53
                    $this->artisanCommand->line($buffer);
54
                }
55 12
            } : null);
56
57 12
            return ! $Process->getExitCode();
58 12
        });
59
    }
60
}
61