UpdateAttributesAction::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace app\modules\admin\actions;
4
5
use yii\base\Action;
6
7
class UpdateAttributesAction extends Action
8
{
9
    /**
10
     * @var string $modelClass
11
     */
12
    public $modelClass;
13
    /**
14
     * @var array $attributes
15
     */
16
    public $attributes;
17
18
    public function run(string $id)
19
    {
20
        $model = $this->modelClass::findOne($id);
0 ignored issues
show
Bug introduced by
The method findOne cannot be called on $this->modelClass (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
21
22
        $model->setAttributes($this->attributes, false);
23
        return $this->controller->asJson($model->save(false));
0 ignored issues
show
Bug introduced by
The method asJson 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...
24
    }
25
}
26