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

CatchAll::findAlternatives()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 35
Code Lines 20

Duplication

Lines 4
Ratio 11.43 %

Importance

Changes 0
Metric Value
dl 4
loc 35
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 20
nc 3
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
    protected $helpAvailable = false;
16
17
    /**
18
     * If this function returns true, the handle method will get called.
19
     *
20
     * @param \Spatie\SlashCommand\Request $request
21
     *
22
     * @return bool
23
     */
24
    public function canHandle(Request $request): bool
25
    {
26
        return true;
27
    }
28
29
    /**
30
     * Handle the given request. Remember that Slack expects a response
31
     * within three seconds after the slash command was issued. If
32
     * there is more time needed, dispatch a job.
33
     *
34
     * @param \Spatie\SlashCommand\Request $request
35
     *
36
     * @return \Spatie\SlashCommand\Response
37
     */
38
    public function handle(Request $request): Response
39
    {
40
        $response = $this->respondToSlack("I did not recognize this command: `/{$request->command} {$request->text}`");
41
42
        list($command) = explode(' ' , $this->request->text);
43
44
        $alternatives = $this->findAlternatives($command);
45
        if (! $alternatives->isEmpty()) {
46
            $response->withAttachment($this->getCommandListAttachment($alternatives));
47
        }
48
49
        if ($this->helpAvailable) {
50
            $response->withAttachment(Attachment::create()
51
                ->setText("For all available commands, try `/{$request->command} help`")
52
            );
53
        }
54
        return $response;
55
    }
56
57
    protected function findAlternatives(string $command): Collection
58
    {
59
        // Number of characters to change
60
        $threshold = 2;
61
62
        $handlers = collect(config('laravel-slack-slash-command.handlers'))
63
            ->map(function (string $handlerClassName) {
64
                return new $handlerClassName($this->request);
65
            })
66
            ->filter(function (HandlesSlashCommand $handler) {
67
                return $handler instanceof SignatureHandler;
68
            })
69 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...
70
                $signatureParts = new SignatureParts($handler->getSignature());
71
                return Str::is($signatureParts->getSlashCommandName(), $this->request->command);
72
            })
73
            ->map(function(SignatureHandler $handler){
74
                if ($handler instanceof Help) {
75
                    $this->helpAvailable = true;
76
                }
77
                return $handler;
78
            })
79
            ;
80
81
        if (strpos($command, ':') !== false) {
82
            $subHandlers = $this->findInNamespace($handlers, $command);
83
            if (! $subHandlers->isEmpty()) {
84
                return $subHandlers;
85
            }
86
        }
87
88
        return $handlers->filter(function(SignatureHandler $handler) use($command, $threshold) {
89
            return levenshtein($handler->getName(), $command) <= $threshold;
90
        });
91
    }
92
93
    protected function findInNamespace(Collection $handlers, string $command): Collection
94
    {
95
        // Find commands in the same namespace
96
        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...
97
98
        $subHandlers = $handlers->filter(function (SignatureHandler $handler) use($namespace) {
99
            return Str::startsWith($handler->getName(), $namespace . ':' );
100
        });
101
102
        return $subHandlers;
103
    }
104
105 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...
106
    {
107
        $attachmentFields = $handlers->map(function (SignatureHandler $handler) {
108
            return AttachmentField::create($handler->getFullCommand(), $handler->getDescription());
109
        })
110
            ->all();
111
112
        return Attachment::create()
113
            ->setTitle('Did you mean:')
114
            ->setFields($attachmentFields);
115
    }
116
}
117