RolesController::actionEdit()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 7.4572

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.2248
c 0
b 0
f 0
ccs 7
cts 13
cp 0.5385
cc 5
nc 6
nop 1
crap 7.4572
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use Yii;
6
use yii\web\NotFoundHttpException;
7
use app\controllers\ControllerTrait;
8
use app\models\entity\AuthItem;
9
use app\modules\admin\models\forms\AuthItemForm;
10
use app\modules\admin\models\search\AuthItemSearch;
11
12
class RolesController extends \yii\web\Controller
13
{
14
    use ControllerTrait;
15
16 4
    public function behaviors()
17
    {
18
        return [
19 4
            'verbs' => [
20
                'class' => 'yii\filters\VerbFilter',
21
                'actions' => [
22
                    'delete' => ['post'],
23
                    'batch' => ['post'],
24
                ],
25
            ],
26
        ];
27
    }
28
29 4
    public function actions()
30
    {
31
        return [
32 4
            'batch' => [
33
                'class' => 'app\modules\admin\actions\BatchAction',
34
                'modelClass' => 'app\models\entity\AuthItem',
35
                'actions' => [
36
                    'delete' => [],
37
                ]
38
            ],
39
            'delete' => [
40
                'class' => 'app\modules\admin\actions\DeleteAction',
41
                'modelClass' => 'app\models\entity\AuthItem',
42
            ],
43
        ];
44
    }
45
46 4
    public function actionIndex()
47
    {
48 4
        $search = new AuthItemSearch();
49 4
        $provider = $search->search(Yii::$app->request->get());
50
51 4
        return $this->render('index', [
52 4
            'search' => $search,
53 4
            'provider' => $provider,
54
        ]);
55
    }
56
57 3
    public function actionEdit($name = null)
58
    {
59 3
        $model = new AuthItemForm();
60
61 3
        if ($name) {
62 2
            $model->setModel($this->findModel($name));
0 ignored issues
show
Compatibility introduced by
$this->findModel($name) of type object<yii\base\Model> is not a sub-type of object<app\models\entity\AuthItem>. It seems like you assume a child class of the class yii\base\Model to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
63
        }
64
65 2
        if (Yii::$app->request->isPost) {
66
            Yii::$app->response->format = 'json';
67
68
            if ($model->load(Yii::$app->request->post()) && $model->validate()) {
69
                $model->save();
70
71
                Yii::$app->session->setFlash('success', Yii::t('app', 'Saved successfully'));
72
                return $this->redirect(['edit', 'name' => $model->name]);
73
            }
74
            return $this->asJsonModelErrors($model);
0 ignored issues
show
Documentation introduced by
$model is of type object<app\modules\admin...els\forms\AuthItemForm>, but the function expects a object<app\controllers\y...ollers\yii\base\Model>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
        }
76
77 2
        return $this->render('edit', [
78 2
            'model' => $model,
79
        ]);
80
    }
81
82
    /**
83
     * Find the model.
84
     * If the model is not found, then 404 HTTP exception will be thrown.
85
     *
86
     * @param string $name
87
     * @return Model
88
     * @throws NotFoundHttpException
89
     */
90
    private function findModel($name): yii\base\Model
91
    {
92
        $model = AuthItem::findOne($name);
93
94
        if ($model === null) {
95
            throw new NotFoundHttpException(Yii::t('app', 'Page not found'));
96
        }
97
98
        return $model;
99
    }
100
}
101