| Conditions | 28 |
| Paths | 10946 |
| Total Lines | 173 |
| Code Lines | 111 |
| Lines | 9 |
| Ratio | 5.2 % |
| 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 |
||
| 42 | public function actionList() |
||
| 43 | { |
||
| 44 | |||
| 45 | $request = Yii::$app->request; |
||
| 46 | |||
| 47 | if (null === $request->get('category_group_id')) { |
||
| 48 | throw new NotFoundHttpException; |
||
| 49 | } |
||
| 50 | |||
| 51 | if (null === $object = Object::getForClass(Product::className())) { |
||
| 52 | throw new ServerErrorHttpException('Object not found.'); |
||
| 53 | } |
||
| 54 | |||
| 55 | $category_group_id = intval($request->get('category_group_id', 0)); |
||
| 56 | |||
| 57 | $title_append = $request->get('title_append', ''); |
||
| 58 | if (!empty($title_append)) { |
||
| 59 | $title_append = is_array($title_append) ? implode(' ', $title_append) : $title_append; |
||
| 60 | unset($_GET['title_append']); |
||
| 61 | } |
||
| 62 | |||
| 63 | $values_by_property_id = $request->get('properties', []); |
||
| 64 | if (!is_array($values_by_property_id)) { |
||
| 65 | $values_by_property_id = [$values_by_property_id]; |
||
| 66 | } |
||
| 67 | |||
| 68 | if (Yii::$app->request->isPost && isset($_POST['properties'])) { |
||
| 69 | if (is_array($_POST['properties'])) { |
||
| 70 | foreach ($_POST['properties'] as $key => $value) { |
||
| 71 | if (isset($values_by_property_id[$key])) { |
||
| 72 | $values_by_property_id[$key] = array_unique( |
||
| 73 | ArrayHelper::merge( |
||
| 74 | $values_by_property_id[$key], |
||
| 75 | $value |
||
| 76 | ) |
||
| 77 | ); |
||
| 78 | } else { |
||
| 79 | $values_by_property_id[$key] = array_unique($value); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | $selected_category_ids = $request->get('categories', []); |
||
| 86 | if (!is_array($selected_category_ids)) { |
||
| 87 | $selected_category_ids = [$selected_category_ids]; |
||
| 88 | } |
||
| 89 | |||
| 90 | if (null !== $selected_category_id = $request->get('last_category_id')) { |
||
| 91 | $selected_category_id = intval($selected_category_id); |
||
| 92 | } |
||
| 93 | |||
| 94 | $result = Product::filteredProducts( |
||
| 95 | $category_group_id, |
||
| 96 | $values_by_property_id, |
||
| 97 | $selected_category_id, |
||
| 98 | false, |
||
| 99 | null, |
||
| 100 | true, |
||
| 101 | false |
||
| 102 | ); |
||
| 103 | /** @var Pagination $pages */ |
||
| 104 | $pages = $result['pages']; |
||
| 105 | if (Yii::$app->response->is_prefiltered_page) { |
||
| 106 | $pages->route = '/' . Yii::$app->request->pathInfo; |
||
| 107 | $pages->params = [ |
||
| 108 | |||
| 109 | ]; |
||
| 110 | } |
||
| 111 | $allSorts = $result['allSorts']; |
||
| 112 | $products = $result['products']; |
||
| 113 | |||
| 114 | // throw 404 if we are at filtered page without any products |
||
| 115 | if ( !empty($values_by_property_id) && empty($products)) { |
||
| 116 | throw new NotFoundHttpException(); |
||
| 117 | } |
||
| 118 | |||
| 119 | if (null !== $selected_category = $selected_category_id) { |
||
| 120 | if ($selected_category_id > 0) { |
||
| 121 | if (null !== $selected_category = Category::findById($selected_category_id, null)) { |
||
| 122 | View Code Duplication | if (!empty($selected_category->meta_description)) { |
|
| 123 | $this->view->registerMetaTag( |
||
| 124 | [ |
||
| 125 | 'name' => 'description', |
||
| 126 | 'content' => $selected_category->meta_description, |
||
| 127 | ], |
||
| 128 | 'meta_description' |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | $this->view->title = $selected_category->title; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | } |
||
| 135 | if (is_null($selected_category) || !$selected_category->active) { |
||
| 136 | throw new NotFoundHttpException; |
||
| 137 | } |
||
| 138 | |||
| 139 | if (!empty($title_append)) { |
||
| 140 | $this->view->title .= " " . $title_append; |
||
| 141 | } |
||
| 142 | |||
| 143 | $this->view->blocks['h1'] = $selected_category->h1; |
||
| 144 | $this->view->blocks['announce'] = $selected_category->announce; |
||
| 145 | $this->view->blocks['content'] = $selected_category->content; |
||
| 146 | |||
| 147 | $this->loadDynamicContent($object->id, 'shop/product/list', $request->get()); |
||
| 148 | |||
| 149 | $params = [ |
||
| 150 | 'model' => $selected_category, |
||
| 151 | 'selected_category' => $selected_category, |
||
| 152 | 'selected_category_id' => $selected_category_id, |
||
| 153 | 'selected_category_ids' => $selected_category_ids, |
||
| 154 | 'values_by_property_id' => $values_by_property_id, |
||
| 155 | 'products' => $products, |
||
| 156 | 'object' => $object, |
||
| 157 | 'category_group_id' => $category_group_id, |
||
| 158 | 'pages' => $pages, |
||
| 159 | 'title_append' => $title_append, |
||
| 160 | 'selections' => $request->get(), |
||
| 161 | 'breadcrumbs' => $this->buildBreadcrumbsArray($selected_category, null, $values_by_property_id), |
||
| 162 | 'allSorts' => $allSorts, |
||
| 163 | ]; |
||
| 164 | $viewFile = $this->computeViewFile($selected_category, 'list'); |
||
| 165 | |||
| 166 | if (Yii::$app->request->isAjax) { |
||
| 167 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 168 | |||
| 169 | $content = $this->renderAjax( |
||
| 170 | $viewFile, |
||
| 171 | $params |
||
| 172 | ); |
||
| 173 | $filters = ''; |
||
| 174 | $activeWidgets = ThemeActiveWidgets::getActiveWidgets(); |
||
| 175 | foreach ($activeWidgets as $activeWidget) { |
||
| 176 | if ($activeWidget->widget->widget == Widget::className()) { |
||
| 177 | /** @var ThemeWidgets $widgetModel */ |
||
| 178 | $widgetModel = $activeWidget->widget; |
||
| 179 | /** @var BaseWidget $widgetClassName */ |
||
| 180 | $widgetClassName = $widgetModel->widget; |
||
| 181 | $widgetConfiguration = Json::decode($widgetModel->configuration_json, true); |
||
| 182 | if (!is_array($widgetConfiguration)) { |
||
| 183 | $widgetConfiguration = []; |
||
| 184 | } |
||
| 185 | $activeWidgetConfiguration = Json::decode($activeWidget->configuration_json, true); |
||
| 186 | if (!is_array($activeWidgetConfiguration)) { |
||
| 187 | $activeWidgetConfiguration = []; |
||
| 188 | } |
||
| 189 | $config = ArrayHelper::merge($widgetConfiguration, $activeWidgetConfiguration); |
||
| 190 | $config['themeWidgetModel'] = $widgetModel; |
||
| 191 | $config['partRow'] = $activeWidget->part; |
||
| 192 | $config['activeWidget'] = $activeWidget; |
||
| 193 | $filters = $widgetClassName::widget($config); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | return [ |
||
| 197 | 'content' => $content, |
||
| 198 | 'filters' => $filters, |
||
| 199 | 'title' => $this->view->title, |
||
| 200 | 'url' => Url::to( |
||
| 201 | [ |
||
| 202 | '/shop/product/list', |
||
| 203 | 'last_category_id' => $selected_category_id, |
||
| 204 | 'properties' => $values_by_property_id |
||
| 205 | ] |
||
| 206 | ), |
||
| 207 | ]; |
||
| 208 | } else { |
||
| 209 | return $this->render( |
||
| 210 | $viewFile, |
||
| 211 | $params |
||
| 212 | ); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 460 |
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: