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 ( 4235fd...911585 )
by Freek
02:30
created

CatchAll::containsHelpHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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
        list($command) = explode(' ', $this->request->text);
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 View Code Duplication
            ->filter(function (SignatureHandler $handler) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
                $signatureParts = new SignatureParts($handler->getSignature());
68
69
                return Str::is($signatureParts->getSlashCommandName(), $this->request->command);
70
            });
71
72
        if (strpos($command, ':') !== false) {
73
74
            $subHandlers = $this->findInNamespace($alternativeHandlers, $command);
75
            if (!$subHandlers->isEmpty()) {
76
                return $subHandlers;
77
            }
78
        }
79
80
        return $alternativeHandlers->filter(function (SignatureHandler $handler) use ($command) {
81
            return levenshtein($handler->getName(), $command) <= 2;
82
        });
83
    }
84
85
    protected function findInNamespace(Collection $handlers, string $command): Collection
86
    {
87
        // Find commands in the same namespace
88
        list($namespace, $subCommand) = explode(':', $command);
0 ignored issues
show
Unused Code introduced by
The assignment to $subCommand is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
89
90
        $subHandlers = $handlers->filter(function (SignatureHandler $handler) use ($namespace) {
91
            return Str::startsWith($handler->getName(), $namespace . ':');
92
        });
93
94
        return $subHandlers;
95
    }
96
97 View Code Duplication
    protected function getCommandListAttachment(Collection $handlers): Attachment
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        $attachmentFields = $handlers
100
            ->map(function (SignatureHandler $handler) {
101
                return AttachmentField::create($handler->getFullCommand(), $handler->getDescription());
102
            })
103
            ->all();
104
105
        return Attachment::create()
106
            ->setTitle('Did you mean:')
107
            ->setFields($attachmentFields);
108
    }
109
110
    protected function containsHelpHandler($alternativeHandlers)
111
    {
112
        $alternativeHandlers->first(function (SignatureHandler $handler) {
113
            return $handler instanceof Help;
114
        });
115
    }
116
}
117