Completed
Push — master ( f3f0c3...6f1d07 )
by Andrii
03:04
created

src/actions/RedirectAction.php (1 issue)

Labels
Severity

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
 * HiSite Yii2 base project
4
 *
5
 * @link      https://github.com/hiqdev/hisite
6
 * @package   hisite
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hisite\actions;
12
13
use Closure;
14
use Yii;
15
use yii\base\Response;
16
use yii\helpers\Url;
17
18
/**
19
 * Action to redirect to given url.
20
 *
21
 * @property array|string|Closure url the URL for redirect. Can be string, array or Closure
22
 */
23
class RedirectAction extends \yii\base\Action
24
{
25
    /**
26
     * @var string|array url to redirect to
27
     */
28
    protected $_url;
29
30
    /**
31
     * Collects the URL array, executing callable functions.
32
     * @return string|array default return to previous page (referer)
33
     */
34
    public function getUrl()
35
    {
36
        if ($this->_url instanceof Closure) {
37
            return call_user_func($this->_url, $this);
38
        }
39
40
        return $this->_url ?: Yii::$app->request->referrer;
41
    }
42
43
    /**
44
     * @param $url
45
     */
46
    public function setUrl($url)
47
    {
48
        $this->_url = $url;
49
    }
50
51
    public function run()
52
    {
53
        $url = $this->getUrl();
54
55
        return $url instanceof Response ? $url : $this->controller->redirect($url);
0 ignored issues
show
The method redirect does only exist in yii\web\Controller, but not in yii\base\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
56
    }
57
}
58