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.
Test Setup Failed
Push — action_getCatTree_order ( 77944b )
by
unknown
23:56
created

PageController   D

Complexity

Total Complexity 38

Size/Duplication

Total Lines 331
Duplicated Lines 34.74 %

Coupling/Cohesion

Components 1
Dependencies 23

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 38
lcom 1
cbo 23
dl 115
loc 331
rs 4.62
c 2
b 1
f 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A actionIndex() 22 22 2
A actionDelete() 0 19 4
A behaviors() 0 14 1
B actions() 37 37 1
C actionEdit() 9 81 13
A actionRemoveAll() 0 12 3
A actionRestore() 0 21 3
C actionClone() 47 96 11

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\page\backend;
4
5
use app\backend\actions\PropertyHandler;
6
use app\backend\events\BackendEntityEditEvent;
7
use app\models\Object;
8
use app\models\ObjectPropertyGroup;
9
use app\models\Property;
10
use app\modules\image\models\Image;
11
use app\modules\image\widgets\views\AddImageAction;
12
use app\modules\page\models\Page;
13
use app\models\ViewObject;
14
use app\properties\HasProperties;
15
use app\modules\image\widgets\RemoveAction;
16
use app\modules\image\widgets\SaveInfoAction;
17
use app\modules\image\widgets\UploadAction;
18
use devgroup\JsTreeWidget\AdjacencyFullTreeDataAction;
19
use devgroup\JsTreeWidget\TreeNodeMoveAction;
20
use devgroup\JsTreeWidget\TreeNodesReorderAction;
21
use Yii;
22
use yii\db\Query;
23
use yii\filters\AccessControl;
24
use yii\helpers\ArrayHelper;
25
use yii\helpers\Url;
26
use yii\web\NotFoundHttpException;
27
28
class PageController extends \app\backend\components\BackendController
29
{
30
31
    const BACKEND_PAGE_EDIT = 'backend-page-edit';
32
    const BACKEND_PAGE_EDIT_SAVE = 'backend-page-edit-save';
33
    const BACKEND_PAGE_EDIT_FORM = 'backend-page-edit-form';
34
    const BACKEND_PAGE_AFTER_SAVE = 'backend-page-after-save';
35
36
    public function behaviors()
37
    {
38
        return [
39
            'access' => [
40
                'class' => AccessControl::className(),
41
                'rules' => [
42
                    [
43
                        'allow' => true,
44
                        'roles' => ['content manage'],
45
                    ],
46
                ],
47
            ],
48
        ];
49
    }
50
51 View Code Duplication
    public function actions()
52
    {
53
        return [
54
            'getTree' => [
55
                'class' => AdjacencyFullTreeDataAction::className(),
56
                'class_name' => Page::className(),
57
                'model_label_attribute' => 'name',
58
            ],
59
            'addImage' => [
60
                'class' => AddImageAction::className(),
61
            ],
62
            'upload' => [
63
                'class' => UploadAction::className(),
64
                'upload' => 'theme/resources/product-images',
65
            ],
66
            'remove' => [
67
                'class' => RemoveAction::className(),
68
                'uploadDir' => 'theme/resources/product-images',
69
            ],
70
            'save-info' => [
71
                'class' => SaveInfoAction::className(),
72
            ],
73
            'property-handler' => [
74
                'class' => PropertyHandler::className(),
75
                'modelName' => Page::className()
76
            ],
77
            'move' => [
78
                'class' => TreeNodeMoveAction::className(),
79
                'className' => Page::className(),
80
                'saveAttributes' => ['slug_compiled'],
81
            ],
82
            'reorder' => [
83
                'class' => TreeNodesReorderAction::className(),
84
                'className' => Page::className(),
85
            ],
86
        ];
87
    }
88
89 View Code Duplication
    public function actionIndex($parent_id = 1)
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...
90
    {
91
        $searchModel = new Page();
92
        $searchModel->parent_id = $parent_id;
93
94
        $params = Yii::$app->request->get();
95
        $dataProvider = $searchModel->search($params);
96
97
        $model = null;
98
        if ($parent_id > 0) {
99
            $model = Page::findOne($parent_id);
100
        }
101
102
        return $this->render(
103
            'index',
104
            [
105
                'dataProvider' => $dataProvider,
106
                'searchModel' => $searchModel,
107
                'model' => $model,
108
            ]
109
        );
110
    }
111
112
    public function actionEdit($parent_id, $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...
113
    {
114
        $object = Object::getForClass(Page::className());
115
116
        /** @var null|Page|HasProperties $model */
117
        $model = new Page;
118
        $model->published = 1;
119
        if ($id !== null) {
120
            $model = Page::findOne($id);
121
            if ($model === null) {
122
                throw new NotFoundHttpException;
123
            }
124
        }
125
        $model->parent_id = $parent_id;
126
127
        $event = new BackendEntityEditEvent($model);
128
        $this->trigger(self::BACKEND_PAGE_EDIT, $event);
129
130
        $post = \Yii::$app->request->post();
131
        if ($event->isValid && $model->load($post)) {
0 ignored issues
show
Bug introduced by
The method load does only exist in app\modules\page\models\Page, but not in app\properties\HasProperties.

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...
132
            $saveStateEvent = new BackendEntityEditEvent($model);
133
            $this->trigger(self::BACKEND_PAGE_EDIT_SAVE, $saveStateEvent);
134
135
            if ($saveStateEvent->isValid && $model->validate()) {
0 ignored issues
show
Bug introduced by
The method validate does only exist in app\modules\page\models\Page, but not in app\properties\HasProperties.

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...
136
                $save_result = $model->save();
0 ignored issues
show
Bug introduced by
The method save does only exist in app\modules\page\models\Page, but not in app\properties\HasProperties.

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...
137
                $model->saveProperties($post);
0 ignored issues
show
Bug introduced by
The method saveProperties does only exist in app\properties\HasProperties, but not in app\modules\page\models\Page.

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...
138
139 View Code Duplication
                if (null !== $view_object = ViewObject::getByModel($model, true)) {
140
                    if ($view_object->load($post, 'ViewObject')) {
141
                        if ($view_object->view_id <= 0) {
142
                            $view_object->delete();
143
                        } else {
144
                            $view_object->save();
145
                        }
146
                    }
147
                }
148
149
                if ($save_result) {
150
                    $modelAfterSaveEvent = new BackendEntityEditEvent($model);
151
                    $this->trigger(self::BACKEND_PAGE_AFTER_SAVE, $modelAfterSaveEvent);
152
153
                    $this->runAction('save-info', ['model_id' => $model->id]);
154
                    Yii::$app->session->setFlash('info', Yii::t('app', 'Object saved'));
155
                    $returnUrl = Yii::$app->request->get('returnUrl', ['/page/backend/index']);
156
                    switch (Yii::$app->request->post('action', 'save')) {
157
                        case 'next':
158
                            return $this->redirect(
159
                                [
160
                                    '/page/backend/edit',
161
                                    'returnUrl' => $returnUrl,
162
                                    'parent_id' => Yii::$app->request->get('parent_id', null)
163
                                ]
164
                            );
165
                        case 'back':
166
                            return $this->redirect($returnUrl);
167
                        default:
168
                            return $this->redirect(
169
                                Url::toRoute(
170
                                    [
171
                                        '/page/backend/edit',
172
                                        'id' => $model->id,
173
                                        'returnUrl' => $returnUrl,
174
                                        'parent_id' => $model->parent_id
175
                                    ]
176
                                )
177
                            );
178
                    }
179
                } else {
180
                    \Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot update data'));
181
                }
182
            }
183
        }
184
185
        return $this->render(
186
            'page-form',
187
            [
188
                'model' => $model,
189
                'object' => $object,
190
            ]
191
        );
192
    }
193
194
    /*
195
     *
196
     */
197
    public function actionDelete($id = null)
198
    {
199
        if ((null === $id) || (null === $model = Page::findOne($id))) {
200
            throw new NotFoundHttpException;
201
        }
202
203
        if (!$model->delete()) {
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $model (of type array|boolean).

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...
204
            Yii::$app->session->setFlash('info', Yii::t('app', 'The object is placed in the cart'));
205
        } else {
206
            Yii::$app->session->setFlash('info', Yii::t('app', 'Object removed'));
207
        }
208
209
        return $this->redirect(
210
            Yii::$app->request->get(
211
                'returnUrl',
212
                Url::toRoute(['index', 'parent_id' => $model->parent_id])
213
            )
214
        );
215
    }
216
217
    public function actionRemoveAll($parent_id)
218
    {
219
        $items = Yii::$app->request->post('items', []);
220
        if (!empty($items)) {
221
            $items = Page::find()->where(['in', 'id', $items])->all();
222
            foreach ($items as $item) {
223
                $item->delete();
224
            }
225
        }
226
227
        return $this->redirect(['index', 'parent_id' => $parent_id]);
228
    }
229
230
    /*
231
     *
232
     */
233
    public function actionRestore($id = null, $parent_id = null)
234
    {
235
        if (null === $id) {
236
            new NotFoundHttpException();
237
        }
238
239
        if (null === $model = Page::findOne(['id' => $id])) {
240
            new NotFoundHttpException();
241
        }
242
243
        $model->restoreFromTrash();
0 ignored issues
show
Bug introduced by
The method restoreFromTrash cannot be called on $model (of type array|boolean).

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...
244
245
        Yii::$app->session->setFlash('success', Yii::t('app', 'Object successfully restored'));
246
247
        return $this->redirect(
248
            Yii::$app->request->get(
249
                'returnUrl',
250
                Url::toRoute(['edit', 'id' => $id, 'parent_id' => $parent_id])
251
            )
252
        );
253
    }
254
255
256
    /**
257
     * Clone page action.
258
     * @param integer $id
259
     * @param array|string $returnUrl
260
     * @throws \yii\web\NotFoundHttpException
261
     */
262
    public function actionClone($id, $returnUrl = ['index'])
263
    {
264
        /** @var Page|HasProperties $model */
265
        $model = Page::findOne($id);
266
        if ($model === null) {
267
            throw new NotFoundHttpException;
268
        }
269
270
        /** @var Page|HasProperties $newModel */
271
        $newModel = new Page;
272
        $newModel->setAttributes($model->attributes, false);
0 ignored issues
show
Bug introduced by
The method setAttributes does only exist in app\modules\page\models\Page, but not in app\properties\HasProperties.

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...
273
        $time = time();
274
        $newModel->name .= ' (copy ' . date('Y-m-d h:i:s', $time) . ')';
275
        $newModel->slug .= '-copy-' . date('Ymdhis', $time);
276
        $newModel->title .= '-copy-' . date('Ymdhis', $time);
277
        $newModel->id = null;
278 View Code Duplication
        if ($newModel->validate() === false) {
0 ignored issues
show
Bug introduced by
The method validate does only exist in app\modules\page\models\Page, but not in app\properties\HasProperties.

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...
279
            $newModel->slug = substr(uniqid() . "-" . $model->slug, 0, 80);
280
        }
281
        if ($newModel->save()) {
0 ignored issues
show
Bug introduced by
The method save does only exist in app\modules\page\models\Page, but not in app\properties\HasProperties.

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...
282
            $object = Object::getForClass(get_class($newModel));
283
            $query = new Query();
284
285
            // save images bindings
286
            $params = $query->select(
287
                ['object_id', 'filename', 'image_title', 'image_alt', 'sort_order']
288
            )->from(Image::tableName())->where(
289
                [
290
                    'object_id' => $object->id,
291
                    'object_model_id' => $model->id
292
                ]
293
            )->all();
294 View Code Duplication
            if (!empty($params)) {
295
                $rows = [];
296
                foreach ($params as $param) {
297
                    $rows[] = [
298
                        $param['object_id'],
299
                        $newModel->id,
300
                        $param['filename'],
301
                        $param['image_title'],
302
                        $param['image_alt'],
303
                        $param['sort_order'],
304
                    ];
305
                }
306
                Yii::$app->db->createCommand()->batchInsert(
307
                    Image::tableName(),
308
                    [
309
                        'object_id',
310
                        'object_model_id',
311
                        'filename',
312
                        'image_title',
313
                        'image_alt',
314
                        'sort_order',
315
                    ],
316
                    $rows
317
                )->execute();
318
            }
319
            $newModelProps = [];
320 View Code Duplication
            foreach (array_keys($model->propertyGroups) as $key) {
321
                $opg = new ObjectPropertyGroup();
322
                $opg->attributes = [
323
                    'object_id' => $object->id,
324
                    'object_model_id' => $newModel->id,
325
                    'property_group_id' => $key,
326
                ];
327
                $opg->save();
328
                $props = Property::getForGroupId($key);
329
                foreach ($props as $prop) {
0 ignored issues
show
Bug introduced by
The expression $props of type null|array<integer,object<app\models\Property>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
330
                    $propValues = $model->getPropertyValuesByPropertyId($prop->id);
0 ignored issues
show
Bug introduced by
The method getPropertyValuesByPropertyId does only exist in app\properties\HasProperties, but not in app\modules\page\models\Page.

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...
331
                    if ($propValues !== null) {
332
                        foreach ($propValues->values as $val) {
333
                            $valueToSave = ArrayHelper::getValue($val, 'psv_id', $val['value']);
334
                            $newModelProps[$prop->key][] = $valueToSave;
335
                        }
336
                    }
337
                }
338
            }
339
            $newModel->saveProperties(
0 ignored issues
show
Bug introduced by
The method saveProperties does only exist in app\properties\HasProperties, but not in app\modules\page\models\Page.

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...
340
                [
341
                    'Properties_Page_' . $newModel->id => $newModelProps,
342
                ]
343
            );
344
            $view = ViewObject::findOne(['object_id' => $model->object->id, 'object_model_id' => $model->id]);
345
            if ($view !== null) {
346
                $newView = new ViewObject;
347
                $newView->setAttributes($view->attributes, false);
348
                $newView->id = null;
349
                $newView->object_model_id = $newModel->id;
350
                $newView->save();
351
            }
352
            Yii::$app->session->setFlash('success', Yii::t('app', 'Page has been cloned successfully.'));
353
            $this->redirect(
354
                ['edit', 'id' => $newModel->id, 'parent_id' => $newModel->parent_id, 'returnUrl' => $returnUrl]
355
            );
356
        }
357
    }
358
}
359