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.

ArgvInput::addLongOption()   C
last analyzed

Complexity

Conditions 16
Paths 75

Size

Total Lines 46
Code Lines 25

Duplication

Lines 9
Ratio 19.57 %

Importance

Changes 0
Metric Value
cc 16
eloc 25
nc 75
nop 2
dl 9
loc 46
rs 5.0026
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symfony\Component\Console\Input;
13
14
/**
15
 * ArgvInput represents an input coming from the CLI arguments.
16
 *
17
 * Usage:
18
 *
19
 *     $input = new ArgvInput();
20
 *
21
 * By default, the `$_SERVER['argv']` array is used for the input values.
22
 *
23
 * This can be overridden by explicitly passing the input values in the constructor:
24
 *
25
 *     $input = new ArgvInput($_SERVER['argv']);
26
 *
27
 * If you pass it yourself, don't forget that the first element of the array
28
 * is the name of the running application.
29
 *
30
 * When passing an argument to the constructor, be sure that it respects
31
 * the same rules as the argv one. It's almost always better to use the
32
 * `StringInput` when you want to provide your own input.
33
 *
34
 * @author Fabien Potencier <[email protected]>
35
 *
36
 * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
37
 * @see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02
38
 *
39
 * @api
40
 */
41
class ArgvInput extends Input
42
{
43
    private $tokens;
44
    private $parsed;
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param array           $argv       An array of parameters from the CLI (in the argv format)
50
     * @param InputDefinition $definition A InputDefinition instance
51
     *
52
     * @api
53
     */
54
    public function __construct(array $argv = null, InputDefinition $definition = null)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
55
    {
56
        if (null === $argv) {
57
            $argv = $_SERVER['argv'];
58
        }
59
60
        // strip the application name
61
        array_shift($argv);
62
63
        $this->tokens = $argv;
64
65
        parent::__construct($definition);
66
    }
67
68
    protected function setTokens(array $tokens)
69
    {
70
        $this->tokens = $tokens;
71
    }
72
73
    /**
74
     * Processes command line arguments.
75
     */
76
    protected function parse()
77
    {
78
        $parseOptions = true;
79
        $this->parsed = $this->tokens;
80
        while (null !== $token = array_shift($this->parsed)) {
81
            if ($parseOptions && '' == $token) {
82
                $this->parseArgument($token);
83
            } elseif ($parseOptions && '--' == $token) {
84
                $parseOptions = false;
85
            } elseif ($parseOptions && 0 === strpos($token, '--')) {
86
                $this->parseLongOption($token);
87
            } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) {
88
                $this->parseShortOption($token);
89
            } else {
90
                $this->parseArgument($token);
91
            }
92
        }
93
    }
94
95
    /**
96
     * Parses a short option.
97
     *
98
     * @param string $token The current token.
99
     */
100
    private function parseShortOption($token)
101
    {
102
        $name = substr($token, 1);
103
104
        if (strlen($name) > 1) {
105
            if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
106
                // an option with a value (with no space)
107
                $this->addShortOption($name[0], substr($name, 1));
108
            } else {
109
                $this->parseShortOptionSet($name);
110
            }
111
        } else {
112
            $this->addShortOption($name, null);
113
        }
114
    }
115
116
    /**
117
     * Parses a short option set.
118
     *
119
     * @param string $name The current token
120
     *
121
     * @throws \RuntimeException When option given doesn't exist
122
     */
123
    private function parseShortOptionSet($name)
124
    {
125
        $len = strlen($name);
126
        for ($i = 0; $i < $len; $i++) {
127
            if (!$this->definition->hasShortcut($name[$i])) {
128
                throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
129
            }
130
131
            $option = $this->definition->getOptionForShortcut($name[$i]);
132
            if ($option->acceptValue()) {
133
                $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
134
135
                break;
136
            } else {
137
                $this->addLongOption($option->getName(), null);
138
            }
139
        }
140
    }
141
142
    /**
143
     * Parses a long option.
144
     *
145
     * @param string $token The current token
146
     */
147
    private function parseLongOption($token)
148
    {
149
        $name = substr($token, 2);
150
151
        if (false !== $pos = strpos($name, '=')) {
152
            $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
153
        } else {
154
            $this->addLongOption($name, null);
155
        }
156
    }
157
158
    /**
159
     * Parses an argument.
160
     *
161
     * @param string $token The current token
162
     *
163
     * @throws \RuntimeException When too many arguments are given
164
     */
165
    private function parseArgument($token)
166
    {
167
        $c = count($this->arguments);
168
169
        // if input is expecting another argument, add it
170
        if ($this->definition->hasArgument($c)) {
171
            $arg = $this->definition->getArgument($c);
172
            $this->arguments[$arg->getName()] = $arg->isArray() ? array($token) : $token;
173
174
        // if last argument isArray(), append token to last argument
175
        } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
176
            $arg = $this->definition->getArgument($c - 1);
177
            $this->arguments[$arg->getName()][] = $token;
178
179
        // unexpected argument
180
        } else {
181
            throw new \RuntimeException('Too many arguments.');
182
        }
