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 ( ebf3bc...e30237 )
by Freek
02:28
created

src/Handlers/Help.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Spatie\SlashCommand\Handlers;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Spatie\SlashCommand\Attachment;
8
use Spatie\SlashCommand\AttachmentField;
9
use Spatie\SlashCommand\HandlesSlashCommand;
10
use Spatie\SlashCommand\Request;
11
use Spatie\SlashCommand\Response;
12
13
class Help extends SignatureHandler
14
{
15
    protected $signature = '* help {command? : The command you want information about}';
16
17
    protected $description = 'List all commands or provide information about all commands';
18
19
    /**
20
     * Handle the given request.
21
     *
22
     * @param \Spatie\SlashCommand\Request $request
23
     *
24
     * @return \Spatie\SlashCommand\Response
25
     */
26
    public function handle(Request $request): Response
27
    {
28
        $handlers = $this->findAvailableHandlers();
29
30
        if ($command = $this->getArgument('command')) {
31
            return $this->displayHelpForCommand($handlers, $command);
32
        }
33
34
        return $this->displayListOfAllCommands($handlers);
35
    }
36
37
    /**
38
     * Find all handlers that are available for the current SlashCommand
39
     * and have a signature.
40
     *
41
     * @return Collection|SignatureHandler[]
42
     */
43
    protected function findAvailableHandlers(): Collection
44
    {
45
        return collect(config('laravel-slack-slash-command.handlers'))
46
            ->map(function (string $handlerClassName) {
47
                return new $handlerClassName($this->request);
48
            })
49
            ->filter(function (HandlesSlashCommand $handler) {
50
                return $handler instanceof SignatureHandler;
51
            })
52 View Code Duplication
            ->filter(function (SignatureHandler $handler) {
53
                $signatureParts = new SignatureParts($handler->getSignature());
54
55
                return Str::is($signatureParts->getSlashCommandName(), $this->request->command);
56
            });
57
    }
58
59
    /**
60
     * Show the help information for a single SignatureHandler.
61
     *
62
     * @param  Collection|SignatureHandler[] $handlers
63
     * @param  string $command
64
     * @return Response
65
     */
66
    protected function displayHelpForCommand(Collection $handlers, string $command): Response
67
    {
68
        $helpRequest = clone $this->request;
69
70
        $helpRequest->text = $command;
71
72
        /** @var \Spatie\SlashCommand\Handlers $handler */
73
        $handler = $handlers->filter(function (HandlesSlashCommand $handler) use ($helpRequest) {
74
            return $handler->canHandle($helpRequest);
75
        })
76
            ->first();
77
78
        $field = AttachmentField::create($handler->getFullCommand(), $handler->getHelpDescription());
79
80
        return $this->respondToSlack('')
81
            ->withAttachment(
82
                Attachment::create()->addField($field)
83
            );
84
    }
85
86
    /**
87
     * Show a list of all available handlers.
88
     *
89
     * @param  Collection|SignatureHandler[] $handlers
90
     * @return Response
91
     */
92
    protected function displayListOfAllCommands(Collection $handlers): Response
93
    {
94
        $attachmentFields = $handlers
95
            ->sort(function (SignatureHandler $handlerA, SignatureHandler $handlerB) {
96
                return strcmp($handlerA->getFullCommand(), $handlerB->getFullCommand());
97
            })
98
            ->map(function (SignatureHandler $handler) {
99
                return AttachmentField::create($handler->getFullCommand(), $handler->getDescription());
100
            })
101
            ->all();
102
103
        return $this->respondToSlack('Available commands:')
104
            ->withAttachment(
105
                Attachment::create()
0 ignored issues
show
It seems like \Spatie\SlashCommand\Att...elds($attachmentFields) can be null; however, withAttachment() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
106
                    ->setColor('good')
107
                    ->setFields($attachmentFields)
108
            );
109
    }
110
}
111