| Conditions | 23 |
| Paths | 5880 |
| Total Lines | 79 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 declare(strict_types=1); |
||
| 32 | function smarty_modifier_rewrite_url($url, $insert_param, $param_value = null, $remove_params_arr = '') |
||
| 33 | { |
||
| 34 | //parse $insert_param if it is a query string |
||
| 35 | if (preg_match('/.+=([\w%,-])*/', $insert_param)) { |
||
| 36 | parse_str($insert_param, $insert_arr); |
||
| 37 | $insert_param = array_keys($insert_arr); |
||
| 38 | $param_value = array_values($insert_arr); |
||
| 39 | } |
||
| 40 | |||
| 41 | //split $url and parse into array |
||
| 42 | if (preg_match('/\w+\.\w+/', $url)) { |
||
| 43 | //assume full url |
||
| 44 | /** @var array $newurl_arr */ |
||
| 45 | $newurl_arr = parse_url($url); |
||
| 46 | $newurl = ''; |
||
| 47 | if (isset($newurl_arr['scheme'])) { |
||
| 48 | $newurl = $newurl_arr['scheme'] . '://'; |
||
| 49 | } |
||
| 50 | if (isset($newurl_arr['username']) && isset($newurl_arr['password'])) { |
||
| 51 | $newurl .= $newurl_arr['username'] . ':' . $newurl_arr['password'] . '@'; |
||
| 52 | } elseif (isset($newurl_arr['username'])) { |
||
| 53 | $newurl .= $newurl_arr['username'] . '@'; |
||
| 54 | } |
||
| 55 | if (isset($newurl_arr['host'])) { |
||
| 56 | $newurl .= $newurl_arr['host']; |
||
| 57 | } |
||
| 58 | if (isset($newurl_arr['port'])) { |
||
| 59 | $newurl .= ':' . $newurl_arr['port']; |
||
| 60 | } |
||
| 61 | if (isset($newurl_arr['path'])) { |
||
| 62 | $newurl .= $newurl_arr['path']; |
||
| 63 | } |
||
| 64 | $newurl .= '?'; |
||
| 65 | if (isset($newurl_arr['query'])) { |
||
| 66 | parse_str($newurl_arr['query'], $url_arr); |
||
| 67 | } |
||
| 68 | } else { |
||
| 69 | //assume just query string |
||
| 70 | if (preg_match('/#/', $url)) { |
||
| 71 | /** @var array $temp_arr */ |
||
| 72 | $temp_arr = explode('#', $url); |
||
| 73 | $newurl_arr['fragment'] = $temp_arr[1]; |
||
|
|
|||
| 74 | $url = $temp_arr[0]; |
||
| 75 | } |
||
| 76 | $newurl = ''; |
||
| 77 | parse_str($url, $url_arr); |
||
| 78 | } |
||
| 79 | |||
| 80 | //remove params from array |
||
| 81 | if (isset($remove_params_arr) && ('' != $remove_params_arr)) { |
||
| 82 | !is_array($remove_params_arr) ? $remove_params_arr = explode(',', $remove_params_arr) : ''; |
||
| 83 | foreach ($remove_params_arr as $param) { |
||
| 84 | unset($url_arr[$param]); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | //add current param to array, params separated by semi-colon |
||
| 89 | if (isset($insert_param) && ('' != $insert_param)) { |
||
| 90 | !is_array($insert_param) ? $insert_param = explode(',', $insert_param) : ''; |
||
| 91 | !is_array($param_value) ? $param_value = explode(',', $param_value) : ''; |
||
| 92 | for ($i = 0, $size = count($param_value); $i < $size; ++$i) { |
||
| 93 | if ('' != trim($param_value[$i])) { |
||
| 94 | $url_arr[trim($insert_param[$i])] = trim($param_value[$i]); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | // Remove any duplicate array elements |
||
| 100 | $url_arr = array_unique($url_arr); |
||
| 101 | |||
| 102 | //assemble the url string from the array |
||
| 103 | $newurl .= http_build_query($url_arr, '', '&'); |
||
| 104 | |||
| 105 | //attach anchor fragment to end of url |
||
| 106 | if (isset($newurl_arr['fragment'])) { |
||
| 107 | $newurl .= '#' . $newurl_arr['fragment']; |
||
| 108 | } |
||
| 109 | |||
| 110 | return $newurl; |
||
| 111 | } |
||
| 164 |