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.

DynamicContentController::actionEdit()   B
last analyzed

Complexity

Conditions 9
Paths 36

Size

Total Lines 52

Duplication

Lines 7
Ratio 13.46 %

Importance

Changes 0
Metric Value
dl 7
loc 52
rs 7.4917
c 0
b 0
f 0
cc 9
nc 36
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace app\backend\controllers;
4
5
use app\backend\actions\DeleteOne;
6
use app\backend\actions\MultipleDelete;
7
use app\backend\components\BackendController;
8
use app\backend\events\BackendEntityEditEvent;
9
use app\backend\traits\BackendRedirect;
10
use app\models\DynamicContent;
11
use app\models\Property;
12
use app\models\PropertyGroup;
13
use app\models\PropertyStaticValues;
14
use Yii;
15
use yii\db\Query;
16
use yii\filters\AccessControl;
17
18
class DynamicContentController extends BackendController
19
{
20
    use BackendRedirect;
21
22
    const BACKEND_DYNAMIC_CONTENT_EDIT = 'backend-dynamic-content-edit';
23
    const BACKEND_DYNAMIC_CONTENT_EDIT_SAVE = 'backend-dynamic-content-edit-save';
24
    const BACKEND_DYNAMIC_CONTENT_EDIT_FORM = 'backend-dynamic-content-edit-form';
25
26
    /**
27
     * @inheritdoc
28
     */
29 View Code Duplication
    public function behaviors()
30
    {
31
        return [
32
            'access' => [
33
                'class' => AccessControl::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
34
                'rules' => [
35
                    [
36
                        'allow' => true,
37
                        'roles' => ['content manage'],
38
                    ],
39
                ],
40
            ],
41
        ];
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 View Code Duplication
    public function actions()
48
    {
49
        return [
50
            'remove-all' => [
51
                'class' => MultipleDelete::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
52
                'modelName' => DynamicContent::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
53
            ],
54
            'delete' => [
55
                'class' => DeleteOne::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
56
                'modelName' => DynamicContent::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
57
            ],
58
        ];
59
    }
60
61 View Code Duplication
    public function actionIndex()
62
    {
63
        $searchModel = new DynamicContent();
64
        $dataProvider = $searchModel->search($_GET);
65
66
        return $this->render(
67
            'index',
68
            [
69
                'dataProvider' => $dataProvider,
70
                'searchModel' => $searchModel,
71
            ]
72
        );
73
    }
74
75
    public function actionEdit($id = null)
76
    {
77
        $model = new DynamicContent;
78
        $model->loadDefaultValues();
79
80
        if ($id !== null) {
81
            $model = DynamicContent::findOne($id);
82
        }
83
84
        $static_values_properties = [];
85
        $get = Yii::$app->request->get();
86
        $post = Yii::$app->request->post();
87
        if (isset($get['DynamicContent'])) {
88
            $post = $get;
89
            if (isset($get['DynamicContent']['object_id'])) {
90
                $model->object_id = (int)$get['DynamicContent']['object_id'];
91
            }
92
        }
93
        $property_groups_ids_for_object = (new Query)->select('id')->from(PropertyGroup::tableName())->where([])->column();
94
95
        $properties = Property::find()->where(
96
            [
97
                'has_static_values' => 1,
98
                'has_slugs_in_values' => 1,
99
            ]
100
        )->andWhere(['in', 'property_group_id', $property_groups_ids_for_object])->all();
101 View Code Duplication
        foreach ($properties as $prop) {
102
            $static_values_properties[$prop->id] = [
103
                'has_static_values' => true,
104
                'property' => $prop,
105
                'static_values_select' => PropertyStaticValues::getSelectForPropertyId($prop->id),
106
            ];
107
        }
108
109
        if ($model->load($post) && $model->validate()&&!isset($get['DynamicContent'])) {
110
            $save_result = $model->save();
111
            if ($save_result) {
112
                $saveStateEvent = new BackendEntityEditEvent($model);
0 ignored issues
show
Documentation introduced by
$model is of type object<yii\db\ActiveRecordInterface>|array|null, but the function expects a object<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...
113
                $this->trigger(self::BACKEND_DYNAMIC_CONTENT_EDIT_SAVE, $saveStateEvent);
114
                return $this->redirectUser($model->id);
115
            }
116
            Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot update data'));
117
118
        }
119
        return $this->render(
120
            'dynamic-content-form',
121
            [
122
                'model' => $model,
123
                'static_values_properties' => $static_values_properties,
124
            ]
125
        );
126
    }
127
}
128