Completed
Push — master ( 86b08e...e9a96e )
by Dmitry
12:37
created

Request::resolve()   C

Complexity

Conditions 13
Paths 33

Size

Total Lines 44
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 13.0042

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 33
cts 34
cp 0.9706
rs 5.1234
c 0
b 0
f 0
cc 13
eloc 31
nc 33
nop 0
crap 13.0042

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
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\console;
9
10
/**
11
 * The console Request represents the environment information for a console application.
12
 *
13
 * It is a wrapper for the PHP `$_SERVER` variable which holds information about the
14
 * currently running PHP script and the command line arguments given to it.
15
 *
16
 * @property array $params The command line arguments. It does not include the entry script name.
17
 *
18
 * @author Qiang Xue <[email protected]>
19
 * @since 2.0
20
 */
21
class Request extends \yii\base\Request
22
{
23
    private $_params;
24
25
26
    /**
27
     * Returns the command line arguments.
28
     * @return array the command line arguments. It does not include the entry script name.
29
     */
30 6
    public function getParams()
31
    {
32 6
        if ($this->_params === null) {
33
            if (isset($_SERVER['argv'])) {
34
                $this->_params = $_SERVER['argv'];
35
                array_shift($this->_params);
36
            } else {
37
                $this->_params = [];
38
            }
39
        }
40
41 6
        return $this->_params;
42
    }
43
44
    /**
45
     * Sets the command line arguments.
46
     * @param array $params the command line arguments
47
     */
48 6
    public function setParams($params)
49
    {
50 6
        $this->_params = $params;
51 6
    }
52
53
    /**
54
     * Resolves the current request into a route and the associated parameters.
55
     * @return array the first element is the route, and the second is the associated parameters.
56
     * @throws Exception when parameter is wrong and can not be resolved
57
     */
58 6
    public function resolve()
59
    {
60 6
        $rawParams = $this->getParams();
61 6
        $endOfOptionsFound = false;
62 6
        if (isset($rawParams[0])) {
63 6
            $route = array_shift($rawParams);
64
65 6
            if ($route === '--') {
66 1
                $endOfOptionsFound = true;
67 1
                $route = array_shift($rawParams);
68 1
            }
69 6
        } else {
70
            $route = '';
71
        }
72
73 6
        $params = [];
74 6
        foreach ($rawParams as $param) {
75 5
            if ($endOfOptionsFound) {
76 2
                $params[] = $param;
77 5
            } elseif ($param === '--') {
78 1
                $endOfOptionsFound = true;
79 4
            } elseif (preg_match('/^--(\w+)(?:=(.*))?$/', $param, $matches)) {
80 3
                $name = $matches[1];
81 3
                if (is_numeric(substr($name, 0, 1))) {
82 1
                    throw new Exception('Parameter "' . $name . '" is not valid');
83
                }
84
85 2
                if ($name !== Application::OPTION_APPCONFIG) {
86 2
                    $params[$name] = isset($matches[2]) ? $matches[2] : true;
87 2
                }
88 3
            } elseif (preg_match('/^-(\w+)(?:=(.*))?$/', $param, $matches)) {
89 2
                $name = $matches[1];
90 2
                if (is_numeric($name)) {
91 2
                    $params[] = $param;
92 2
                } else {
93 2
                    $params['_aliases'][$name] = isset($matches[2]) ? $matches[2] : true;
94
                }
95 2
            } else {
96 3
                $params[] = $param;
97
            }
98 5
        }
99
100 5
        return [$route, $params];
101
    }
102
}
103