183
    }
184
185
    /**
186
     * Adds a short option value.
187
     *
188
     * @param string $shortcut The short option key
189
     * @param mixed  $value    The value for the option
190
     *
191
     * @throws \RuntimeException When option given doesn't exist
192
     */
193 View Code Duplication
    private function addShortOption($shortcut, $value)
194
    {
195
        if (!$this->definition->hasShortcut($shortcut)) {
196
            throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
197
        }
198
199
        $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
200
    }
201
202
    /**
203
     * Adds a long option value.
204
     *
205
     * @param string $name  The long option key
206
     * @param mixed  $value The value for the option
207
     *
208
     * @throws \RuntimeException When option given doesn't exist
209
     */
210
    private function addLongOption($name, $value)
211
    {
212
        if (!$this->definition->hasOption($name)) {
213
            throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
214
        }
215
216
        $option = $this->definition->getOption($name);
217
218
        // Convert false values (from a previous call to substr()) to null
219
        if (false === $value) {
220
            $value = null;
221
        }
222
223
        if (null !== $value && !$option->acceptValue()) {
224
            throw new \RuntimeException(sprintf('The "--%s" option does not accept a value.', $name, $value));
225
        }
226
227
        if (null === $value && $option->acceptValue() && count($this->parsed)) {
228
            // if option accepts an optional or mandatory argument
229
            // let's see if there is one provided
230
            $next = array_shift($this->parsed);
231
            if (isset($next[0]) && '-' !== $next[0]) {
232
                $value = $next;
233
            } elseif (empty($next)) {
234
                $value = '';
235
            } else {
236
                array_unshift($this->parsed, $next);
237
            }
238
        }
239
240 View Code Duplication
        if (null === $value) {
241
            if ($option->isValueRequired()) {
242
                throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
243
            }
244
245
            if (!$option->isArray()) {
246
                $value = $option->isValueOptional() ? $option->getDefault() : true;
247
            }
248
        }
249
250
        if ($option->isArray()) {
251
            $this->options[$name][] = $value;
252
        } else {
253
            $this->options[$name] = $value;
254
        }
255
    }
256
257
    /**
258
     * Returns the first argument from the raw parameters (not parsed).
259
     *
260
     * @return string The value of the first argument or null otherwise
261
     */
262
    public function getFirstArgument()
263
    {
264
        foreach ($this->tokens as $token) {
265
            if ($token && '-' === $token[0]) {
266
                continue;
267
            }
268
269
            return $token;
270
        }
271
    }
272
273
    /**
274
     * Returns true if the raw parameters (not parsed) contain a value.
275
     *
276
     * This method is to be used to introspect the input parameters
277
     * before they have been validated. It must be used carefully.
278
     *
279
     * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
280
     *
281
     * @return bool true if the value is contained in the raw parameters
282
     */
283
    public function hasParameterOption($values)
284
    {
285
        $values = (array) $values;
286
287
        foreach ($this->tokens as $token) {
288
            foreach ($values as $value) {
289
                if ($token === $value || 0 === strpos($token, $value.'=')) {
290
                    return true;
291
                }
292
            }
293
        }
294
295
        return false;
296
    }
297
298
    /**
299
     * Returns the value of a raw option (not parsed).
300
     *
301
     * This method is to be used to introspect the input parameters
302
     * before they have been validated. It must be used carefully.
303
     *
304
     * @param string|array $values  The value(s) to look for in the raw parameters (can be an array)
305
     * @param mixed        $default The default value to return if no result is found
306
     *
307
     * @return mixed The option value
308
     */
309
    public function getParameterOption($values, $default = false)
310
    {
311
        $values = (array) $values;
312
        $tokens = $this->tokens;
313
314
        while (0 < count($tokens)) {
315
            $token = array_shift($tokens);
316
317
            foreach ($values as $value) {
318
                if ($token === $value || 0 === strpos($token, $value.'=')) {
319
                    if (false !== $pos = strpos($token, '=')) {
320
                        return substr($token, $pos + 1);
321
                    }
322
323
                    return array_shift($tokens);
324
                }
325
            }
326
        }
327
328
        return $default;
329
    }
330
331
    /**
332
     * Returns a stringified representation of the args passed to the command.
333
     *
334
     * @return string
335
     */
336
    public function __toString()
337
    {
338
        $self = $this;
339
        $tokens = array_map(function ($token) use ($self) {
340
            if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
341
                return $match[1].$self->escapeToken($match[2]);
342
            }
343
344
            if ($token && $token[0] !== '-') {
345
                return $self->escapeToken($token);
346
            }
347
348
            return $token;
349
        }, $this->tokens);
350
351
        return implode(' ', $tokens);
352
    }
353
}
354