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.
38:51 queued 38:29
created

Help::handle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 47
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 9.0303
c 0
b 0
f 0
cc 3
eloc 26
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
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Helper\DescriptorHelper;
13
use Symfony\Component\Console\Output\BufferedOutput;
14
15
class Help extends SignatureHandler
16
{
17
    protected $signature = '* help {command? : The command you want information about}';
18
19
    protected $description = 'List all commands or provide information about all commands';
20
21
    /**
22
     * Handle the given request.
23
     *
24
     * @param \Spatie\SlashCommand\Request $request
25
     *
26
     * @return \Spatie\SlashCommand\Response
27
     */
28
    public function handle(Request $request): Response
29
    {
30
        $command = $this->getArgument('command');
31
32
        $helpRequest = clone $this->request;
33
        $helpRequest->text = $command;
34
35
        $handlers = collect(config('laravel-slack-slash-command.handlers'))
36
            ->map(function (string $handlerClassName) use($helpRequest) {
37
                return new $handlerClassName($helpRequest);
38
            })
39
            ->filter(function (HandlesSlashCommand $handler) use ($helpRequest){
40
                if ($handler instanceof SignatureHandler) {
41
                    $signatureParts = new SignatureParts($handler->getSignature());
42
                    return in_array($signatureParts->getSlashCommandName(), [$this->request->command, '*']);
43
                }
44
            });
45
46
        // When command is passed, find all commands
47
        if (! empty($command)) {
48
49
            /** @var SignatureHandler $handler */
50
            $handler = $handlers
51
                ->filter(function (HandlesSlashCommand $handler) use ($helpRequest){
52
                    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...
53
                })
54
                ->first();
55
56
            return $this->respondToSlack('')
57
                ->withAttachment(Attachment::create()
58
                    ->addField($this->getAttachmentFieldForHandler($handler))
59
                );
60
        } else {
61
            // Create AttachmentFields for each handler
62
            $attachmentFields = collect($handlers)->reduce(function (array $attachmentFields, SignatureHandler $handler) {
63
64
                $attachmentFields[] = AttachmentField::create($this->getFullCommand($handler), $handler->getDescription());
65
66
                return $attachmentFields;
67
            }, []);
68
69
            return $this->respondToSlack("Available commands:")
70
                ->withAttachment(Attachment::create()
71
                    ->setFields($attachmentFields)
72
                );
73
        }
74
    }
75
76
    protected function getFullCommand(SignatureHandler $handler): string
77
    {
78
        $signatureParts = new SignatureParts($handler->signature);
79
80
        return '/' . $this->request->command . ' ' . $signatureParts->getHandlerName();
81
    }
82
83
    protected function getAttachmentFieldForHandler(SignatureHandler $handler): AttachmentField
84
    {
85
        $fullCommand = $this->getFullCommand($handler);
86
87
        $inputDefinition = $handler->getInputDefinition();
88
        $output = new BufferedOutput();
89
90
        $command = (new Command($fullCommand))
91
            ->setDefinition($inputDefinition)
92
            ->setDescription($handler->getDescription())
93
        ;
94
95
        $descriptor = new DescriptorHelper();
96
        $descriptor->describe($output, $command);
97
98
        return AttachmentField::create($fullCommand, $output->fetch());
99
    }
100
}
101