Conditions | 12 |
Paths | 152 |
Total Lines | 117 |
Code Lines | 74 |
Lines | 7 |
Ratio | 5.98 % |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
51 | public function actionEdit($id = null) |
||
52 | { |
||
53 | $model = new Form(); |
||
54 | if ($id > 0) { |
||
55 | $model = Form::findOne($id); |
||
56 | } |
||
57 | |||
58 | /** @var \app\models\Object $object */ |
||
59 | $object = Object::getForClass(Form::className()); |
||
60 | |||
61 | $propIds = (new Query())->select('property_group_id')->from([ObjectPropertyGroup::tableName()])->where( |
||
62 | [ |
||
63 | 'and', |
||
64 | 'object_id = :object', |
||
65 | 'object_model_id = :id' |
||
66 | ], |
||
67 | [ |
||
68 | ':object' => $object->id, |
||
69 | ':id' => $id |
||
70 | ] |
||
71 | )->column(); |
||
72 | |||
73 | $post = \Yii::$app->request->post(); |
||
74 | $properties = isset($post['Form']['properties']) ? $post['Form']['properties'] : []; |
||
75 | |||
76 | if ($model->load($post) && $model->validate()) { |
||
77 | if ($model->save()) { |
||
78 | $id = $model->id; |
||
79 | $remove = []; |
||
80 | $add = []; |
||
81 | |||
82 | foreach ($propIds as $value) { |
||
83 | $key = array_search($value, $properties); |
||
84 | if ($key === false) { |
||
85 | $remove[] = $value; |
||
86 | } else { |
||
87 | unset($properties[$key]); |
||
88 | } |
||
89 | } |
||
90 | foreach ($properties as $value) { |
||
91 | $add[] = [ |
||
92 | $value, |
||
93 | $object->id, |
||
94 | $id |
||
95 | ]; |
||
96 | } |
||
97 | |||
98 | Yii::$app->db->createCommand()->delete( |
||
99 | ObjectPropertyGroup::tableName(), |
||
100 | [ |
||
101 | 'and', |
||
102 | 'object_id = :object', |
||
103 | 'object_model_id = :id', |
||
104 | ['in', 'property_group_id', $remove] |
||
105 | ], |
||
106 | [ |
||
107 | ':object' => $object->id, |
||
108 | ':id' => $id, |
||
109 | ] |
||
110 | )->execute(); |
||
111 | |||
112 | View Code Duplication | if (!empty($add)) { |
|
113 | Yii::$app->db->createCommand()->batchInsert( |
||
114 | ObjectPropertyGroup::tableName(), |
||
115 | ['property_group_id', 'object_id', 'object_model_id'], |
||
116 | $add |
||
117 | )->execute(); |
||
118 | } |
||
119 | |||
120 | \Yii::$app->session->setFlash('info', Yii::t('app', 'Object saved')); |
||
121 | $returnUrl = Yii::$app->request->get('returnUrl', ['/backend/form/index', 'id' => $model->id]); |
||
122 | switch (Yii::$app->request->post('action', 'save')) { |
||
123 | case 'next': |
||
124 | return $this->redirect( |
||
125 | [ |
||
126 | '/backend/form/edit', |
||
127 | 'returnUrl' => $returnUrl, |
||
128 | ] |
||
129 | ); |
||
130 | case 'back': |
||
131 | return $this->redirect($returnUrl); |
||
132 | default: |
||
133 | return $this->redirect( |
||
134 | Url::toRoute( |
||
135 | [ |
||
136 | '/backend/form/edit', |
||
137 | 'id' => $model->id, |
||
138 | 'returnUrl' => $returnUrl, |
||
139 | ] |
||
140 | ) |
||
141 | ); |
||
142 | } |
||
143 | } else { |
||
144 | \Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot update data')); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | $items = ArrayHelper::map( |
||
149 | PropertyGroup::find()->where( |
||
150 | 'object_id = :object', |
||
151 | [ |
||
152 | ':object' => $object->id, |
||
153 | ] |
||
154 | )->all(), |
||
155 | 'id', |
||
156 | 'name' |
||
157 | ); |
||
158 | |||
159 | return $this->render( |
||
160 | 'edit', |
||
161 | [ |
||
162 | 'model' => $model, |
||
163 | 'items' => $items, |
||
164 | 'selected' => $propIds, |
||
165 | ] |
||
166 | ); |
||
167 | } |
||
168 | |||
290 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: