GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — filters-dev ( b8c330...c59b06 )
by Ivan
11:21
created

BackendCategoryController   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 241
Duplicated Lines 19.09 %

Coupling/Cohesion

Components 1
Dependencies 5
Metric Value
wmc 30
lcom 1
cbo 5
dl 46
loc 241
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A actionAutocomplete() 0 22 2
A behaviors() 14 14 1
B actions() 0 36 1
A actionIndex() 23 23 2
B actionDelete() 0 22 4
A actionRemoveAll() 0 13 3
D actionEdit() 9 96 17

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace app\modules\shop\controllers;
4
5
use app\backend\actions\PropertyHandler;
6
use app\backend\components\BackendController;
7
use app\backend\events\BackendEntityEditEvent;
8
use app\modules\image\widgets\views\AddImageAction;
9
use app\modules\shop\models\Category;
10
use app\models\Object;
11
use app\models\ViewObject;
12
use app\properties\HasProperties;
13
use app\modules\image\widgets\RemoveAction;
14
use app\modules\image\widgets\SaveInfoAction;
15
use app\modules\image\widgets\UploadAction;
16
use devgroup\JsTreeWidget\AdjacencyFullTreeDataAction;
17
use devgroup\JsTreeWidget\TreeNodeMoveAction;
18
use devgroup\JsTreeWidget\TreeNodesReorderAction;
19
use Yii;
20
use yii\db\Query;
21
use yii\filters\AccessControl;
22
use yii\helpers\Json;
23
use yii\helpers\Url;
24
use yii\web\NotFoundHttpException;
25
use yii\web\ServerErrorHttpException;
26
27
class BackendCategoryController extends BackendController
28
{
29
30
    const BACKEND_CATEGORY_EDIT = 'backend-category-edit';
31
    const BACKEND_CATEGORY_EDIT_SAVE = 'backend-category-edit-save';
32
    const BACKEND_CATEGORY_EDIT_FORM = 'backend-category-edit-form';
33
    const BACKEND_CATEGORY_AFTER_SAVE = 'backend-category-after-save';
34
35 View Code Duplication
    public function behaviors()
36
    {
37
        return [
38
            'access' => [
39
                'class' => AccessControl::className(),
40
                'rules' => [
41
                    [
42
                        'allow' => true,
43
                        'roles' => ['category manage'],
44
                    ],
45
                ],
46
            ],
47
        ];
48
    }
49
50
    public function actions()
51
    {
52
        return [
53
            'getTree' => [
54
                'class' => AdjacencyFullTreeDataAction::className(),
55
                'class_name' => Category::className(),
56
                'model_label_attribute' => 'name',
57
            ],
58
            'move' => [
59
                'class' => TreeNodeMoveAction::className(),
60
                'className' => Category::className(),
61
            ],
62
            'reorder' => [
63
                'class' => TreeNodesReorderAction::className(),
64
                'className' => Category::className(),
65
            ],
66
            'addImage' => [
67
                'class' => AddImageAction::className(),
68
            ],
69
            'upload' => [
70
                'class' => UploadAction::className(),
71
                'upload' => 'theme/resources/product-images',
72
            ],
73
            'remove' => [
74
                'class' => RemoveAction::className(),
75
                'uploadDir' => 'theme/resources/product-images',
76
            ],
77
            'save-info' => [
78
                'class' => SaveInfoAction::className(),
79
            ],
80
            'property-handler' => [
81
                'class' => PropertyHandler::className(),
82
                'modelName' => Category::className()
83
            ]
84
        ];
85
    }
86
87 View Code Duplication
    public function actionIndex($parent_id = 0)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
88
    {
89
        $searchModel = new Category();
90
        $searchModel->parent_id = $parent_id;
91
92
        $params = Yii::$app->request->get();
93
94
        $dataProvider = $searchModel->search($params);
95
96
        $model = null;
97
        if ($parent_id > 0) {
98
            $model = Category::findOne($parent_id);
99
        }
100
101
        return $this->render(
102
            'index',
103
            [
104
                'dataProvider' => $dataProvider,
105
                'searchModel' => $searchModel,
106
                'model' => $model,
107
            ]
108
        );
109
    }
110
111
    public function actionEdit($parent_id = null, $id = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
112
    {
113
        if (null === $parent_id) {
114
            throw new NotFoundHttpException;
115
        }
116
117
        if (null === $object = Object::getForClass(Category::className())) {
118
            throw new ServerErrorHttpException;
119
        }
120
121
        /** @var null|Category|HasProperties $model */
122
        $model = null;
123
        if (null !== $id) {
124
            $model = Category::findById($id, null, null);
125
        } else {
126
            $parent = Category::findById($parent_id, null, null);
127
            if ($parent_id === '0' || !is_null($parent)) {
128
                $model = new Category;
129
                $model->loadDefaultValues();
130
                $model->parent_id = $parent_id;
131
                if ($parent_id !== '0') {
132
                    $model->category_group_id = $parent->category_group_id;
133
                }
134
            } else {
135
                throw new ServerErrorHttpException;
136
            }
137
        }
138
139
        if (null === $model) {
140
            throw new ServerErrorHttpException;
141
        }
142
143
        $event = new BackendEntityEditEvent($model);
144
        $this->trigger(self::BACKEND_CATEGORY_EDIT, $event);
145
146
        $post = \Yii::$app->request->post();
147
        if ($event->isValid && $model->load($post) && $model->validate()) {
148
            $saveStateEvent = new BackendEntityEditEvent($model);
149
            $this->trigger(self::BACKEND_CATEGORY_EDIT_SAVE, $saveStateEvent);
150
151
            $save_result = $model->save();
152
            $model->saveProperties($post);
153
154 View Code Duplication
            if (null !== $view_object = ViewObject::getByModel($model, true)) {
155
                if ($view_object->load($post, 'ViewObject')) {
156
                    if ($view_object->view_id <= 0) {
157
                        $view_object->delete();
158
                    } else {
159
                        $view_object->save();
160
                    }
161
                }
162
            }
163
164
            if ($save_result) {
165
                $modelAfterSaveEvent = new BackendEntityEditEvent($model);
166
                $this->trigger(self::BACKEND_CATEGORY_AFTER_SAVE, $modelAfterSaveEvent);
167
168
                $this->runAction('save-info', ['model_id'=>$model->id]);
169
                Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved'));
170
                $returnUrl = Yii::$app->request->get('returnUrl', ['index']);
171
                switch (Yii::$app->request->post('action', 'save')) {
172
                    case 'next':
173
                        return $this->redirect(
174
                            [
175
                                'edit',
176
                                'returnUrl' => $returnUrl,
177
                                'parent_id' => Yii::$app->request->get('parent_id', null)
178
                            ]
179
                        );
180
                    case 'back':
181
                        return $this->redirect($returnUrl);
182
                    default:
183
                        return $this->redirect(
184
                            Url::toRoute(
185
                                [
186
                                    'edit',
187
                                    'id' => $model->id,
188
                                    'returnUrl' => $returnUrl,
189
                                    'parent_id' => $model->parent_id
190
                                ]
191
                            )
192
                        );
193
                }
194
            } else {
195
                throw new ServerErrorHttpException;
196
            }
197
        }
198
199
        return $this->render(
200
            'category-form',
201
            [
202
                'model' => $model,
203
                'object' => $object,
204
            ]
205
        );
206
    }
207
208
    public function actionDelete($id = null, $parent_id = null, $mode = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
209
    {
210
211
        if ((null === $id) || (null === $model = Category::findById($id, null, null))) {
212
            throw new NotFoundHttpException;
213
        }
214
215
        $model->deleteMode = $mode;
216
        if (!$model->delete()) {
217
            Yii::$app->session->setFlash('success', Yii::t('app', 'The object is placed in the cart'));
218
        } else {
219
            Yii::$app->session->setFlash('success', Yii::t('app', 'Object has been removed'));
220
        }
221
222
        return $this->redirect(
223
            Yii::$app->request->get(
224
                'returnUrl',
225
                Url::to(['index', 'parent_id' => $model->parent_id])
226
            )
227
        );
228
229
    }
230
231
    public function actionRemoveAll($parent_id, $mode = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
232
    {
233
        $items = Yii::$app->request->post('items', []);
234
        if (!empty($items)) {
235
            $items = Category::findAll(['id' => $items]);
236
            /** @var Category[] $items */
237
            foreach ($items as $item) {
238
                $item->deleteMode = $mode;
239
                $item->delete();
240
            }
241
        }
242
        return $this->redirect(['index', 'parent_id' => $parent_id]);
243
    }
244
245
    public function actionAutocomplete()
246
    {
247
        /**
248
         * @todo Добавить отображение вложенности
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
249
         */
250
        $search = Yii::$app->request->get('search');
251
        $out = ['more' => false];
252
        if (!is_null($search['term'])) {
253
            $query = new Query;
254
            $query->select('id, name AS text')->from(Category::tableName())->andWhere(['like', 'name', $search['term']])->limit(
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
255
                100
256
            );
257
            $command = $query->createCommand();
258
            $data = $command->queryAll();
259
            $out['results'] = array_values($data);
260
        }/* elseif ($id > 0) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
261
            $out['results'] = ['id' => $id, 'text' => Category::findOne($id)->name];
262
        } else {
263
            $out['results'] = ['id' => 0, 'text' => Yii::t('app', 'No matching records found')];
264
        }*/
265
        echo Json::encode($out);
266
    }
267
}
268