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 (#43)
by Casper
01:23
created

Arguments::parse()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 6
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
        'disable-on-start'
13
    ];
14
15
    protected $testFile;
16
    protected $phpUnitOptions = [];
17
    protected $applicationOptions = [];
18
19
    /**
20
     * Parses the given arguments string and instantiates a new object containing the arguments.
21
     *
22
     * @param  string $argumentsInput
23
     * @return $this
24
     */
25
    public static function fromString($argumentsInput)
26
    {
27
        return (new static)->parse($argumentsInput);
28
    }
29
30
    /**
31
     * Adds an argument if it does not yet exist, otherwise the argument value is replaced.
32
     *
33
     * In case $value is left empty, the argument will be treated as a boolean option. If the
34
     * name and value are not separated by a space, pass the correct separator as a third
35
     * parameter.
36
     *
37
     * Unless an option is registered (in this class) as an application option, we will assume
38
     * the option is meant to be passed through to PHPUnit.
39
     *
40
     * @param  string $name
41
     * @param  mixed $value
42
     * @param  string $separator
43
     */
44
    public function addArgument($name, $value = null, $separator = '=')
45
    {
46
        $valueWithSeparator = is_null($value) ? $value : ['value' => $value, 'separator' => $separator];
47
48
        if ($this->isApplicationOption($this->optionName($name))) {
49
            $this->applicationOptions[$name] = $valueWithSeparator;
50
51
            return;
52
        }
53
54
        $this->phpUnitOptions[$name] = $valueWithSeparator;
55
    }
56
57
    /**
58
     * Returns a string containing all PHPUnit options with corresponding values in the correct format.
59
     *
60
     * @return string
61
     */
62
    public function phpUnitArguments()
63
    {
64
        $arguments = [];
65
66
        foreach ($this->phpUnitOptions as $name => $optionValue) {
67
            if (is_null($optionValue)) {
68
                $arguments[] = $name;
69
                continue;
70
            }
71
            $arguments[] = $name.$optionValue['separator'].$optionValue['value'];
72
        }
73
74
        if (! empty($this->testFile)) {
75
            $arguments[] = $this->testFile;
76
        }
77
78
        return implode(' ', $arguments);
79
    }
80
81
    /**
82
     * Returns an array representation containing application options, PHPUnit options and the test file.
83
     *
84
     * @return array
85
     */
86
    public function toArray()
87
    {
88
        return array_merge(
89
            $this->applicationOptions,
90
            $this->phpUnitOptions,
91
            empty($this->testFile) ? [] : [$this->testFile => null]
92
        );
93
    }
94
95
    /**
96
     * Parses a raw arguments string.
97
     *
98
     * The following types of arguments will be parsed: space separated arguments, key/value and boolean arguments,
99
     * and test file name arguments. Space separated arguments are key/value arguments separated by a space. Boolean
100
     * and key/value arguments are regular options that either take no parameters or take a =-separated parameter.
101
     * Test file name arguments are file names (without option flags).
102
     *
103
     * @param $argumentsInput
104
     * @return $this
105
     */
106
    protected function parse($argumentsInput)
107
    {
108
        $arguments = explode(' ', $argumentsInput);
109
110
        // Keeps track of option name belonging to value when option name and value are space separated
111
        $nextArgumentBelongsTo = false;
112
113
        // PHPUnit only uses first file when multiple are given
114
        $fileSet = false;
115
116
        foreach ($arguments as $argument) {
117
            if ($nextArgumentBelongsTo) {
118
                $this->addArgument($nextArgumentBelongsTo, $argument, ' ');
119
                $nextArgumentBelongsTo = false;
120
                continue;
121
            }
122
123
            if ($this->isOption($argument) && $this->isOptionWithSpaceSeparatedArgument($this->optionName($argument))) {
124
                $nextArgumentBelongsTo = $argument;
125
                continue;
126
            }
127
128
            if ($this->isOption($argument)) {
129
                $this->parseOption($argument);
130
                continue;
131
            }
132
133
            if (! $fileSet) {
134
                $this->testFile = $argument;
135
                $fileSet = true;
136
            }
137
        }
138
139
        return $this;
140
    }
141
142
    /**
143
     * Parses an argument string of an option (either key/value or boolean) and adds it to the list of arguments.
144
     *
145
     * @param  string  $argument
146
     */
147
    protected function parseOption($argument)
148
    {
149
        if (strpos($argument, '=') !== false) {
150
            list($name, $value) = explode('=', $argument);
151
152
            $this->addArgument($name, $value);
153
154
            return;
155
        }
156
157
        $this->addArgument($argument);
158
    }
159
160
    /**
161
     * Converts an argument string into the name of the option.
162
     *
163
     * @param  string  $argument
164
     * @return mixed
165
     */
166
    protected function optionName($argument)
167
    {
168
        // String starts with --
169
        if (substr($argument, 0, 2) == '--') {
170
            $argument = substr($argument, 2);
171
        }
172
173
        // String starts with -
174
        if (substr($argument, 0, 1) == '-') {
175
            $argument = substr($argument, 1);
176
        }
177
178
        // Remove everything after (and including) equals sign
179
        if (strpos($argument, '=') !== false) {
180
            $argument = substr($argument, 0, strpos($argument, '='));
181
        }
182
183
        return $argument;
184
    }
185
186
    /**
187
     * Determines if the given argument string is an option.
188
     *
189
     * @param  string  $argument
190
     * @return bool
191
     */
192
    protected function isOption($argument)
193
    {
194
        return substr($argument, 0, 1) == '-';
195
    }
196
197
    /**
198
     * Determines if the given option name is an application option.
199
     *
200
     * @param  string  $option
201
     * @return bool
202
     */
203
    protected function isApplicationOption($option)
204
    {
205
        return in_array($option, self::APPLICATION_OPTIONS);
206
    }
207
208
    /**
209
     * Determines if the given option name belongs to an option that takes a space separated argument.
210
     *
211
     * @param  string  $option
212
     * @return bool
213
     */
214
    protected function isOptionWithSpaceSeparatedArgument($option)
215
    {
216
        return in_array($option, PhpUnitCommand::optionsWithArguments());
217
    }
218
}
219