CommandParser::parseArgument()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 29
Code Lines 17

Duplication

Lines 7
Ratio 24.14 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 7
loc 29
rs 8.439
cc 6
eloc 17
nc 10
nop 1
1
<?php
2
3
use Symfony\Component\Console\Input\InputArgument;
4
use Symfony\Component\Console\Input\InputOption;
5
6
/**
7
 * Class Parser.
8
 *
9
 * Shameless copy/paste from Taylor Otwell's Laravel
10
 */
11
class CommandParser
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    /**
14
     * Parse the given console command definition into an array.
15
     *
16
     * @param string $expression
17
     *
18
     * @throws \InvalidArgumentException
19
     *
20
     * @return array
21
     */
22
    public static function parse($expression)
23
    {
24
        if (trim($expression) === '') {
25
            throw new InvalidArgumentException('Console command definition is empty.');
26
        }
27
28
        preg_match('/[^\s]+/', $expression, $matches);
29
30
        if (isset($matches[0])) {
31
            $name = $matches[0];
32
        } else {
33
            throw new InvalidArgumentException('Unable to determine command name from signature.');
34
        }
35
36
        preg_match_all('/\{\s*(.*?)\s*\}/', $expression, $matches);
37
38
        $tokens = isset($matches[1]) ? $matches[1] : [];
39
40
        if (count($tokens)) {
41
            return array_merge([$name], static::parameters($tokens));
42
        }
43
44
        return [$name, [], []];
45
    }
46
47
    /**
48
     * Extract all of the parameters from the tokens.
49
     *
50
     * @param array $tokens
51
     *
52
     * @return array
53
     */
54
    protected static function parameters(array $tokens)
55
    {
56
        $arguments = [];
57
58
        $options = [];
59
60
        foreach ($tokens as $token) {
61
            if (!Str::startsWith($token, '--')) {
62
                $arguments[] = static::parseArgument($token);
63
            } else {
64
                $options[] = static::parseOption(ltrim($token, '-'));
65
            }
66
        }
67
68
        return [$arguments, $options];
69
    }
70
71
    /**
72
     * Parse an argument expression.
73
     *
74
     * @param string $token
75
     *
76
     * @return \Symfony\Component\Console\Input\InputArgument
77
     */
78
    protected static function parseArgument($token)
79
    {
80
        $description = null;
81
82 View Code Duplication
        if (Str::contains($token, ' : ')) {
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...
83
            list($token, $description) = explode(' : ', $token, 2);
84
85
            $token = trim($token);
86
87
            $description = trim($description);
88
        }
89
90
        switch (true) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match('/(.+)\\=(.+)/', $token, $matches) of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
91
            case Str::endsWith($token, '?*'):
92
                return new InputArgument(trim($token, '?*'), InputArgument::IS_ARRAY, $description);
93
94
            case Str::endsWith($token, '*'):
95
                return new InputArgument(trim($token, '*'), InputArgument::IS_ARRAY | InputArgument::REQUIRED, $description);
96
97
            case Str::endsWith($token, '?'):
98
                return new InputArgument(trim($token, '?'), InputArgument::OPTIONAL, $description);
99
100
            case preg_match('/(.+)\=(.+)/', $token, $matches):
101
                return new InputArgument($matches[1], InputArgument::OPTIONAL, $description, $matches[2]);
0 ignored issues
show
Bug introduced by
The variable $matches seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
102
103
            default:
104
                return new InputArgument($token, InputArgument::REQUIRED, $description);
105
        }
106
    }
107
108
    /**
109
     * Parse an option expression.
110
     *
111
     * @param string $token
112
     *
113
     * @return \Symfony\Component\Console\Input\InputOption
114
     */
115
    protected static function parseOption($token)
116
    {
117
        $description = null;
118
119 View Code Duplication
        if (Str::contains($token, ' : ')) {
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...
120
            list($token, $description) = explode(' : ', $token);
121
            $token = trim($token);
122
            $description = trim($description);
123
        }
124
125
        $shortcut = null;
126
127
        $matches = preg_split('/\s*\|\s*/', $token, 2);
128
129
        if (isset($matches[1])) {
130
            $shortcut = $matches[0];
131
            $token = $matches[1];
132
        }
133
134
        switch (true) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match('/(.+)\\=(.+)/', $token, $matches) of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
135 View Code Duplication
            case Str::endsWith($token, '='):
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...
136
                return new InputOption(trim($token, '='), $shortcut, InputOption::VALUE_OPTIONAL, $description);
137
138 View Code Duplication
            case Str::endsWith($token, '=*'):
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...
139
                return new InputOption(trim($token, '=*'), $shortcut, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $description);
140
141
            case preg_match('/(.+)\=(.+)/', $token, $matches):
142
                return new InputOption($matches[1], $shortcut, InputOption::VALUE_OPTIONAL, $description, $matches[2]);
143
144
            default:
145
                return new InputOption($token, $shortcut, InputOption::VALUE_NONE, $description);
146
        }
147
    }
148
}
149