| Conditions | 51 |
| Paths | 22 |
| Total Lines | 200 |
| Code Lines | 118 |
| 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 |
||
| 218 | public function rewrite($link) |
||
| 219 | { |
||
| 220 | if (!$this->config->get('config_seo_url')) { |
||
| 221 | return $link; |
||
| 222 | } |
||
| 223 | |||
| 224 | $seo_url = ''; |
||
| 225 | |||
| 226 | $url_info = parse_url(str_replace('&', '&', $link)); |
||
| 227 | |||
| 228 | $data = array(); |
||
| 229 | |||
| 230 | parse_str($url_info['query'], $data); |
||
| 231 | |||
| 232 | $route = $data['route']; |
||
| 233 | |||
| 234 | unset($data['route']); |
||
| 235 | |||
| 236 | $url_info['scheme'] = $this->url_sheme; |
||
| 237 | |||
| 238 | foreach ($this->ssl_routes as $ssl_route) { |
||
| 239 | if (stripos($route, $ssl_route) === 0) { |
||
| 240 | $url_info['scheme'] = 'https'; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | switch ($route) { |
||
| 245 | |||
| 246 | case 'product/product': |
||
| 247 | |||
| 248 | if (isset($data['product_id'])) { |
||
| 249 | $tmp = $data; |
||
| 250 | $data = array(); |
||
| 251 | |||
| 252 | if ($this->config->get('config_seo_url_include_path')) { |
||
| 253 | $data['path'] = $this->getPathByProduct($tmp['product_id']); |
||
| 254 | if (!$data['path']) { |
||
| 255 | return $link; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | $data['product_id'] = $tmp['product_id']; |
||
| 260 | |||
| 261 | // --- add valide routes |
||
| 262 | foreach ($this->valide_routes as $valide_route) { |
||
| 263 | if (isset($tmp[$valide_route])) { |
||
| 264 | $data[$valide_route] = $tmp[$valide_route]; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | // --- add valide routes |
||
| 268 | } |
||
| 269 | |||
| 270 | break; |
||
| 271 | |||
| 272 | case 'product/category': |
||
| 273 | |||
| 274 | if (isset($data['path'])) { |
||
| 275 | $category = explode('_', $data['path']); |
||
| 276 | $category = end($category); |
||
| 277 | $data['path'] = $this->getPathByCategory($category); |
||
| 278 | |||
| 279 | if (!$data['path']) { |
||
| 280 | return $link; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | break; |
||
| 285 | |||
| 286 | case 'blog/article': |
||
| 287 | |||
| 288 | if (isset($data['article_id'])) { |
||
| 289 | $tmp = $data; |
||
| 290 | $data = array(); |
||
| 291 | if ($this->config->get('config_seo_url_include_path')) { |
||
| 292 | $data['blog_category_id'] = $this->getPathByArticle($tmp['article_id']); |
||
| 293 | if (!$data['blog_category_id']) { |
||
| 294 | return $link; |
||
| 295 | } |
||
| 296 | } |
||
| 297 | $data['article_id'] = $tmp['article_id']; |
||
| 298 | } |
||
| 299 | |||
| 300 | break; |
||
| 301 | |||
| 302 | case 'blog/category': |
||
| 303 | |||
| 304 | if (isset($data['blog_category_id'])) { |
||
| 305 | $blog_category_id = explode('_', $data['blog_category_id']); |
||
| 306 | $blog_category_id = end($blog_category_id); |
||
| 307 | $data['blog_category_id'] = $this->getPathByBlogCategory($blog_category_id); |
||
| 308 | |||
| 309 | if (!$data['blog_category_id']) { |
||
| 310 | return $link; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | break; |
||
| 315 | |||
| 316 | case 'product/product/review': |
||
| 317 | case 'information/information/info': |
||
| 318 | case 'information/information/agree': |
||
| 319 | return $link; |
||
| 320 | break; |
||
| 321 | default: |
||
| 322 | break; |
||
| 323 | } |
||
| 324 | |||
| 325 | $link = $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : ''); |
||
| 326 | |||
| 327 | //fix subfolder ($url_info['path']) |
||
| 328 | $link .= $url_info['path'] . '?route=' . $route . (count($data) ? '&' . urldecode(http_build_query($data, '', '&')) : ''); |
||
| 329 | |||
| 330 | $queries = array(); |
||
| 331 | |||
| 332 | foreach ($data as $key => $value) { |
||
| 333 | switch ($key) { |
||
| 334 | case 'product_id': |
||
| 335 | case 'article_id': |
||
| 336 | case 'manufacturer_id': |
||
| 337 | case 'category_id': |
||
| 338 | case 'information_id': |
||
| 339 | case 'order_id': |
||
| 340 | case 'download_id': |
||
| 341 | case 'search': |
||
| 342 | case 'sub_category': |
||
| 343 | case 'description': |
||
| 344 | $queries[] = $key . '=' . $value; |
||
| 345 | unset($data[$key]); |
||
| 346 | $postfix = 1; |
||
| 347 | break; |
||
| 348 | case 'path': |
||
| 349 | $categories = explode('_', $value); |
||
| 350 | foreach ($categories as $category) { |
||
| 351 | $queries[] = 'category_id=' . $category; |
||
| 352 | } |
||
| 353 | unset($data[$key]); |
||
| 354 | break; |
||
| 355 | case 'blog_category_id': |
||
| 356 | $blog_categories = explode('_', $value); |
||
| 357 | foreach ($blog_categories as $blog_category) { |
||
| 358 | $queries[] = 'blog_category_id=' . $blog_category; |
||
| 359 | } |
||
| 360 | unset($data[$key]); |
||
| 361 | break; |
||
| 362 | default: |
||
| 363 | break; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | if (empty($queries)) { |
||
| 368 | $queries[] = $route; |
||
| 369 | } |
||
| 370 | |||
| 371 | $rows = array(); |
||
| 372 | |||
| 373 | foreach ($queries as $query) { |
||
| 374 | if (isset($this->cache_data['queries'][$query])) { |
||
| 375 | $rows[] = array('query' => $query, 'keyword' => $this->cache_data['queries'][$query]); |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | if (count($rows) == count($queries)) { |
||
| 380 | $aliases = array(); |
||
| 381 | |||
| 382 | foreach ($rows as $row) { |
||
| 383 | $aliases[$row['query']] = $row['keyword']; |
||
| 384 | } |
||
| 385 | |||
| 386 | foreach ($queries as $query) { |
||
| 387 | $seo_url .= '/' . rawurlencode($aliases[$query]); |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | if ($seo_url == '') { |
||
| 392 | return $link; |
||
| 393 | } |
||
| 394 | |||
| 395 | $seo_url = trim($seo_url, '/'); |
||
| 396 | |||
| 397 | //fix subfolder |
||
| 398 | $path = rtrim($url_info['path'], '/index.php'); |
||
| 399 | |||
| 400 | $seo_url = $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . $path . '/' . $seo_url; |
||
| 401 | |||
| 402 | if (isset($postfix)) { |
||
| 403 | $seo_url .= trim($this->config->get('config_seo_url_postfix')); |
||
| 404 | } else { |
||
| 405 | $seo_url .= '/'; |
||
| 406 | } |
||
| 407 | |||
| 408 | //fix subfolder |
||
| 409 | if (substr($seo_url, -2) == '//') { |
||
| 410 | $seo_url = substr($seo_url, 0, -1); |
||
| 411 | } |
||
| 412 | |||
| 413 | if (count($data)) { |
||
| 414 | $seo_url .= '?' . urldecode(http_build_query($data, '', '&')); |
||
| 415 | } |
||
| 416 | |||
| 417 | return $seo_url; |
||
| 418 | } |
||
| 638 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.