Completed
Push — master ( d0bd4d...b4b0fd )
by Igor
23:04
created

BatchAction::run()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
rs 8.439
cc 5
eloc 16
nc 3
nop 0
1
<?php
2
3
namespace app\modules\admin\actions;
4
5
use Yii;
6
use yii\base\Action;
7
8
class BatchAction extends Action
9
{
10
    /**
11
     * @var string $modelClass
12
     */
13
    public $modelClass;
14
    /**
15
     * @var array $actions
16
     */
17
    public $actions;
18
19
    public function run()
20
    {
21
        $model = new $this->modelClass;
22
23
        if (($ids = Yii::$app->request->post('selection')) !== null) {
24
            $models = $model::findAll($ids);
25
            $operation = Yii::$app->request->post('operation');
26
27
            if (isset($this->actions[$operation])) {
28
                $attrubutes = $this->actions[$operation];
29
                foreach ($models as $model) {
30
                    switch ($operation) {
31
                        case 'delete':
32
                            $model->delete();
33
                            break;
34
                        default:
35
                            $model->updateAttributes($attrubutes);
36
                            break;
37
                    }
38
                }
39
            }
40
        }
41
42
        return $this->controller->asJson(true);
0 ignored issues
show
Bug introduced by
The method asJson 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...
43
    }
44
}
45