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.
Passed
Pull Request — master (#13)
by
unknown
03:23
created

Parser   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 0
dl 0
loc 216
ccs 62
cts 62
cp 1
rs 9.92
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getBoolean() 0 12 4
A parse() 0 11 2
A getArgvFromServer() 0 4 2
A getCoalescingDefault() 0 4 1
A getParamWithEqual() 0 7 1
C handleArguments() 0 42 13
A parseAndMergeCommandWithEqualSign() 0 12 2
A stripSlashes() 0 10 2
A has() 0 4 1
A get() 0 8 2
A getParsedCommands() 0 4 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of the Cop package.
5
 *
6
 * (c) Phalcon Team <[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 Phalcon\Cop;
13
14
/**
15
 * Phalcon\Cop\Parser
16
 *
17
 * @package Phalcon\Cop
18
 */
19
class Parser
20
{
21
    /** @var array */
22
    private $boolParamSet = [
23
        'y'     => true,
24
        'n'     => false,
25
        'yes'   => true,
26
        'no'    => false,
27
        'true'  => true,
28
        'false' => false,
29
        '1'     => true,
30
        '0'     => false,
31
        'on'    => true,
32
        'off'   => false,
33
    ];
34
35
    /** @var array */
36
    private $parsedCommands = [];
37
38
    /**
39
     * Check if parsed parameters has param.
40
     *
41
     * @param  string $key     The parameter's "key"
42
     * @return bool
43
     */
44 33
    public function has($key): bool
45
    {
46 33
        return isset($this->parsedCommands[$key]);
47
    }
48
    
49
    /**
50
     * Get value from parsed parameters.
51
     *
52
     * @param  string $key     The parameter's "key"
53
     * @param  mixed  $default A default value in case the key is not set
54
     * @return mixed
55
     */
56 21
    public function get($key, $default = null)
57
    {
58 21
        if (!$this->has($key)) {
59 14
            return $default;
60
        }
61
62 7
        return $this->parsedCommands[$key];
63
    }
64
    
65
    /**
66
     * Get boolean from parsed parameters.
67
     *
68
     * @param  string $key     The parameter's "key"
69
     * @param  bool   $default A default value in case the key is not set
70
     *
71
     * @return bool
72
     */
73 12
    public function getBoolean(string $key, bool $default = false): bool
74
    {
75 12
        if (!$this->has($key)) {
76 1
            return $default;
77
        }
78
79 11
        if (is_bool($this->parsedCommands[$key]) || is_int($this->parsedCommands[$key])) {
80 1
            return (bool)$this->parsedCommands[$key];
81
        }
82
83 10
        return $this->getCoalescingDefault($this->parsedCommands[$key], $default);
84
    }
85
86
    /**
87
     * Parse console input.
88
     *
89
     * @param  array $argv Arguments to parse. Defaults to empty array
90
     * @return array
91
     */
92 48
    public function parse(array $argv = []): array
93
    {
94 48
        if (empty($argv)) {
95 1
            $argv = $this->getArgvFromServer();
96
        }
97
98 48
        array_shift($argv);
99 48
        $this->parsedCommands = [];
100
101 48
        return $this->handleArguments($argv);
102
    }
103
104
    /**
105
     * Gets array of arguments passed from the input.
106
     *
107
     * @return array
108
     */
109 1
    protected function getArgvFromServer(): array
110
    {
111 1
        return empty($_SERVER['argv']) ? [] : $_SERVER['argv'];
112
    }
113
114
    /**
115
     * Return either received parameter or default
116
     *
117
     * @param string $value   The parameter passed
118
     * @param bool   $default A default value if the parameter is not set
119
     *
120
     * @return bool
121
     */
122 10
    protected function getCoalescingDefault(string $value, bool $default): bool
123
    {
124 10
        return $this->boolParamSet[$value] ?? $default;
125
    }
126
127
    /**
128
     * @param string $arg   The argument passed
129
     * @param int    $eqPos The position of where the equals sign is located
130
     * @return array
131
     */
132 10
    protected function getParamWithEqual(string $arg, int $eqPos): array
133
    {
134 10
        $key       = $this->stripSlashes(substr($arg, 0, $eqPos));
135 10
        $out[$key] = substr($arg, $eqPos +1);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$out was never initialized. Although not strictly required by PHP, it is generally a good practice to add $out = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
136
137 10
        return $out;
138
    }
139
140
    /**
141
     * Handle received parameters
142
     *
143
     * @param array $argv The array with the arguments passed in the CLI
144
     * @return array
145
     */
146 48
    protected function handleArguments(array $argv): array
147
    {
148 48
        for ($i = 0, $j = count($argv); $i < $j; $i++) {
149
            // --foo --bar=baz
150 48
            if (substr($argv[$i], 0, 2) === '--') {
151 27
                if ($this->parseAndMergeCommandWithEqualSign($argv[$i])) {// --bar=baz
152 10
                    continue;
153
                }
154
155 27
                $key = $this->stripSlashes($argv[$i]);
156 27
                if ($i + 1 < $j && $argv[$i + 1][0] !== '-') {// --foo value
157 20
                    $this->parsedCommands[$key] = $argv[$i + 1];
158 20
                    $i++;
159 20
                    continue;
160
                }
161 12
                $this->parsedCommands[$key] = $this->parsedCommands[$key] ?? true; // --foo
162 12
                continue;
163
            }
164
165
            // -k=value -abc
166 26
            if (substr($argv[$i], 0, 1) === '-') {
167 20
                if ($this->parseAndMergeCommandWithEqualSign($argv[$i])) {// -k=value
168 5
                    continue;
169
                }
170
171
                // -a value1 -abc value2 -abc
172 20
                $hasNextElementDash = $i + 1 < $j && $argv[$i + 1][0] !== '-' ? false : true;
173 20
                foreach (str_split(substr($argv[$i], 1)) as $char) {
174 20
                    $this->parsedCommands[$char] = $hasNextElementDash ? true : $argv[$i + 1];
175
                }
176
177 20
                if (!$hasNextElementDash) {// -a value1 -abc value2
178 15
                    $i++;
179
                }
180 20
                continue;
181
            }
182
183 11
            $this->parsedCommands[] = $argv[$i];
184
        }
185
186 48
        return $this->parsedCommands;
187
    }
188
189
    /**
190
     * Parse command `foo=bar`
191
     *
192
     * @param string $command
193
     * @return bool
194
     */
195 42
    protected function parseAndMergeCommandWithEqualSign(string $command): bool
196
    {
197 42
        $eqPos = strpos($command, '=');
198
199 42
        if ($eqPos !== false) {
200 10
            $this->parsedCommands = array_merge($this->parsedCommands, $this->getParamWithEqual($command, $eqPos));
201
202 10
            return true;
203
        }
204
205 42
        return false;
206
    }
207
208
    /**
209
     * Delete dashes from param
210
     *
211
     * @param string $argument
212
     * @return string
213
     */
214 27
    protected function stripSlashes(string $argument): string
215
    {
216 27
        if (substr($argument, 0, 1) !== '-') {
217 27
            return $argument;
218
        }
219
220 27
        $argument = substr($argument, 1);
221
222 27
        return $this->stripSlashes($argument);
223
    }
224
225
    /**
226
     *
227
     * @return array
228
     */
229 8
    public function getParsedCommands(): array
230
    {
231 8
        return $this->parsedCommands;
232
    }
233
234
}
235