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
Push — master ( 65058f...8a6e2f )
by François
02:41
created

CliParser::parse()   C

Complexity

Conditions 14
Paths 68

Size

Total Lines 51
Code Lines 25

Duplication

Lines 12
Ratio 23.53 %

Importance

Changes 0
Metric Value
cc 14
eloc 25
nc 68
nop 1
dl 12
loc 51
rs 5.6426
c 0
b 0
f 0

How to fix   Long Method    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
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace SURFnet\VPN\Server;
19
20
use SURFnet\VPN\Common\Config;
21
use SURFnet\VPN\Server\Exception\CliException;
22
23
class CliParser
24
{
25
    /** @var string */
26
    private $programDescription;
27
28
    /** @var array */
29
    private $optionList;
30
31
    public function __construct($programDescription, array $optionList)
32
    {
33
        $this->programDescription = $programDescription;
34
        $this->optionList = $optionList;
35
    }
36
37
    public function help()
38
    {
39
        $helpText = $this->programDescription.PHP_EOL;
40
        $helpText .= 'Options:'.PHP_EOL;
41
        foreach ($this->optionList as $k => $v) {
42
            if ($v[1]) {
43
                $helpText .= sprintf("  --%s %s\t\t%s", $k, $k, $v[0]);
44
            } else {
45
                $helpText .= sprintf("  --%s\t\t%s", $k, $v[0]);
46
            }
47
            if ($v[2]) {
48
                $helpText .= ' (REQUIRED)';
49
            }
50
            $helpText .= PHP_EOL;
51
        }
52
53
        return $helpText;
54
    }
55
56
    public function parse(array $argv)
57
    {
58
        $argc = count($argv);
59
        $optionValues = [];
60
61
        for ($i = 1; $i < $argc; ++$i) {
62
            if (0 === strpos($argv[$i], '--')) {
63
                // it is an option selector
64
                $p = substr($argv[$i], 2);  // strip the dashes
65
                $pO = [];
66
                while ($i + 1 < $argc && false === strpos($argv[$i + 1], '--')) {
67
                    $pO[] = $argv[++$i];
68
                }
69
                if (1 === count($pO)) {
70
                    $optionValues[$p] = $pO[0];
71
                } else {
72
                    $optionValues[$p] = $pO;
73
                }
74
            }
75
        }
76
77
        // --help is special
78
        if (array_key_exists('help', $optionValues)) {
79
            return new Config(['help' => true]);
80
        }
81
82
        // check if any of the required keys is missing
83
        foreach (array_keys($this->optionList) as $opt) {
84 View Code Duplication
            if ($this->optionList[$opt][2]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
                // required
86
                if (!array_key_exists($opt, $optionValues)) {
87
                    throw new CliException(sprintf('missing required parameter "--%s"', $opt));
88
                }
89
            }
90
        }
91
92
        // check if any of the options that require a value has no value
93
        foreach (array_keys($this->optionList) as $opt) {
94
            if ($this->optionList[$opt][1]) {
95
                // check if it is actually there
96 View Code Duplication
                if (array_key_exists($opt, $optionValues)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
                    // must have value
98
                    if (0 === count($optionValues[$opt])) {
99
                        throw new CliException(sprintf('missing required parameter value for option "--%s"', $opt));
100
                    }
101
                }
102
            }
103
        }
104
105
        return new Config($optionValues);
106
    }
107
}
108