| Conditions | 9 |
| Paths | 8 |
| Total Lines | 35 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 20 |
| CRAP Score | 9 |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 31 | 951 | public static function unescapeAndEncode(string $uri): string |
|
| 32 | { |
||
| 33 | // Optimization: if the URL only includes characters we know will be kept as-is, then just return the URL as-is. |
||
| 34 | 951 | if (\preg_match('/^[A-Za-z0-9~!@#$&*()\-_=+;:,.\/?]+$/', $uri)) { |
|
| 35 | 744 | return $uri; |
|
| 36 | } |
||
| 37 | |||
| 38 | 219 | $result = ''; |
|
| 39 | |||
| 40 | 219 | $chars = \preg_split('//u', $uri, -1, \PREG_SPLIT_NO_EMPTY); |
|
| 41 | |||
| 42 | 219 | if (! \is_array($chars) || ! \mb_check_encoding($uri, 'UTF-8')) { |
|
|
|
|||
| 43 | 3 | throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected'); |
|
| 44 | } |
||
| 45 | |||
| 46 | 216 | $l = \count($chars); |
|
| 47 | 216 | for ($i = 0; $i < $l; $i++) { |
|
| 48 | 201 | $code = $chars[$i]; |
|
| 49 | 201 | if ($code === '%' && $i + 2 < $l) { |
|
| 50 | 99 | if (\preg_match('/^[0-9a-f]{2}$/i', $chars[$i + 1] . $chars[$i + 2]) === 1) { |
|
| 51 | 96 | $result .= '%' . $chars[$i + 1] . $chars[$i + 2]; |
|
| 52 | 96 | $i += 2; |
|
| 53 | 96 | continue; |
|
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | 144 | if (\ord($code) < 128) { |
|
| 58 | 144 | $result .= self::ENCODE_CACHE[\ord($code)]; |
|
| 59 | 144 | continue; |
|
| 60 | } |
||
| 61 | |||
| 62 | 18 | $result .= \rawurlencode($code); |
|
| 63 | } |
||
| 64 | |||
| 65 | 216 | return $result; |
|
| 66 | } |
||
| 68 |