| Conditions | 11 |
| Paths | 43 |
| Total Lines | 96 |
| Code Lines | 69 |
| Lines | 47 |
| Ratio | 48.96 % |
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 |
||
| 268 | public function actionClone($id, $returnUrl = ['index']) |
||
| 269 | { |
||
| 270 | /** @var Page|HasProperties $model */ |
||
| 271 | $model = Page::findOne($id); |
||
| 272 | if ($model === null) { |
||
| 273 | throw new NotFoundHttpException; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** @var Page|HasProperties $newModel */ |
||
| 277 | $newModel = new Page; |
||
| 278 | $newModel->setAttributes($model->attributes, false); |
||
| 279 | $time = time(); |
||
| 280 | $newModel->name .= ' (copy ' . date('Y-m-d h:i:s', $time) . ')'; |
||
| 281 | $newModel->slug .= '-copy-' . date('Ymdhis', $time); |
||
| 282 | $newModel->title .= '-copy-' . date('Ymdhis', $time); |
||
| 283 | $newModel->id = null; |
||
| 284 | View Code Duplication | if ($newModel->validate() === false) { |
|
| 285 | $newModel->slug = substr(uniqid() . "-" . $model->slug, 0, 80); |
||
| 286 | } |
||
| 287 | if ($newModel->save()) { |
||
| 288 | $object = Object::getForClass(get_class($newModel)); |
||
| 289 | $query = new Query(); |
||
| 290 | |||
| 291 | // save images bindings |
||
| 292 | $params = $query->select( |
||
| 293 | ['object_id', 'filename', 'image_title', 'image_alt', 'sort_order'] |
||
| 294 | )->from(Image::tableName())->where( |
||
| 295 | [ |
||
| 296 | 'object_id' => $object->id, |
||
| 297 | 'object_model_id' => $model->id |
||
| 298 | ] |
||
| 299 | )->all(); |
||
| 300 | View Code Duplication | if (!empty($params)) { |
|
| 301 | $rows = []; |
||
| 302 | foreach ($params as $param) { |
||
| 303 | $rows[] = [ |
||
| 304 | $param['object_id'], |
||
| 305 | $newModel->id, |
||
| 306 | $param['filename'], |
||
| 307 | $param['image_title'], |
||
| 308 | $param['image_alt'], |
||
| 309 | $param['sort_order'], |
||
| 310 | ]; |
||
| 311 | } |
||
| 312 | Yii::$app->db->createCommand()->batchInsert( |
||
| 313 | Image::tableName(), |
||
| 314 | [ |
||
| 315 | 'object_id', |
||
| 316 | 'object_model_id', |
||
| 317 | 'filename', |
||
| 318 | 'image_title', |
||
| 319 | 'image_alt', |
||
| 320 | 'sort_order', |
||
| 321 | ], |
||
| 322 | $rows |
||
| 323 | )->execute(); |
||
| 324 | } |
||
| 325 | $newModelProps = []; |
||
| 326 | View Code Duplication | foreach (array_keys($model->propertyGroups) as $key) { |
|
| 327 | $opg = new ObjectPropertyGroup(); |
||
| 328 | $opg->attributes = [ |
||
| 329 | 'object_id' => $object->id, |
||
| 330 | 'object_model_id' => $newModel->id, |
||
| 331 | 'property_group_id' => $key, |
||
| 332 | ]; |
||
| 333 | $opg->save(); |
||
| 334 | $props = Property::getForGroupId($key); |
||
| 335 | foreach ($props as $prop) { |
||
| 336 | $propValues = $model->getPropertyValuesByPropertyId($prop->id); |
||
| 337 | if ($propValues !== null) { |
||
| 338 | foreach ($propValues->values as $val) { |
||
| 339 | $valueToSave = ArrayHelper::getValue($val, 'psv_id', $val['value']); |
||
| 340 | $newModelProps[$prop->key][] = $valueToSave; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | } |
||
| 344 | } |
||
| 345 | $newModel->saveProperties( |
||
| 346 | [ |
||
| 347 | 'Properties_Page_' . $newModel->id => $newModelProps, |
||
| 348 | ] |
||
| 349 | ); |
||
| 350 | $view = ViewObject::findOne(['object_id' => $model->object->id, 'object_model_id' => $model->id]); |
||
| 351 | if ($view !== null) { |
||
| 352 | $newView = new ViewObject; |
||
| 353 | $newView->setAttributes($view->attributes, false); |
||
| 354 | $newView->id = null; |
||
| 355 | $newView->object_model_id = $newModel->id; |
||
| 356 | $newView->save(); |
||
| 357 | } |
||
| 358 | Yii::$app->session->setFlash('success', Yii::t('app', 'Page has been cloned successfully.')); |
||
| 359 | $this->redirect( |
||
| 360 | ['edit', 'id' => $newModel->id, 'parent_id' => $newModel->parent_id, 'returnUrl' => $returnUrl] |
||
| 361 | ); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | } |
||
| 365 |
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
@returnannotation as described here.