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

Arguments::setFilterArgument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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