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 (#14)
by Barry vd.
02:24
created

Help::handle()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 8.7396
c 0
b 0
f 0
cc 4
eloc 28
nc 2
nop 1
1
<?php
2
3
namespace Spatie\SlashCommand\Handlers;
4
5
use Illuminate\Support\Str;
6
use Spatie\SlashCommand\Attachment;
7
use Spatie\SlashCommand\AttachmentField;
8
use Spatie\SlashCommand\HandlesSlashCommand;
9
use Spatie\SlashCommand\Request;
10
use Spatie\SlashCommand\Response;
11
12
class Help extends BaseHandler
13
{
14
15
    /**
16
     * Check if the command begins with 'help'
17
     *
18
     * @param \Spatie\SlashCommand\Request $request
19
     *
20
     * @return bool
21
     */
22
    public function canHandle(Request $request): bool
23
    {
24
        return Str::startsWith($request->text, 'help');
25
    }
26
27
    /**
28
     * Handle the given request.
29
     *
30
     * @param \Spatie\SlashCommand\Request $request
31
     *
32
     * @return \Spatie\SlashCommand\Response
33
     */
34
    public function handle(Request $request): Response
35
    {
36
        $command = trim(substr($this->request->text, 4));
37
        $helpRequest = clone $this->request;
38
        $helpRequest->text = $command;
39
40
        $handlers = collect(config('laravel-slack-slash-command.handlers'))
41
            ->map(function (string $handlerClassName) use($helpRequest) {
42
                return new $handlerClassName($helpRequest);
43
            })
44
            ->filter(function (HandlesSlashCommand $handler) use ($helpRequest){
45
                if ($handler instanceof SignatureHandler && isset($handler->signature)) {
46
                    $signatureParts = new SignatureParts($handler->signature);
0 ignored issues
show
Bug introduced by
The property signature does not seem to exist. Did you mean signatureIsBound?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
47
                    return in_array($signatureParts->getSlashCommandName(), [$this->request->command, '*']);
48
                }
49
            });
50
51
        // When command is passed, find all commands
52
        if (! empty($command)) {
53
54
            /** @var SignatureHandler $handler */
55
            $handler = $handlers
56
                ->filter(function (HandlesSlashCommand $handler) use ($helpRequest){
57
                    return $handler->canHandle($helpRequest);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Spatie\SlashCommand\HandlesSlashCommand as the method canHandle() does only exist in the following implementations of said interface: Spatie\SlashCommand\Handlers\BaseHandler, Spatie\SlashCommand\Handlers\CatchAll, Spatie\SlashCommand\Handlers\Help, Spatie\SlashCommand\Handlers\SignatureHandler.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
58
                })
59
                ->first();
60
61
            $signature = $this->formatSignature($handler->signature);
0 ignored issues
show
Bug introduced by
The property signature does not seem to exist. Did you mean signatureIsBound?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
62
63
            return $this->respondToSlack("Usage for command */{$this->request->command} {$command}*")
64
                ->withAttachment(Attachment::create()->setText($signature));
65
        } else {
66
            // Create AttachmentFields for each handler
67
            $attachmentFields = collect($handlers)->reduce(function (array $attachmentFields, SignatureHandler $handler) {
68
69
                $signature = $this->formatSignature($handler->signature);
0 ignored issues
show
Bug introduced by
The property signature does not seem to exist. Did you mean signatureIsBound?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
70
                $signatureParts = new SignatureParts($signature);
71
                $attachmentFields[] = AttachmentField::create($signatureParts->getHandlerName(), $signature);
72
73
                return $attachmentFields;
74
            }, []);
75
76
            return $this->respondToSlack("Listing all commands available for */{$this->request->command}*:")
77
                ->withAttachment(Attachment::create()
78
                    ->setFields($attachmentFields)
79
                );
80
        }
81
    }
82
83
    protected function formatSignature($signature)
84
    {
85
        $signatureParts = new SignatureParts($signature);
86
        return '/' . $this->request->command . ' ' . $signatureParts->getSignatureWithoutCommandName();
87
    }
88
}
89