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.
04:05 queued 01:36
created

SignatureHandler::canHandle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
3
namespace Spatie\SlashCommand\Handlers;
4
5
use Illuminate\Console\Parser;
6
use Illuminate\Support\Str;
7
use Spatie\SlashCommand\Exceptions\InvalidHandler;
8
use Spatie\SlashCommand\Request;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Exception\RuntimeException;
11
use Symfony\Component\Console\Helper\DescriptorHelper;
12
use Symfony\Component\Console\Input\InputDefinition;
13
use Symfony\Component\Console\Input\StringInput;
14
use Symfony\Component\Console\Output\BufferedOutput;
15
16
abstract class SignatureHandler extends BaseHandler
17
{
18
    /** @var string */
19
    protected $name;
20
21
    /** @var string */
22
    protected $signature;
23
24
    /** @var string */
25
    protected $description;
26
27
    /** @var \Symfony\Component\Console\Input\StringInput */
28
    protected $input;
29
30
    /** @var \Symfony\Component\Console\Input\InputDefinition */
31
    protected $inputDefinition;
32
33
    /** @var bool */
34
    protected $signatureIsBound;
35
36
    public function __construct(Request $request)
37
    {
38
        parent::__construct($request);
39
40
        if (empty($this->signature)) {
41
            throw InvalidHandler::signatureIsRequired(static::class);
42
        }
43
44
        $this->signatureIsBound = $this->bindSignature($this->signature);
0 ignored issues
show
Unused Code introduced by
The call to SignatureHandler::bindSignature() has too many arguments starting with $this->signature.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
45
    }
46
47
    public function getSignature(): string
48
    {
49
        return $this->signature;
50
    }
51
52
    public function getDescription(): string
53
    {
54
        return $this->description ?: '';
55
    }
56
57
    public function getArgument($foo)
58
    {
59
        return $this->input->getArgument($foo);
60
    }
61
62
    public function getOption($foo)
63
    {
64
        return $this->input->getOption($foo);
65
    }
66
67
    public function getArguments(): array
68
    {
69
        return $this->input->getArguments();
70
    }
71
72
    public function getOptions(): array
73
    {
74
        return $this->input->getOptions();
75
    }
76
77
    /**
78
     * Get the full command (eg. `/bot ping`)
79
     *
80
     * @return string
81
     */
82
    public function getFullCommand(): string
83
    {
84
        return '/' . $this->request->command . ' ' . $this->name;
85
    }
86
87
    /**
88
     * Get the usage description, including parameters and options
89
     *
90
     * @return string
91
     */
92
    public function getHelpDescription(): string
93
    {
94
        $inputDefinition = $this->inputDefinition;
95
        $output = new BufferedOutput();
96
97
        $name = '/' . $this->request->command . ' ' . $this->name;
98
99
        $command = (new Command($name))
100
            ->setDefinition($inputDefinition)
101
            ->setDescription($this->getDescription())
102
        ;
103
104
        $descriptor = new DescriptorHelper();
105
        $descriptor->describe($output, $command);
106
107
        return $output->fetch();
108
    }
109
110
    public function canHandle(Request $request): bool
111
    {
112
        if (! $this->signatureIsBound) {
113
            return false;
114
        }
115
116
        $signatureParts = new SignatureParts($this->signature);
117
118
119
        if (! Str::is($signatureParts->getSlashCommandName(), $request->command)) {
120
            return false;
121
        }
122
123
        if (explode(' ', $request->text)[0] != $signatureParts->getHandlerName()) {
124
            return false;
125
        }
126
127
        return true;
128
    }
129
130
    protected function bindSignature()
131
    {
132
        $signatureParts = new SignatureParts($this->signature);
133
134
        $signature = $signatureParts->getSignatureWithoutCommandName();
135
136
        list($name, $arguments, $options) = Parser::parse($signature);
137
138
        $this->name = $name;
139
140
        $inputDefinition = new InputDefinition();
141
142
        foreach ($arguments as $argument) {
143
            $inputDefinition->addArgument($argument);
144
        }
145
146
        foreach ($options as $option) {
147
            $inputDefinition->addOption($option);
148
        }
149
150
        $inputWithoutHandlerName = explode(' ', $this->request->text, 2)[1] ?? '';
151
152
        $this->input = new StringInput($inputWithoutHandlerName);
153
        $this->inputDefinition = $inputDefinition;
154
155
        try {
156
            $this->input->bind($inputDefinition);
157
        } catch (RuntimeException $exception) {
158
            return false;
159
        }
160
161
        return true;
162
    }
163
}
164