coniadev /
boiler
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Conia\Boiler; |
||
| 6 | |||
| 7 | use Conia\Boiler\Exception\RuntimeException; |
||
| 8 | |||
| 9 | class Url |
||
| 10 | { |
||
| 11 | 5 | public static function clean(string $url): string |
|
| 12 | { |
||
| 13 | 5 | $parsed = parse_url($url); |
|
| 14 | |||
| 15 | 5 | if (!$parsed) { |
|
|
0 ignored issues
–
show
|
|||
| 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 | } |
||
| 48 | } |
||
| 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.