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