GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 9e2d47...99c276 )
by Freek
14:02 queued 12:49
created

TailCommand::getTailCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Tail;
4
5
use Exception;
6
use Illuminate\Console\Command;
7
use Spatie\Ssh\Ssh;
8
use Symfony\Component\Process\Process;
9
10
class TailCommand extends Command
11
{
12
    protected $signature = 'tail {environment?}
13
                            {--lines=0 : Output the last number of lines}
14
                            {--clear : Clear the terminal screen}';
15
16
    protected $description = 'Tail the latest logfile';
17
18
    public function handle()
19
    {
20
        $this->handleClearOption();
21
22
        $environment = $this->argument('environment');
23
24
        is_null($environment)
25
            ? $this->tailLocally()
26
            : $this->tailRemotely($environment);
0 ignored issues
show
Bug introduced by
It seems like $environment defined by $this->argument('environment') on line 22 can also be of type array; however, Spatie\Tail\TailCommand::tailRemotely() does only seem to accept string, 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...
27
    }
28
29
    protected function handleClearOption()
30
    {
31
        if (! $this->option('clear')) {
32
            return;
33
        }
34
35
        $this->output->write(sprintf("\033\143\e[3J"));
36
    }
37
38
    protected function tailLocally(): void
39
    {
40
        $logDirectory = storage_path('logs');
41
42
        Process::fromShellCommandline($this->getTailCommand(), $logDirectory)
43
            ->setTty(true)
44
            ->setTimeout(null)
45
            ->run(function ($type, $line) {
46
                $this->handleClearOption();
47
48
                $this->output->write($line);
49
            });
50
    }
51
52
    protected function tailRemotely(string $environment): void
53
    {
54
        $environmentConfig = $this->getEnvironmentConfiguration($environment);
55
56
        Ssh::create($environmentConfig['user'], $environmentConfig['host'])
57
            ->configureProcess(function (Process $process) {
58
                $process->setTty(true);
59
            })
60
            ->onOutput(function ($type, $line) {
61
                $this->handleClearOption();
62
63
                $this->output->write($line);
64
            })
65
            ->execute([
66
                "cd {$environmentConfig['log_directory']}",
67
                $this->getTailCommand($environmentConfig['log_directory']),
0 ignored issues
show
Unused Code introduced by
The call to TailCommand::getTailCommand() has too many arguments starting with $environmentConfig['log_directory'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
68
            ]);
69
    }
70
71
    protected function getEnvironmentConfiguration(string $environment): array
72
    {
73
        $config = config('tail');
74
75
        if (! isset($config[$environment])) {
76
            throw new Exception("No configuration set for environment `{$environment}`. Make sure this environment is specified in the `tail` config file!");
77
        }
78
79
        return $config[$environment];
80
    }
81
82
    public function getTailCommand(): string
83
    {
84
        return 'tail -f -n '.$this->option('lines').' "`ls -t | head -1`"';
85
    }
86
}
87