UpdateAction   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 30
ccs 0
cts 9
cp 0
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 16 4
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\rest;
9
10
use Yii;
11
use yii\base\Model;
12
use yii\db\ActiveRecord;
13
use yii\web\ServerErrorHttpException;
14
15
/**
16
 * UpdateAction implements the API endpoint for updating a model.
17
 *
18
 * For more details and usage information on UpdateAction, see the [guide article on rest controllers](guide:rest-controllers).
19
 *
20
 * @author Qiang Xue <[email protected]>
21
 * @since 2.0
22
 */
23
class UpdateAction extends Action
24
{
25
    /**
26
     * @var string the scenario to be assigned to the model before it is validated and updated.
27
     */
28
    public $scenario = Model::SCENARIO_DEFAULT;
29
30
31
    /**
32
     * Updates an existing model.
33
     * @param string $id the primary key of the model.
34
     * @return \yii\db\ActiveRecordInterface the model being updated
35
     * @throws ServerErrorHttpException if there is any error when updating the model
36
     */
37
    public function run($id)
38
    {
39
        /* @var $model ActiveRecord */
40
        $model = $this->findModel($id);
41
42
        if ($this->checkAccess) {
43
            call_user_func($this->checkAccess, $this->id, $model);
44
        }
45
46
        $model->scenario = $this->scenario;
47
        $model->load(Yii::$app->getRequest()->getBodyParams(), '');
0 ignored issues
show
Bug introduced by
The method getBodyParams() does not exist on yii\console\Request. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        $model->load(Yii::$app->getRequest()->/** @scrutinizer ignore-call */ getBodyParams(), '');
Loading history...
Bug introduced by
It seems like Yii::app->getRequest()->getBodyParams() can also be of type object; however, parameter $data of yii\base\Model::load() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        $model->load(/** @scrutinizer ignore-type */ Yii::$app->getRequest()->getBodyParams(), '');
Loading history...
48
        if ($model->save() === false && !$model->hasErrors()) {
49
            throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
50
        }
51
52
        return $model;
53
    }
54
}
55