normalize_webfinger()   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 3
nop 1
dl 0
loc 39
ccs 20
cts 20
cp 1
crap 6
rs 8.9617
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\OpenIdClient;
6
7
use function array_pop;
8
use function explode;
9
use function preg_match;
10
use function preg_replace;
11
use function strpos;
12
use function substr;
13
14
/**
15
 * @param string $input
16
 *
17
 * @return string
18
 */
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