| Conditions | 15 |
| Paths | 106 |
| Total Lines | 124 |
| Code Lines | 92 |
| Lines | 73 |
| Ratio | 58.87 % |
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 |
||
| 137 | public function actionEditProperty($property_group_id, $id = null) |
||
| 138 | { |
||
| 139 | if ($id === null) { |
||
| 140 | $model = new Property(); |
||
| 141 | $model->handler_additional_params = '[]'; |
||
| 142 | } else { |
||
| 143 | $model = Property::findById($id); |
||
| 144 | } |
||
| 145 | $object = Object::getForClass(Property::className()); |
||
| 146 | $model->property_group_id = $property_group_id; |
||
| 147 | |||
| 148 | if ($model->load(\Yii::$app->request->post()) && $model->validate()) { |
||
| 149 | $propertyHandler = PropertyHandlers::createHandler($model->handler); |
||
| 150 | if (!$propertyHandler->changePropertyType($model)) { |
||
| 151 | if ($model->is_column_type_stored) { |
||
| 152 | if ($model->isNewRecord) { |
||
| 153 | $object = Object::findById($model->group->object_id); |
||
| 154 | Yii::$app->db->createCommand() |
||
| 155 | ->addColumn($object->column_properties_table_name, $model->key, "TINYTEXT") |
||
| 156 | ->execute(); |
||
| 157 | if ($object->object_class == Form::className()) { |
||
| 158 | $submissionObject = Object::getForClass(Submission::className()); |
||
| 159 | $col_type = $this->getColumnType($model->value_type); |
||
| 160 | Yii::$app->db->createCommand() |
||
| 161 | ->addColumn($submissionObject->column_properties_table_name, $model->key, $col_type) |
||
| 162 | ->execute(); |
||
| 163 | } |
||
| 164 | } else { |
||
| 165 | View Code Duplication | if ($model->key != $model->getOldAttribute('key')) { |
|
| 166 | $object = Object::findById($model->group->object_id); |
||
| 167 | Yii::$app->db->createCommand() |
||
| 168 | ->renameColumn( |
||
| 169 | $object->column_properties_table_name, |
||
| 170 | $model->getOldAttribute('key'), |
||
| 171 | $model->key |
||
| 172 | )->execute(); |
||
| 173 | if ($object->object_class == Form::className()) { |
||
| 174 | $submissionObject = Object::getForClass(Submission::className()); |
||
| 175 | Yii::$app->db->createCommand() |
||
| 176 | ->renameColumn( |
||
| 177 | $submissionObject->column_properties_table_name, |
||
| 178 | $model->getOldAttribute('key'), |
||
| 179 | $model->key |
||
| 180 | )->execute(); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | View Code Duplication | if ($model->value_type != $model->getOldAttribute('value_type')) { |
|
| 184 | $object = Object::findById($model->group->object_id); |
||
| 185 | $new_type = $this->getColumnType($model->value_type); |
||
| 186 | Yii::$app->db->createCommand() |
||
| 187 | ->alterColumn( |
||
| 188 | $object->column_properties_table_name, |
||
| 189 | $model->getOldAttribute('key'), |
||
| 190 | $new_type |
||
| 191 | )->execute(); |
||
| 192 | if ($object->object_class == Form::className()) { |
||
| 193 | $submissionObject = Object::getForClass(Submission::className()); |
||
| 194 | Yii::$app->db->createCommand() |
||
| 195 | ->renameColumn( |
||
| 196 | $submissionObject->column_properties_table_name, |
||
| 197 | $model->getOldAttribute('key'), |
||
| 198 | $new_type |
||
| 199 | )->execute(); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | $save_result = $model->save(); |
||
| 207 | View Code Duplication | if ($save_result) { |
|
| 208 | $this->runAction('save-info'); |
||
| 209 | Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); |
||
| 210 | $returnUrl = Yii::$app->request->get( |
||
| 211 | 'returnUrl', |
||
| 212 | [ |
||
| 213 | '/backend/properties/group', |
||
| 214 | 'id' => $property_group_id, |
||
| 215 | ] |
||
| 216 | ); |
||
| 217 | switch (Yii::$app->request->post('action', 'save')) { |
||
| 218 | case 'next': |
||
| 219 | return $this->redirect( |
||
| 220 | [ |
||
| 221 | '/backend/properties/edit-property', |
||
| 222 | 'property_group_id' => $property_group_id, |
||
| 223 | 'returnUrl' => $returnUrl, |
||
| 224 | ] |
||
| 225 | ); |
||
| 226 | case 'back': |
||
| 227 | return $this->redirect($returnUrl); |
||
| 228 | default: |
||
| 229 | return $this->redirect( |
||
| 230 | Url::toRoute( |
||
| 231 | [ |
||
| 232 | '/backend/properties/edit-property', |
||
| 233 | 'id' => $model->id, |
||
| 234 | 'property_group_id' => $model->property_group_id, |
||
| 235 | 'returnUrl' => $returnUrl, |
||
| 236 | ] |
||
| 237 | ) |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | } else { |
||
| 241 | Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot save data')); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | $searchModel = new PropertyStaticValues(); |
||
| 246 | |||
| 247 | $searchModel->property_id = $model->id; |
||
| 248 | $dataProvider = $searchModel->search($_GET); |
||
| 249 | |||
| 250 | return $this->render( |
||
| 251 | 'edit-property', |
||
| 252 | [ |
||
| 253 | 'model' => $model, |
||
| 254 | 'dataProvider' => $dataProvider, |
||
| 255 | 'searchModel' => $searchModel, |
||
| 256 | 'fieldinterpretParentId' => 0, |
||
| 257 | 'object' => $object, |
||
| 258 | ] |
||
| 259 | ); |
||
| 260 | } |
||
| 261 | |||
| 472 |
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: