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 (#41)
by Freek
01:35
created

Arguments::optionName()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 8
nc 8
nop 1
1
<?php
2
3
namespace Spatie\PhpUnitWatcher;
4
5
use Spatie\PhpUnitWatcher\PhpUnit\Command as PhpUnitCommand;
6
7
class Arguments
8
{
9
    use ArgumentAccessors;
10
11
    const APPLICATION_OPTIONS = [];
12
13
    /** @var string  */
14
    protected $testFile;
15
    protected $phpUnitOptions = [];
16
    protected $applicationOptions = [];
17
18
    public static function fromString(string $argumentsInput)
19
    {
20
        return (new static)->parse($argumentsInput);
21
    }
22
23
    /**
24
     * Parses a raw arguments string.
25
     *
26
     * The following types of arguments will be parsed: space separated arguments, key/value and boolean arguments,
27
     * and test file name arguments. Space separated arguments are key/value arguments separated by a space. Boolean
28
     * and key/value arguments are regular options that either take no parameters or take a =-separated parameter.
29
     * Test file name arguments are file names (without option flags).
30
     *
31
     * @param $argumentsInput
32
     * @return $this
33
     */
34
    protected function parse(string $argumentsInput)
35
    {
36
        $arguments = explode(' ', $argumentsInput);
37
38
        // Keeps track of option name belonging to value when option name and value are space separated
39
        $nextArgumentBelongsTo = false;
40
41
        // PHPUnit only uses first file when multiple are given
42
        $fileSet = false;
43
44
        foreach ($arguments as $argument) {
45
            if ($nextArgumentBelongsTo) {
46
                $this->addArgument($nextArgumentBelongsTo, $argument, ' ');
47
                $nextArgumentBelongsTo = false;
48
                continue;
49
            }
50
51
            if ($this->isOption($argument) && $this->isOptionWithSpaceSeparatedArgument($this->optionName($argument))) {
52
                $nextArgumentBelongsTo = $argument;
53
                continue;
54
            }
55
56
            if ($this->isOption($argument)) {
57
                $this->parseOption($argument);
58
                continue;
59
            }
60
61
            if (! $fileSet) {
62
                $this->testFile = $argument;
63
                $fileSet = true;
64
            }
65
        }
66
67
        return $this;
68
    }
69
70
    /*
71
     * Adds an argument if it does not yet exist, otherwise the argument value is replaced.
72
     *
73
     * In case $value is left empty, the argument will be treated as a boolean option. If the
74
     * name and value are not separated by a space, pass the correct separator as a third
75
     * parameter.
76
     *
77
     * Unless an option is registered (in this class) as an application option, we will assume
78
     * the option is meant to be passed through to PHPUnit.
79
     */
80
    public function addArgument(string $name, string $value = null, string $separator = '=')
81
    {
82
        $valueWithSeparator = is_null($value) ? $value : ['value' => $value, 'separator' => $separator];
83
84
        if ($this->isApplicationOption($this->optionName($name))) {
85
            $this->applicationOptions[$name] = $valueWithSeparator;
86
87
            return;
88
        }
89
90
        $this->phpUnitOptions[$name] = $valueWithSeparator;
91
    }
92
93
    public function phpUnitArguments(): string
94
    {
95
        $arguments = array_map(function($name, $optionValue) {
96
            return is_null($optionValue)
97
                ? $name
98
                : $name.$optionValue['separator'].$optionValue['value'];
99
        }, array_keys($this->phpUnitOptions), $this->phpUnitOptions);
100
101
102
        if (! empty($this->testFile)) {
103
            $arguments[] = $this->testFile;
104
        }
105
106
        return implode(' ', $arguments);
107
    }
108
109
    public function toArray(): array
110
    {
111
        return array_merge(
112
            $this->applicationOptions,
113
            $this->phpUnitOptions,
114
            empty($this->testFile)
115
                ? []
116
                : [$this->testFile => null]
117
        );
118
    }
119
120
121
122
    protected function parseOption(string $argument)
123
    {
124
        if (strpos($argument, '=') !== false) {
125
            list($name, $value) = explode('=', $argument);
126
127
            $this->addArgument($name, $value);
128
129
            return;
130
        }
131
132
        $this->addArgument($argument);
133
    }
134
135
    protected function optionName(string $argument): string
136
    {
137
        if (substr($argument, 0, 2) == '--') {
138
            $argument = substr($argument, 2);
139
        }
140
141
        if (substr($argument, 0, 1) == '-') {
142
            $argument = substr($argument, 1);
143
        }
144
145
        if (strpos($argument, '=') !== false) {
146
            $argument = substr($argument, 0, strpos($argument, '='));
147
        }
148
149
        return $argument;
150
    }
151
152
    protected function isOption(string $argument): bool
153
    {
154
        return substr($argument, 0, 1) == '-';
155
    }
156
157
    protected function isApplicationOption(string $option): bool
158
    {
159
        return in_array($option, self::APPLICATION_OPTIONS);
160
    }
161
162
    protected function isOptionWithSpaceSeparatedArgument(string $option): bool
163
    {
164
        return in_array($option, PhpUnitCommand::optionsWithArguments());
165
    }
166
}
167