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 ( c5edcc...4235fd )
by Freek
02:23
created

SignatureHandler::getHelpDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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