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

Arguments   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 211
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 26
c 3
b 0
f 1
lcom 1
cbo 2
dl 0
loc 211
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 4 1
A addArgument() 0 12 3
A phpUnitArguments() 0 18 4
A toArray() 0 8 2
C parse() 0 35 7
A parseOption() 0 12 2
A optionName() 0 19 4
A isOption() 0 4 1
A isApplicationOption() 0 4 1
A isOptionWithSpaceSeparatedArgument() 0 4 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 = [];
64
65
        foreach ($this->phpUnitOptions as $name => $optionValue) {
66
            if (is_null($optionValue)) {
67
                $arguments[] = $name;
68
                continue;
69
            }
70
            $arguments[] = $name.$optionValue['separator'].$optionValue['value'];
71
        }
72
73
        if (! empty($this->testFile)) {
74
            $arguments[] = $this->testFile;
75
        }
76
77
        return implode(' ', $arguments);
78
    }
79
80
    /**
81
     * Returns an array representation containing application options, PHPUnit options and the test file.
82
     *
83
     * @return array
84
     */
85
    public function toArray()
86
    {
87
        return array_merge(
88
            $this->applicationOptions,
89
            $this->phpUnitOptions,
90
            empty($this->testFile) ? [] : [$this->testFile => null]
91
        );
92
    }
93
94
    /**
95
     * Parses a raw arguments string.
96
     *
97
     * The following types of arguments will be parsed: space separated arguments, key/value and boolean arguments,
98
     * and test file name arguments. Space separated arguments are key/value arguments separated by a space. Boolean
99
     * and key/value arguments are regular options that either take no parameters or take a =-separated parameter.
100
     * Test file name arguments are file names (without option flags).
101
     *
102
     * @param $argumentsInput
103
     * @return $this
104
     */
105
    protected function parse($argumentsInput)
106
    {
107
        $arguments = explode(' ', $argumentsInput);
108
109
        // Keeps track of option name belonging to value when option name and value are space separated
110
        $nextArgumentBelongsTo = false;
111
112
        // PHPUnit only uses first file when multiple are given
113
        $fileSet = false;
114
115
        foreach ($arguments as $argument) {
116
            if ($nextArgumentBelongsTo) {
117
                $this->addArgument($nextArgumentBelongsTo, $argument, ' ');
118
                $nextArgumentBelongsTo = false;
119
                continue;
120
            }
121
122
            if ($this->isOption($argument) && $this->isOptionWithSpaceSeparatedArgument($this->optionName($argument))) {
123
                $nextArgumentBelongsTo = $argument;
124
                continue;
125
            }
126
127
            if ($this->isOption($argument)) {
128
                $this->parseOption($argument);
129
                continue;
130
            }
131
132
            if (! $fileSet) {
133
                $this->testFile = $argument;
134
                $fileSet = true;
135
            }
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * Parses an argument string of an option (either key/value or boolean) and adds it to the list of arguments.
143
     *
144
     * @param  string  $argument
145
     */
146
    protected function parseOption($argument)
147
    {
148
        if (strpos($argument, '=') !== false) {
149
            list($name, $value) = explode('=', $argument);
150
151
            $this->addArgument($name, $value);
152
153
            return;
154
        }
155
156
        $this->addArgument($argument);
157
    }
158
159
    /**
160
     * Converts an argument string into the name of the option.
161
     *
162
     * @param  string  $argument
163
     * @return mixed
164
     */
165
    protected function optionName($argument)
166
    {
167
        // String starts with --
168
        if (substr($argument, 0, 2) == '--') {
169
            $argument = substr($argument, 2);
170
        }
171
172
        // String starts with -
173
        if (substr($argument, 0, 1) == '-') {
174
            $argument = substr($argument, 1);
175
        }
176
177
        // Remove everything after (and including) equals sign
178
        if (strpos($argument, '=') !== false) {
179
            $argument = substr($argument, 0, strpos($argument, '='));
180
        }
181
182
        return $argument;
183
    }
184
185
    /**
186
     * Determines if the given argument string is an option.
187
     *
188
     * @param  string  $argument
189
     * @return bool
190
     */
191
    protected function isOption($argument)
192
    {
193
        return substr($argument, 0, 1) == '-';
194
    }
195
196
    /**
197
     * Determines if the given option name is an application option.
198
     *
199
     * @param  string  $option
200
     * @return bool
201
     */
202
    protected function isApplicationOption($option)
203
    {
204
        return in_array($option, self::APPLICATION_OPTIONS);
205
    }
206
207
    /**
208
     * Determines if the given option name belongs to an option that takes a space separated argument.
209
     *
210
     * @param  string  $option
211
     * @return bool
212
     */
213
    protected function isOptionWithSpaceSeparatedArgument($option)
214
    {
215
        return in_array($option, PhpUnitCommand::optionsWithArguments());
216
    }
217
}
218