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::addArgument()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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