Completed
Push — master ( 3082b8...6de04a )
by Andrii
04:26
created

SelectAction::setModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * Yii2 module for language switching
5
 *
6
 * @link      https://github.com/hiqdev/yii2-language
7
 * @package   yii2-language
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\yii2\language\actions;
13
14
use Yii;
15
16
/**
17
 * Select language action.
18
 *
19
 * @property Module $module The module to be used, can be found by default.
20
 */
21
class SelectAction extends \yii\base\Action
22
{
23
    use \hiqdev\yii2\language\GetModuleTrait;
24
25
    public function run($language)
26
    {
27
        $this->getModule()->setLanguage($language);
28
29
        $url = Yii::$app->request->referrer;
30
        if ($url === null) {
31
            $url = Yii::$app->getHomeUrl();
0 ignored issues
show
Bug introduced by
The method getHomeUrl does only exist in yii\web\Application, but not in yii\console\Application.

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...
32
        }
33
34
        return Yii::$app->response->redirect($url);
35
    }
36
}
37