Terminal::call()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 3
nc 4
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Console\Services;
6
7
use Illuminate\Http\Request;
8
use Cortex\Console\Console\Commands\Artisan;
9
use Symfony\Component\Console\Input\StringInput;
10
use Symfony\Component\Console\Output\BufferedOutput;
11
use Symfony\Component\Console\Formatter\OutputFormatter;
12
use Illuminate\Console\Application as ConsoleApplication;
13
14
class Terminal extends ConsoleApplication
15
{
16
    /**
17
     * Resolve an array of commands through the application.
18
     *
19
     * @param array|mixed $commands
20
     *
21
     * @return static|\Illuminate\Console\Application
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Terminal.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
22
     */
23
    public function resolveCommands($commands)
24
    {
25
        return in_array(Artisan::class, $commands) ? parent::resolveCommands($commands) : $this;
26
    }
27
28
    /**
29
     * Run an Artisan console command by name.
30
     *
31
     * @param string                                            $command
32
     * @param array                                             $parameters
33
     * @param \Symfony\Component\Console\Output\OutputInterface $outputBuffer
0 ignored issues
show
Documentation introduced by
Should the type for parameter $outputBuffer not be \Symfony\Component\Conso...ut\OutputInterface|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...
34
     *
35
     * @throws \Exception
36
     *
37
     * @return int
38
     */
39
    public function call($command, array $parameters = [], $outputBuffer = null): int
40
    {
41
        $class = $outputBuffer ?: new BufferedOutput();
42
43
        if ($this->request()->ajax() === true) {
44
            $this->lastOutput = new $class(BufferedOutput::VERBOSITY_NORMAL, true, new OutputFormatter(true));
45
            $this->setCatchExceptions(true);
46
        } else {
47
            $this->lastOutput = new $class();
48
            $this->setCatchExceptions(false);
49
        }
50
51
        $parameters = collect($parameters)->prepend($command);
52
        $input = new StringInput($parameters->implode(' '));
53
54
        $input->setInteractive(false);
55
56
        $result = $this->run($input, $this->lastOutput);
57
58
        $this->setCatchExceptions(true);
59
60
        return $result;
61
    }
62
63
    /**
64
     * Request.
65
     *
66
     * @return \Illuminate\Http\Request
67
     */
68
    protected function request()
69
    {
70
        return $this->laravel['request'] ?: Request::capture();
71
    }
72
}
73