| Conditions | 11 |
| Paths | 193 |
| Total Lines | 36 |
| Code Lines | 20 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 21 |
| CRAP Score | 11 |
| 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 |
||
| 11 | 5 | public static function clean(string $url): string |
|
| 12 | { |
||
| 13 | 5 | $parsed = parse_url($url); |
|
| 14 | |||
| 15 | 5 | if (!$parsed) { |
|
|
|
|||
| 16 | 1 | throw new RuntimeException('Invalid Url'); |
|
| 17 | } |
||
| 18 | |||
| 19 | |||
| 20 | 4 | $path = empty($parsed['scheme']) ? '' : $parsed['scheme'] . '://'; |
|
| 21 | 4 | $path .= rawurlencode($parsed['user'] ?? ''); |
|
| 22 | 4 | $path .= rawurlencode(empty($parsed['pass']) ? '' : ':' . $parsed['pass']); |
|
| 23 | 4 | $path .= !empty($parsed['pass']) || !empty($parsed['pass']) ? '@' : ''; |
|
| 24 | 4 | $path .= $parsed['host'] ?? ''; |
|
| 25 | 4 | $path .= empty($parsed['port']) ? '' : ':' . $parsed['port']; |
|
| 26 | |||
| 27 | 4 | $segments = []; |
|
| 28 | |||
| 29 | 4 | foreach (explode('/', $parsed['path'] ?? '') as $segment) { |
|
| 30 | 4 | $segments[] = urlencode($segment); |
|
| 31 | } |
||
| 32 | |||
| 33 | 4 | $path .= implode('/', $segments); |
|
| 34 | 4 | $query = ''; |
|
| 35 | |||
| 36 | 4 | if (!empty($parsed['query'])) { |
|
| 37 | 1 | parse_str($parsed['query'], $array); |
|
| 38 | |||
| 39 | 1 | if (count($array) > 0) { |
|
| 40 | 1 | $query .= '?' . http_build_query($array); |
|
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | 4 | $query .= empty($parsed['fragment']) ? '' : '#' . rawurlencode($parsed['fragment']); |
|
| 45 | |||
| 46 | 4 | return $path . $query; |
|
| 47 | } |
||
| 49 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.