Conditions | 11 |
Paths | 43 |
Total Lines | 96 |
Code Lines | 69 |
Lines | 47 |
Ratio | 48.96 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
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 |
||
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); |
||
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) { |
|
279 | $newModel->slug = substr(uniqid() . "-" . $model->slug, 0, 80); |
||
280 | } |
||
281 | if ($newModel->save()) { |
||
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) { |
||
330 | $propValues = $model->getPropertyValuesByPropertyId($prop->id); |
||
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( |
||
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 |
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.