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