| Conditions | 8 |
| Paths | 128 |
| Total Lines | 60 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 38 |
| CRAP Score | 8 |
| 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 |
||
| 43 | 5 | public static function fromGlobal(): UriInterface |
|
| 44 | { |
||
| 45 | 5 | $server = $_SERVER ?? []; |
|
| 46 | |||
| 47 | 5 | $uri = ''; |
|
| 48 | 5 | $user = ''; |
|
| 49 | 5 | $host = ''; |
|
| 50 | 5 | $pass = ''; |
|
| 51 | 5 | $path = ''; |
|
| 52 | 5 | $port = ''; |
|
| 53 | 5 | $query = ''; |
|
| 54 | 5 | $scheme = ''; |
|
| 55 | |||
| 56 | 5 | $uriComponents = [ |
|
| 57 | 5 | 'user' => 'PHP_AUTH_USER', |
|
| 58 | 5 | 'host' => 'HTTP_HOST', |
|
| 59 | 5 | 'pass' => 'PHP_AUTH_PW', |
|
| 60 | 5 | 'path' => 'REQUEST_URI', |
|
| 61 | 5 | 'port' => 'SERVER_PORT', |
|
| 62 | 5 | 'query' => 'QUERY_STRING', |
|
| 63 | 5 | 'scheme' => 'REQUEST_SCHEME', |
|
| 64 | 5 | ]; |
|
| 65 | |||
| 66 | 5 | foreach ($uriComponents as $key => $value) { |
|
| 67 | 5 | ${$key} = $server[$value] ?? ''; |
|
| 68 | } |
||
| 69 | |||
| 70 | 5 | $userInfo = $user; |
|
| 71 | |||
| 72 | 5 | if ($pass) { |
|
| 73 | 2 | $userInfo .= ':' . $pass; |
|
| 74 | } |
||
| 75 | |||
| 76 | 5 | $authority = ''; |
|
| 77 | |||
| 78 | 5 | if ($userInfo) { |
|
| 79 | 2 | $authority .= $userInfo . '@'; |
|
| 80 | } |
||
| 81 | |||
| 82 | 5 | $authority .= $host; |
|
| 83 | |||
| 84 | 5 | if ($port) { |
|
| 85 | 5 | $authority .= ':' . $port; |
|
| 86 | } |
||
| 87 | |||
| 88 | 5 | if ($scheme) { |
|
| 89 | 5 | $uri .= $scheme . ':'; |
|
| 90 | } |
||
| 91 | |||
| 92 | 5 | if ($authority) { |
|
| 93 | 5 | $uri .= '//' . $authority; |
|
| 94 | } |
||
| 95 | |||
| 96 | 5 | $uri .= '/' . ltrim($path, '/'); |
|
| 97 | |||
| 98 | 5 | if ($query) { |
|
| 99 | 2 | $uri .= '?' . $query; |
|
| 100 | } |
||
| 101 | |||
| 102 | 5 | return new Uri($uri); |
|
| 103 | } |
||
| 115 |