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:40
created

Arguments::optionName()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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