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
Pull Request — master (#17)
by Barry vd.
02:23
created

CatchAll::getCommandListAttachment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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
    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
            ->filter(function (SignatureHandler $handler) {
70
                $signatureParts = new SignatureParts($handler->getSignature());
0 ignored issues
show
Bug introduced by
The method getSignature() does not seem to exist on object<Spatie\SlashComma...dlers\SignatureHandler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
                return Str::is($signatureParts->getSlashCommandName(), $this->request->command);
72
            })
73
            ->map(function(SignatureHandler $handler){
74
                if ($handler instanceof Help) {
0 ignored issues
show
Bug introduced by
The class Spatie\SlashCommand\Handlers\Help does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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
    protected function getCommandListAttachment(Collection $handlers): Attachment
106
    {
107
        $attachmentFields = $handlers->map(function (SignatureHandler $handler) {
108
            return AttachmentField::create($handler->getFullCommand(), $handler->getDescription());
0 ignored issues
show
Bug introduced by
The method getFullCommand() does not seem to exist on object<Spatie\SlashComma...dlers\SignatureHandler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getDescription() does not seem to exist on object<Spatie\SlashComma...dlers\SignatureHandler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
        })
110
            ->all();
111
112
        return Attachment::create()
113
            ->setTitle('Did you mean:')
114
            ->setFields($attachmentFields);
115
    }
116
}
117