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

Arguments::fromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
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
    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
    public function addArgument(string $name, string $value = null, string $separator = '=')
40
    {
41
        $valueWithSeparator = is_null($value) ? $value : ['value' => $value, 'separator' => $separator];
42
43
        if ($this->isApplicationOption($this->optionName($name))) {
44
            $this->applicationOptions[$name] = $valueWithSeparator;
45
46
            return;
47
        }
48
49
        $this->phpUnitOptions[$name] = $valueWithSeparator;
50
    }
51
52
    public function phpUnitArguments(): string
53
    {
54
        $arguments = array_map(function($name, $optionValue) {
55
            return is_null($optionValue)
56
                ? $name
57
                : $name.$optionValue['separator'].$optionValue['value'];
58
        }, array_keys($this->phpUnitOptions), $this->phpUnitOptions);
59
60
61
        if (! empty($this->testFile)) {
62
            $arguments[] = $this->testFile;
63
        }
64
65
        return implode(' ', $arguments);
66
    }
67
68
    public function toArray(): array
69
    {
70
        return array_merge(
71
            $this->applicationOptions,
72
            $this->phpUnitOptions,
73
            empty($this->testFile)
74
                ? []
75
                : [$this->testFile => null]
76
        );
77
    }
78
79
    /**
80
     * Parses a raw arguments string.
81
     *
82
     * The following types of arguments will be parsed: space separated arguments, key/value and boolean arguments,
83
     * and test file name arguments. Space separated arguments are key/value arguments separated by a space. Boolean
84
     * and key/value arguments are regular options that either take no parameters or take a =-separated parameter.
85
     * Test file name arguments are file names (without option flags).
86
     *
87
     * @param $argumentsInput
88
     * @return $this
89
     */
90
    protected function parse(string $argumentsInput)
91
    {
92
        $arguments = explode(' ', $argumentsInput);
93
94
        // Keeps track of option name belonging to value when option name and value are space separated
95
        $nextArgumentBelongsTo = false;
96
97
        // PHPUnit only uses first file when multiple are given
98
        $fileSet = false;
99
100
        foreach ($arguments as $argument) {
101
            if ($nextArgumentBelongsTo) {
102
                $this->addArgument($nextArgumentBelongsTo, $argument, ' ');
103
                $nextArgumentBelongsTo = false;
104
                continue;
105
            }
106
107
            if ($this->isOption($argument) && $this->isOptionWithSpaceSeparatedArgument($this->optionName($argument))) {
108
                $nextArgumentBelongsTo = $argument;
109
                continue;
110
            }
111
112
            if ($this->isOption($argument)) {
113
                $this->parseOption($argument);
114
                continue;
115
            }
116
117
            if (! $fileSet) {
118
                $this->testFile = $argument;
119
                $fileSet = true;
120
            }
121
        }
122
123
        return $this;
124
    }
125
126
    protected function parseOption(string $argument)
127
    {
128
        if (strpos($argument, '=') !== false) {
129
            list($name, $value) = explode('=', $argument);
130
131
            $this->addArgument($name, $value);
132
133
            return;
134
        }
135
136
        $this->addArgument($argument);
137
    }
138
139
    protected function optionName(string $argument): string
140
    {
141
        if (substr($argument, 0, 2) == '--') {
142
            $argument = substr($argument, 2);
143
        }
144
145
        if (substr($argument, 0, 1) == '-') {
146
            $argument = substr($argument, 1);
147
        }
148
149
        if (strpos($argument, '=') !== false) {
150
            $argument = substr($argument, 0, strpos($argument, '='));
151
        }
152
153
        return $argument;
154
    }
155
156
    protected function isOption(string $argument): bool
157
    {
158
        return substr($argument, 0, 1) == '-';
159
    }
160
161
    protected function isApplicationOption(string $option): bool
162
    {
163
        return in_array($option, self::APPLICATION_OPTIONS);
164
    }
165
166
    protected function isOptionWithSpaceSeparatedArgument(string $option): bool
167
    {
168
        return in_array($option, PhpUnitCommand::optionsWithArguments());
169
    }
170
}
171