Completed
Push — master ( 3204cd...9afd5c )
by Andrii
02:53
created

Request   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 42
ccs 6
cts 7
cp 0.8571
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getParams() 0 21 4
A setParams() 0 5 1
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\base;
12
13
use Yii;
14
15
/**
16
 * The Request.
17
 * Redefined for aliases.
18
 */
19
class Request extends \yii\console\Request
20
{
21
    /**
22
     * @var name of script called, like $0 in sh/perl
23
     */
24
    protected $_self;
25
26
    protected $_args;
27
28
    /**
29
     * Returns the command line arguments.
30
     *
31
     * @return array the command line arguments. It does not include the entry script name
32
     */
33 3
    public function getParams()
0 ignored issues
show
Coding Style introduced by
getParams 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...
34
    {
35 3
        if (!isset($this->_args)) {
36
            if (isset($_SERVER['argv'])) {
37
                $args        = $_SERVER['argv'];
38
                $this->_self = array_shift($args);
39
                $command     = $args[0];
40
                $alias       = Yii::$app->config->aliases->get($command);
41
                if ($alias) {
42
                    array_shift($args);
43
                    $args = array_merge($alias, $args);
44
                }
45
                $this->_args = $args;
46
            } else {
47
                $this->_args = [];
48
            }
49
            $this->setParams($this->_args);
50
        }
51
52 3
        return $this->_args;
53
    }
54
55 3
    public function setParams($params)
56
    {
57
        parent::setParams($params);
58 3
        $this->_args = $params;
59 3
    }
60
}
61