| Conditions | 13 |
| Paths | 1282 |
| Total Lines | 43 |
| Code Lines | 22 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 20 | public static function setGetVar(string $key, string $value, string $url): string |
||
| 21 | { |
||
| 22 | if (!self::is_absolute_url($url)) { |
||
| 23 | $isRelative = true; |
||
| 24 | $url = 'http://dummy.com/' . ltrim($url, '/'); |
||
| 25 | } else { |
||
| 26 | $isRelative = false; |
||
| 27 | } |
||
| 28 | |||
| 29 | // try to parse uri |
||
| 30 | $parts = parse_url($url); |
||
| 31 | if (empty($parts)) { |
||
| 32 | throw new InvalidArgumentException("Can't parse URL: " . $url); |
||
| 33 | } |
||
| 34 | |||
| 35 | // Parse params and add new variable |
||
| 36 | $params = []; |
||
| 37 | if (isset($parts['query'])) { |
||
| 38 | parse_str($parts['query'], $params); |
||
| 39 | } |
||
| 40 | $params[$key] = $value; |
||
| 41 | |||
| 42 | // Generate URI segments and formatting |
||
| 43 | $scheme = (array_key_exists('scheme', $parts)) ? $parts['scheme'] : 'http'; |
||
| 44 | $user = (array_key_exists('user', $parts)) ? $parts['user'] : ''; |
||
| 45 | $port = (array_key_exists('port', $parts) && $parts['port']) ? ':' . $parts['port'] : ''; |
||
| 46 | |||
| 47 | if ($user != '') { |
||
| 48 | // format in either user:[email protected] or [email protected] |
||
| 49 | $user .= (array_key_exists('pass', $parts) && $parts['pass']) ? ':' . $parts['pass'] . '@' : ''; |
||
| 50 | } |
||
| 51 | |||
| 52 | // handle URL params which are existing / new |
||
| 53 | $params = ($params) ? '?' . http_build_query($params) : ''; |
||
| 54 | |||
| 55 | // Recompile URI segments |
||
| 56 | $newUri = $scheme . '://' . $user . $parts['host'] . $port . $parts['path'] . $params; |
||
| 57 | |||
| 58 | if ($isRelative) { |
||
| 59 | return str_replace('http://dummy.com/', '', $newUri); |
||
| 60 | } |
||
| 61 | |||
| 62 | return $newUri; |
||
| 63 | } |
||
| 96 |