Conditions | 29 |
Paths | 140 |
Total Lines | 155 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
136 | public function parseRequest($manager, $request) |
||
137 | { |
||
138 | Yii::beginProfile("ObjectRule::parseRequest"); |
||
139 | |||
140 | $url = $request->getPathInfo(); |
||
141 | if (empty($url)) { |
||
142 | Yii::endProfile("ObjectRule::parseRequest"); |
||
143 | return false; |
||
144 | } |
||
145 | |||
146 | $cacheKey = 'ObjectRule:'.$url.':'.Json::encode($request->getQueryParams()); |
||
147 | $result = Yii::$app->cache->get($cacheKey); |
||
148 | if ($result !== false) { |
||
149 | Yii::endProfile("ObjectRule::parseRequest"); |
||
150 | $this->defineBlocksTitleAndView($result); |
||
151 | return $result['result']; |
||
152 | } |
||
153 | |||
154 | $prefilteredPage = PrefilteredPages::getActiveByUrl($url); |
||
155 | |||
156 | if ($prefilteredPage !== null) { |
||
157 | $params = [ |
||
158 | 'properties' => Json::decode($prefilteredPage['params']) |
||
159 | ]; |
||
160 | $category = Category::findById($prefilteredPage['last_category_id']); |
||
161 | if ($category === null) { |
||
162 | throw new NotFoundHttpException; |
||
163 | } |
||
164 | $params['category_group_id'] = $category->category_group_id; |
||
165 | $params['last_category_id'] = $category->id; |
||
166 | $data = ['blocks'=>[]]; |
||
167 | if (!empty($prefilteredPage['title'])) { |
||
168 | $data['title'] = $prefilteredPage['title']; |
||
169 | } |
||
170 | if (!empty($prefilteredPage['meta_description'])) { |
||
171 | $data['meta_description'] = $prefilteredPage['meta_description']; |
||
172 | } |
||
173 | $blocks = [ |
||
174 | 'content', |
||
175 | 'announce', |
||
176 | 'breadcrumbs_label', |
||
177 | 'h1', |
||
178 | ]; |
||
179 | |||
180 | foreach ($blocks as $block_name) { |
||
181 | |||
182 | if (!empty($prefilteredPage[$block_name])) { |
||
183 | $data['blocks'][$block_name] = $prefilteredPage[$block_name]; |
||
184 | } |
||
185 | } |
||
186 | $data['is_prefiltered_page'] = true; |
||
187 | |||
188 | if ($prefilteredPage['view_id']>0) { |
||
189 | $data['viewId'] = $prefilteredPage['view_id']; |
||
190 | } |
||
191 | |||
192 | $data['result'] = [ |
||
193 | 'shop/product/list', |
||
194 | $params |
||
195 | ]; |
||
196 | $this->defineBlocksTitleAndView($data); |
||
197 | Yii::$app->cache->set( |
||
198 | $cacheKey, |
||
199 | $data, |
||
200 | 86400, |
||
201 | new TagDependency([ |
||
202 | 'tags' => [ |
||
203 | ActiveRecordHelper::getObjectTag(PrefilteredPages::className(), $prefilteredPage['id']), |
||
204 | ActiveRecordHelper::getObjectTag(Category::className(), $category->id), |
||
205 | ] |
||
206 | ]) |
||
207 | ); |
||
208 | return $data['result']; |
||
209 | } |
||
210 | |||
211 | $routes = ObjectRule::getRoutes(); |
||
212 | $cacheTags = []; |
||
213 | foreach ($routes as $model) { |
||
214 | /** @var UrlPart[] $handlers */ |
||
215 | $handlers = []; |
||
216 | $object = BaseObject::findById($model->object_id); |
||
217 | foreach ($model->template as $t) { |
||
218 | $handler = Yii::createObject($t); |
||
219 | $handler->object = $object; |
||
220 | $handlers[] = $handler; |
||
221 | } |
||
222 | $url_parts = []; |
||
223 | $parameters = []; |
||
224 | $next_part = $url; |
||
225 | foreach ($handlers as $handler) { |
||
226 | if (empty($next_part)) { |
||
227 | //break; |
||
228 | } |
||
229 | $result = $handler->getNextPart($url, $next_part, $url_parts); |
||
230 | if ($result !== false && is_object($result) === true) { |
||
231 | $parameters = ArrayHelper::merge($parameters, $result->parameters); |
||
232 | $cacheTags = ArrayHelper::merge($cacheTags, $result->cacheTags); |
||
233 | // удалим leading slash |
||
234 | $next_part = ltrim($result->rest_part, '/'); |
||
235 | $url_parts[] = $result; |
||
236 | } elseif ($result === false && $handler->optional===false) { |
||
237 | continue; |
||
238 | } |
||
239 | } |
||
240 | if (count($url_parts)==0) { |
||
241 | continue; |
||
242 | } |
||
243 | |||
244 | // в конце удачного парсинга next_part должен остаться пустым |
||
245 | if (empty($next_part)) { |
||
246 | $resultForCache = ['result'=>[$model->route, $parameters]]; |
||
247 | if (isset($_POST['properties'], $parameters['properties'])) { |
||
248 | |||
249 | foreach ($_POST['properties'] as $key=>$value) { |
||
250 | if (isset($parameters['properties'][$key])) { |
||
251 | $parameters['properties'][$key] = array_unique(ArrayHelper::merge($parameters['properties'][$key], $value)); |
||
252 | } else { |
||
253 | $parameters['properties'][$key] = array_unique($value); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | |||
258 | } elseif (isset($_POST['properties'])) { |
||
259 | $parameters['properties'] = $_POST['properties']; |
||
260 | } |
||
261 | Yii::endProfile("ObjectRule::parseRequest"); |
||
262 | if (isset($parameters['properties'])) { |
||
263 | foreach ($parameters['properties'] as $key => $values) { |
||
264 | foreach ($parameters['properties'][$key] as $index => $value) { |
||
265 | if ($value === '') { |
||
266 | unset($parameters['properties'][$key][$index]); |
||
267 | } |
||
268 | } |
||
269 | if (count($parameters['properties'][$key]) === 0) { |
||
270 | unset($parameters['properties'][$key]); |
||
271 | } |
||
272 | } |
||
273 | } |
||
274 | $result = [$model->route, $parameters]; |
||
275 | |||
276 | Yii::$app->cache->set( |
||
277 | $cacheKey, |
||
278 | $resultForCache, |
||
279 | 86400, |
||
280 | new TagDependency([ |
||
281 | 'tags' => $cacheTags, |
||
282 | ]) |
||
283 | ); |
||
284 | |||
285 | return $result; |
||
286 | } |
||
287 | } |
||
288 | Yii::endProfile("ObjectRule::parseRequest"); |
||
289 | return false; // this rule does not apply |
||
290 | } |
||
291 | |||
317 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.