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

OperationsAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 37
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 25 5
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