RedirectAction   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 35
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrl() 0 8 3
A setUrl() 0 4 1
A run() 0 6 2
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
Bug introduced by
The method redirect does only exist in yii\web\Controller, but not in yii\base\Controller and yii\console\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