UserController::actionDelete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
nc 2
nop 1
dl 0
loc 7
c 0
b 0
f 0
cc 2
rs 10
1
<?php
2
3
namespace app\controllers\admin;
4
5
use Yii;
6
use yii\base\InvalidConfigException;
7
use app\traits\AdminBeforeActionTrait;
8
9
/**
10
 * Class UserController
11
 * UserController implements the CRUD actions for identityClass.
12
 *
13
 * @package app\controllers\admin
14
 */
15
class UserController extends BaseUserController
16
{
17
    use AdminBeforeActionTrait;
0 ignored issues
show
Bug introduced by
The trait app\traits\AdminBeforeActionTrait requires the property $controller which is not provided by app\controllers\admin\UserController.
Loading history...
18
19
    /**
20
     * @return mixed|string
21
     */
22
    public function actionIndex()
23
    {
24
        if (!$this->checkAccessToIndex()) {
25
            return $this->accessError();
26
        }
27
28
        $request = Yii::$app->request;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->request can also be of type yii\web\Request. However, the property $request is declared as type yii\console\Request. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
29
30
        if ($request->get('id') != null && $request->get('order') != null) {
31
32
            if (!$this->checkAccessToAdministrate()) {
33
                return $this->accessError();
34
            }
35
36
            return $this->actionSetOrder($request->get('id'), $request->get('order'));
0 ignored issues
show
Bug introduced by
It seems like $request->get('order') can also be of type array; however, parameter $order of app\controllers\admin\Us...oller::actionSetOrder() does only seem to accept integer, 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

36
            return $this->actionSetOrder($request->get('id'), /** @scrutinizer ignore-type */ $request->get('order'));
Loading history...
Bug introduced by
It seems like $request->get('id') can also be of type array; however, parameter $id of app\controllers\admin\Us...oller::actionSetOrder() does only seem to accept integer, 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

36
            return $this->actionSetOrder(/** @scrutinizer ignore-type */ $request->get('id'), $request->get('order'));
Loading history...
37
        }
38
39
        return parent::actionIndex();
40
    }
41
42
    /**
43
     * @param int $id
44
     * @param int $order
45
     *
46
     * @return \yii\web\Response
47
     */
48
    public function actionSetOrder(int $id, int $order)
49
    {
50
        /* @var \app\models\User $model */
51
        $model = $this->findModel($id);
52
        $model->moveOrder($order);
53
54
        return $this->redirect([
55
            $this->urlPrefix.'index'
56
        ]);
57
    }
58
59
    /**
60
     * @param int|string $id
61
     *
62
     * @return mixed
63
     */
64
    public function actionView($id)
65
    {
66
        if (!$this->checkAccessToView()) {
67
            return $this->accessError();
68
        }
69
70
        return parent::actionView($id);
71
    }
72
73
    /**
74
     * @return mixed|string|\yii\web\Response
75
     */
76
    public function actionCreate()
77
    {
78
        if (!$this->checkAccessToCreate()) {
79
            return $this->accessError();
80
        }
81
82
        return parent::actionCreate();
83
    }
84
85
    /**
86
     * @param int|string $id
87
     *
88
     * @return string|\yii\web\Response
89
     */
90
    public function actionUpdate($id)
91
    {
92
        if ($id != Yii::$app->getUser()->id && !$this->checkAccessToUpdate()) {
93
            return $this->accessError();
94
        }
95
96
        return parent::actionUpdate($id);
97
    }
98
99
    /**
100
     * @param int|string $id
101
     *
102
     * @return mixed|\yii\web\Response
103
     */
104
    public function actionDelete($id)
105
    {
106
        if (!$this->checkAccessToDelete()) {
107
            return $this->accessError();
108
        }
109
110
        return parent::actionDelete($id);
111
    }
112
113
    /**
114
     * Returns addition fields.
115
     *
116
     * @throws InvalidConfigException
117
     *
118
     * @return array
119
     */
120
    protected function getAdditionFields(): array
121
    {
122
        $additionFields = parent::getAdditionFields();
123
124
        if ($this->action->id == 'create' || $this->action->id == 'update') {
125
            return array_merge($additionFields, [
126
                'changeRoles' => $this->checkAccessToSetRoles()
127
            ]);
128
        }
129
130
        if ($this->action->id == 'index') {
131
            return array_merge($additionFields, [
132
                'administrateAccess' => $this->checkAccessToAdministrate()
133
            ]);
134
        }
135
136
        return $additionFields;
137
    }
138
}
139