Passed
Push — master ( e62745...2f64c5 )
by Thomas Mauro
02:54
created

normalize_webfinger()   B

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
/**
8
 * @param string $input
9
 *
10
 * @return string
11
 */
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