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 ( 08ea9d...7729c5 )
by Freek
02:30
created

SignatureHandler::getDescription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
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\Exceptions\InvalidInput;
9
use Spatie\SlashCommand\Request;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Exception\RuntimeException;
12
use Symfony\Component\Console\Helper\DescriptorHelper;
13
use Symfony\Component\Console\Input\InputDefinition;
14
use Symfony\Component\Console\Input\StringInput;
15
use Symfony\Component\Console\Output\BufferedOutput;
16
17
abstract class SignatureHandler extends BaseHandler
18
{
19
    /** @var string */
20
    protected $name;
21
22
    /** @var string */
23
    protected $signature;
24
25
    /** @var string */
26
    protected $description;
27
28
    /** @var \Symfony\Component\Console\Input\StringInput */
29
    protected $input;
30
31
    /** @var \Symfony\Component\Console\Input\InputDefinition */
32
    protected $inputDefinition;
33
34
    /** @var bool */
35
    protected $signatureIsBound;
36
37
    public function __construct(Request $request)
38
    {
39
        parent::__construct($request);
40
41
        if (empty($this->signature)) {
42
            throw InvalidHandler::signatureIsRequired(static::class);
43
        }
44
45
        $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...
46
    }
47
48
    public function getName(): string
49
    {
50
        return $this->name;
51
    }
52
53
    public function getSignature(): string
54
    {
55
        return $this->signature;
56
    }
57
58
    public function getDescription(): string
59
    {
60
        return $this->description ?: '';
61
    }
62
63
    public function getArgument($foo)
64
    {
65
        return $this->input->getArgument($foo);
66
    }
67
68
    public function getOption($foo)
69
    {
70
        return $this->input->getOption($foo);
71
    }
72
73
    public function getArguments(): array
74
    {
75
        return $this->input->getArguments();
76
    }
77
78
    public function getOptions(): array
79
    {
80
        return $this->input->getOptions();
81
    }
82
83
    /**
84
     * Get the full command (eg. `/bot ping`).
85
     *
86
     * @return string
87
     */
88
    public function getFullCommand(): string
89
    {
90
        return "/{$this->request->command} {$this->name}";
91
    }
92
93
    /**
94
     * Get the usage description, including parameters and options.
95
     *
96
     * @return string
97
     */
98
    public function getHelpDescription(): string
99
    {
100
        $inputDefinition = $this->inputDefinition;
101
        $output = new BufferedOutput();
102
103
        $name = $this->getFullCommand();
104
105
        $command = (new Command($name))
106
            ->setDefinition($inputDefinition)
107
            ->setDescription($this->getDescription());
108
109
        $descriptor = new DescriptorHelper();
110
        $descriptor->describe($output, $command);
111
112
        return $output->fetch();
113
    }
114
115
    public function canHandle(Request $request): bool
116
    {
117
        if (! $this->signatureIsBound) {
118
            return false;
119
        }
120
121
        $signatureParts = new SignatureParts($this->signature);
122
123
124
        if (! Str::is($signatureParts->getSlashCommandName(), $request->command)) {
125
            return false;
126
        }
127
128
        if (explode(' ', $request->text)[0] != $signatureParts->getHandlerName()) {
129
            return false;
130
        }
131
132
        return true;
133
    }
134
135
    protected function bindSignature()
136
    {
137
        $signatureParts = new SignatureParts($this->signature);
138
139
        $signature = $signatureParts->getSignatureWithoutCommandName();
140
141
        list($name, $arguments, $options) = Parser::parse($signature);
142
143
        $this->name = $name;
144
145
        $inputDefinition = new InputDefinition();
146
147
        foreach ($arguments as $argument) {
148
            $inputDefinition->addArgument($argument);
149
        }
150
151
        foreach ($options as $option) {
152
            $inputDefinition->addOption($option);
153
        }
154
155
        $inputWithoutHandlerName = explode(' ', $this->request->text, 2)[1] ?? '';
156
157
        $this->input = new StringInput($inputWithoutHandlerName);
158
        $this->inputDefinition = $inputDefinition;
159
160
        try {
161
            $this->input->bind($inputDefinition);
162
        } catch (RuntimeException $exception) {
163
            return false;
164
        }
165
166
        return true;
167
    }
168
169
    protected function validate()
170
    {
171
        try {
172
            $this->input->validate();
173
        } catch (RuntimeException $e) {
174
            throw new InvalidInput($e->getMessage(), $this, $e);
175
        }
176
    }
177
}
178