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