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.

CatchAll::canHandle()   A
last analyzed

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 1
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 CatchAll extends BaseHandler
14
{
15
    /**
16
     * If this function returns true, the handle method will get called.
17
     *
18
     * @param \Spatie\SlashCommand\Request $request
19
     *
20
     * @return bool
21
     */
22
    public function canHandle(Request $request): bool
23
    {
24
        return true;
25
    }
26
27
    /**
28
     * Handle the given request. Remember that Slack expects a response
29
     * within three seconds after the slash command was issued. If
30
     * there is more time needed, dispatch a job.
31
     *
32
     * @param \Spatie\SlashCommand\Request $request
33
     *
34
     * @return \Spatie\SlashCommand\Response
35
     */
36
    public function handle(Request $request): Response
37
    {
38
        $response = $this->respondToSlack("I did not recognize this command: `/{$request->command} {$request->text}`");
39
40
        [$command] = explode(' ', $this->request->text);
0 ignored issues
show
Bug introduced by
The variable $command does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
41
42
        $alternativeHandlers = $this->findAlternativeHandlers($command);
43
44
        if ($alternativeHandlers->count()) {
45
            $response->withAttachment($this->getCommandListAttachment($alternativeHandlers));
46
        }
47
48
        if ($this->containsHelpHandler($alternativeHandlers)) {
49
            $response->withAttachment(Attachment::create()
50
                ->setText("For all available commands, try `/{$request->command} help`")
51
            );
52
        }
53
54
        return $response;
55
    }
56
57
    protected function findAlternativeHandlers(string $command): Collection
58
    {
59
        $alternativeHandlers = collect(config('laravel-slack-slash-command.handlers'))
60
            ->map(function (string $handlerClassName) {
61
                return new $handlerClassName($this->request);
62
            })
63
            ->filter(function (HandlesSlashCommand $handler) {
64
                return $handler instanceof SignatureHandler;
65
            })
66
            ->filter(function (SignatureHandler $handler) {
67
                $signatureParts = new SignatureParts($handler->getSignature());
68
69
                return Str::is($signatureParts->getSlashCommandName(), $this->request->command);
70
            });
71
72
        if (strpos($command, ':') !== false) {
73
            $subHandlers = $this->findInNamespace($alternativeHandlers, $command);
74
            if ($subHandlers->count()) {
75
                return $subHandlers;
76
            }
77
        }
78
79
        return $alternativeHandlers->filter(function (SignatureHandler $handler) use ($command) {
80
            return levenshtein($handler->getName(), $command) <= 2;
81
        });
82
    }
83
84
    protected function findInNamespace(Collection $handlers, string $command): Collection
85
    {
86
        // Find commands in the same namespace
87
        [$namespace, $subCommand] = explode(':', $command);
0 ignored issues
show
Bug introduced by
The variable $namespace does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $subCommand does not exist. Did you mean $command?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
88
89
        $subHandlers = $handlers->filter(function (SignatureHandler $handler) use ($namespace) {
90
            return Str::startsWith($handler->getName(), $namespace.':');
91
        });
92
93
        return $subHandlers;
94
    }
95
96
    protected function getCommandListAttachment(Collection $handlers): Attachment
97
    {
98
        $attachmentFields = $handlers
99
            ->map(function (SignatureHandler $handler) {
100
                return AttachmentField::create($handler->getFullCommand(), $handler->getDescription());
101
            })
102
            ->all();
103
104
        return Attachment::create()
105
            ->setColor('warning')
106
            ->setTitle('Did you mean:')
107
            ->setFields($attachmentFields);
108
    }
109
110
    protected function containsHelpHandler(Collection $alternativeHandlers): bool
111
    {
112
        return ! $alternativeHandlers->filter(function (SignatureHandler $handler) {
113
            return $handler instanceof Help;
114
        })->isEmpty();
115
    }
116
}
117