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