Completed
Push — master ( edb2b9...da334f )
by Dmitry
13:12
created

src/actions/ProxyAction.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\actions;
12
13
use Yii;
14
use yii\helpers\Url;
15
16
/**
17
 * Class ProxyAction.
18
 *
19
 * @property mixed pjaxUrl the url, which will be set to the `X-PJAX-URL` HTTP header, if request is PJAX.
20
 * Boolean (default true) - whether to set to the header URL, which is based on [[action]] and [[params]]
21
 * Array - direct
22
 * @property array|callable params params, that will be passed to [[action]] arguments when it will be called
23
 */
24
class ProxyAction extends Action
25
{
26
    /**
27
     * @var string|\Closure action to run
28
     */
29
    public $action;
30
31
    /**
32
     * @var array
33
     * @see params
34
     */
35
    public $_params = [];
36
37
    /**
38
     * @var boolean
39
     * @see pjaxUrl
40
     */
41
    public $_pjaxUrl = true;
42
43
    /**
44
     * @return array
45
     */
46
    public function getParams()
47
    {
48
        if ($this->_params instanceof \Closure) {
49
            return call_user_func($this->_params, $this, $this->getModel());
50
        }
51
52
        return $this->_params;
53
    }
54
55
    /**
56
     * @return bool|string
57
     */
58
    public function getPjaxUrl()
59
    {
60
        if ($this->_pjaxUrl instanceof \Closure) {
61
            $url = call_user_func($this->_pjaxUrl, $this);
62
        } elseif ($this->_pjaxUrl === true) {
63
            $url = (array) $this->action;
64
        } elseif (is_array($this->_pjaxUrl) || is_string($this->_pjaxUrl)) {
65
            $url = $this->_pjaxUrl;
66
        } else {
67
            return false;
68
        }
69
70
        return Url::to($url);
71
    }
72
73
    /**
74
     * @param $params
75
     */
76
    public function setParams($params)
77
    {
78
        $this->_params = $params;
79
    }
80
81
    /**
82
     * @return mixed
83
     */
84
    public function run()
85
    {
86
        if ($this->pjaxUrl && $this->parent->rule->requestType === 'pjax') {
87
            Yii::$app->response->getHeaders()->add('X-PJAX-URL', $this->pjaxUrl);
88
        }
89
        $action = $this->action instanceof \Closure ? call_user_func($this->action, $this) : $this->action;
90
91
        return $this->controller->runAction($action, $this->params);
0 ignored issues
show
$this->params is of type callable, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92
    }
93
94
    /**
95
     * @param mixed $pjaxUrl
96
     */
97
    public function setPjaxUrl($pjaxUrl)
98
    {
99
        $this->_pjaxUrl = $pjaxUrl;
100
    }
101
}
102