Completed
Push — master ( 2e8683...7e20fc )
by Igor
07:20
created

OperationsAction::run()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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