| Conditions | 14 |
| Paths | 24 |
| Total Lines | 108 |
| Code Lines | 67 |
| Lines | 29 |
| Ratio | 26.85 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 45 | public function run() |
||
|
|
|||
| 46 | { |
||
| 47 | app\backend\assets\FrontendEditingAsset::register($this->view); |
||
| 48 | |||
| 49 | $items = [ |
||
| 50 | [ |
||
| 51 | 'label' => Icon::show('dashboard') . ' ' . Yii::t('app', 'Backend'), |
||
| 52 | 'url' => ['/backend/'], |
||
| 53 | ] |
||
| 54 | ]; |
||
| 55 | |||
| 56 | switch (Yii::$app->requestedRoute) { |
||
| 57 | case 'shop/product/list': |
||
| 58 | if (isset($_GET['properties'])) { |
||
| 59 | $apply_if_params = []; |
||
| 60 | foreach ($_GET['properties'] as $property_id => $values) { |
||
| 61 | if (isset($values[0])) { |
||
| 62 | $apply_if_params[$property_id] = $values[0]; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | if (Yii::$app->response->dynamic_content_trait === true) { |
||
| 66 | $items[] = [ |
||
| 67 | 'label' => Icon::show('puzzle') . ' ' . Yii::t('app', 'Edit Dynamic Content'), |
||
| 68 | 'url' => [ |
||
| 69 | '/backend/dynamic-content/edit', |
||
| 70 | 'id' => Yii::$app->response->matched_dynamic_content_trait_model->id, |
||
| 71 | ], |
||
| 72 | ]; |
||
| 73 | } else { |
||
| 74 | if (isset($_GET['properties'], $_GET['last_category_id'])) { |
||
| 75 | $items[] = [ |
||
| 76 | 'label' => Icon::show('puzzle') . ' ' . Yii::t('app', 'Add Dynamic Content'), |
||
| 77 | 'url' => [ |
||
| 78 | '/backend/dynamic-content/edit', |
||
| 79 | 'DynamicContent' => [ |
||
| 80 | 'apply_if_params' => Json::encode($apply_if_params), |
||
| 81 | 'apply_if_last_category_id' => $_GET['last_category_id'], |
||
| 82 | 'object_id' => Object::getForClass(app\modules\shop\models\Product::className())->id, |
||
| 83 | 'route' => 'shop/product/list', |
||
| 84 | ] |
||
| 85 | ], |
||
| 86 | ]; |
||
| 87 | |||
| 88 | } |
||
| 89 | |||
| 90 | } |
||
| 91 | View Code Duplication | } else { |
|
| 92 | // no properties selected - go to category edit page |
||
| 93 | |||
| 94 | if (isset($_GET['last_category_id'])) { |
||
| 95 | $reviewsLink = $this->getReviewEditParams("Category", intval($_GET['last_category_id'])); |
||
| 96 | $cat = app\modules\shop\models\Category::findById($_GET['last_category_id']); |
||
| 97 | $items[] = [ |
||
| 98 | 'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit category'), |
||
| 99 | 'url' => [ |
||
| 100 | '/shop/backend-category/edit', |
||
| 101 | 'id' => $cat->id, |
||
| 102 | 'parent_id' => $cat->parent_id, |
||
| 103 | ], |
||
| 104 | ]; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | break; |
||
| 109 | case 'shop/product/show': |
||
| 110 | if (isset($_GET['model_id'])) { |
||
| 111 | $reviewsLink = $this->getReviewEditParams("Product", intval($_GET['model_id'])); |
||
| 112 | $items[] = [ |
||
| 113 | 'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit product'), |
||
| 114 | 'url' => [ |
||
| 115 | '/shop/backend-product/edit', |
||
| 116 | 'id' => intval($_GET['model_id']) |
||
| 117 | ], |
||
| 118 | ]; |
||
| 119 | } |
||
| 120 | break; |
||
| 121 | |||
| 122 | case '/page/page/show': |
||
| 123 | case '/page/page/list': |
||
| 124 | View Code Duplication | if (isset($_GET['id'])) { |
|
| 125 | $page = app\modules\page\models\Page::findById($_GET['id']); |
||
| 126 | $reviewsLink = $this->getReviewEditParams("Page", $_GET['id']); |
||
| 127 | $items[] = [ |
||
| 128 | 'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit page'), |
||
| 129 | 'url' => [ |
||
| 130 | '/page/backend/edit', |
||
| 131 | 'id' => $page->id, |
||
| 132 | 'parent_id' =>$page->parent_id, |
||
| 133 | |||
| 134 | ], |
||
| 135 | ]; |
||
| 136 | } |
||
| 137 | break; |
||
| 138 | } |
||
| 139 | |||
| 140 | if (!empty($reviewsLink)) { |
||
| 141 | $items[] = $reviewsLink; |
||
| 142 | } |
||
| 143 | |||
| 144 | return $this->render( |
||
| 145 | 'floating-panel', |
||
| 146 | [ |
||
| 147 | 'items' => $items, |
||
| 148 | 'bottom' => $this->bottom, |
||
| 149 | ] |
||
| 150 | ); |
||
| 151 | |||
| 152 | } |
||
| 153 | } |
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: