Conditions | 6 |
Paths | 3 |
Total Lines | 39 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Tests | 20 |
CRAP Score | 6 |
Changes | 0 |
1 | <?php |
||
19 | function normalize_webfinger(string $input): string |
||
20 | { |
||
21 | $hasScheme = static function (string $resource): bool { |
||
22 | 5 | if (false !== strpos($resource, '://')) { |
|
23 | 1 | return true; |
|
24 | } |
||
25 | |||
26 | 4 | $authority = explode('#', (string) preg_replace('/(\/|\?)/', '#', $resource))[0]; |
|
27 | |||
28 | 4 | if (false === ($index = strpos($authority, ':'))) { |
|
29 | 2 | return false; |
|
30 | } |
||
31 | |||
32 | 2 | $hostOrPort = substr($resource, $index + 1); |
|
33 | |||
34 | 2 | return ! (bool) preg_match('/^\d+$/', $hostOrPort); |
|
35 | 5 | }; |
|
36 | |||
37 | $acctSchemeAssumed = static function (string $input): bool { |
||
38 | 3 | if (false === strpos($input, '@')) { |
|
39 | 1 | return false; |
|
40 | } |
||
41 | |||
42 | 2 | $parts = explode('@', $input); |
|
43 | /** @var string $host */ |
||
44 | 2 | $host = array_pop($parts); |
|
45 | |||
46 | 2 | return ! (bool) preg_match('/[:\/?]+/', $host); |
|
47 | 5 | }; |
|
48 | |||
49 | 5 | if ($hasScheme($input)) { |
|
50 | 2 | $output = $input; |
|
51 | 3 | } elseif ($acctSchemeAssumed($input)) { |
|
52 | 2 | $output = 'acct:' . $input; |
|
53 | } else { |
||
54 | 1 | $output = 'https://' . $input; |
|
55 | } |
||
56 | |||
57 | 5 | return explode('#', $output)[0]; |
|
58 | } |
||
59 |