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

Arguments::toArray()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 1
nop 0
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
131
132
    protected function parseOption(string $argument)
133
    {
134
        if (strpos($argument, '=') !== false) {
135
            list($name, $value) = explode('=', $argument);
136
137
            $this->addArgument($name, $value);
138
139
            return;
140
        }
141
142
        $this->addArgument($argument);
143
    }
144
145
    protected function optionName(string $argument): string
146
    {
147
        if (substr($argument, 0, 2) == '--') {
148
            $argument = substr($argument, 2);
149
        }
150
151
        if (substr($argument, 0, 1) == '-') {
152
            $argument = substr($argument, 1);
153
        }
154
155
        if (strpos($argument, '=') !== false) {
156
            $argument = substr($argument, 0, strpos($argument, '='));
157
        }
158
159
        return $argument;
160
    }
161
162
    protected function isOption(string $argument): bool
163
    {
164
        return substr($argument, 0, 1) == '-';
165
    }
166
167
    protected function isApplicationOption(string $option): bool
168
    {
169
        return in_array($option, $this->applicationOptions);
170
    }
171
172
    protected function isOptionWithSpaceSeparatedArgument(string $option): bool
173
    {
174
        return in_array($option, PhpUnitCommand::optionsWithArguments());
175
    }
176
}
177