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

SignatureHandler::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Spatie\SlashCommand\Handlers;
4
5
use Illuminate\Console\Parser;
6
use Spatie\SlashCommand\Exceptions\InvalidHandler;
7
use Spatie\SlashCommand\Request;
8
use Symfony\Component\Console\Exception\RuntimeException;
9
use Symfony\Component\Console\Input\InputDefinition;
10
use Symfony\Component\Console\Input\StringInput;
11
12
abstract class SignatureHandler extends BaseHandler
13
{
14
    /** @var string */
15
    protected $name;
16
17
    /** @var string */
18
    protected $signature;
19
20
    /** @var string */
21
    protected $description;
22
23
    /** @var \Symfony\Component\Console\Input\StringInput */
24
    protected $input;
25
26
    /** @var \Symfony\Component\Console\Input\InputDefinition */
27
    protected $inputDefinition;
28
29
    /** @var bool */
30
    protected $signatureIsBound;
31
32
    public function __construct(Request $request)
33
    {
34
        parent::__construct($request);
35
36
        if (empty($this->signature)) {
37
            throw InvalidHandler::signatureIsRequired(static::class);
38
        }
39
40
        $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...
41
    }
42
43
    public function getSignature(): string
44
    {
45
        return $this->signature;
46
    }
47
48
    public function getDescription(): string
49
    {
50
        return $this->description ?: '';
51
    }
52
53
    public function getArgument($foo)
54
    {
55
        return $this->input->getArgument($foo);
56
    }
57
58
    public function getOption($foo)
59
    {
60
        return $this->input->getOption($foo);
61
    }
62
63
    public function getArguments(): array
64
    {
65
        return $this->input->getArguments();
66
    }
67
68
    public function getOptions(): array
69
    {
70
        return $this->input->getOptions();
71
    }
72
73
    public function getInputDefinition(): InputDefinition
74
    {
75
        return $this->inputDefinition;
76
    }
77
78
    public function canHandle(Request $request): bool
79
    {
80
        if (! $this->signatureIsBound) {
81
            return false;
82
        }
83
84
        $signatureParts = new SignatureParts($this->signature);
85
86
        if ($request->command != $signatureParts->getSlashCommandName()) {
87
            return false;
88
        }
89
90
        if (explode(' ', $request->text)[0] != $signatureParts->getHandlerName()) {
91
            return false;
92
        }
93
94
        return true;
95
    }
96
97
    protected function bindSignature()
98
    {
99
        $signatureParts = new SignatureParts($this->signature);
100
101
        $signature = $signatureParts->getSignatureWithoutCommandName();
102
103
        list($name, $arguments, $options) = Parser::parse($signature);
104
105
        $this->name = $name;
106
107
        $inputDefinition = new InputDefinition();
108
109
        foreach ($arguments as $argument) {
110
            $inputDefinition->addArgument($argument);
111
        }
112
113
        foreach ($options as $option) {
114
            $inputDefinition->addOption($option);
115
        }
116
117
        $inputWithoutHandlerName = explode(' ', $this->request->text, 2)[1] ?? '';
118
119
        $this->input = new StringInput($inputWithoutHandlerName);
120
        $this->inputDefinition = $inputDefinition;
121
122
        try {
123
            $this->input->bind($inputDefinition);
124
        } catch (RuntimeException $exception) {
125
            return false;
126
        }
127
128
        return true;
129
    }
130
}
131