Passed
Push — main ( c32779...b25da4 )
by Peter
04:38
created

UrlHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 33
ccs 14
cts 15
cp 0.9333
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parseQueryTokenFromResponse() 0 9 2
A parseTokenFromQueryString() 0 9 2
A parseQueryTokenFromUrl() 0 9 2
1
<?php
2
namespace Pelmered\LaravelHttpOAuthHelper;
3
4
use Illuminate\Http\Client\Response;
5
6
class UrlHelper
7
{
8 5
    public static function parseQueryTokenFromResponse(Response $response, string $queryKey = 'token'): ?string
9
    {
10 5
        $uri = $response->effectiveUri();
11
12 5
        if (! $uri) {
0 ignored issues
show
introduced by
$uri is of type Psr\Http\Message\UriInterface, thus it always evaluated to true.
Loading history...
13
            return null;
14
        }
15
16 5
        return self::parseTokenFromQueryString($response->effectiveUri()?->getQuery(), $queryKey);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::parseTokenF...>getQuery(), $queryKey) could return the type array which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
17
    }
18
19 6
    public static function parseQueryTokenFromUrl(string $url, string $queryKey = 'token'): ?string
20
    {
21 6
        $queryString = parse_url($url, PHP_URL_QUERY);
22
23 6
        if (! $queryString) {
24 1
            return null;
25
        }
26
27 5
        return self::parseTokenFromQueryString($queryString, $queryKey);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::parseTokenF...queryString, $queryKey) could return the type array which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
28
    }
29
30 13
    public static function parseTokenFromQueryString(string $queryString, string $queryKey = 'token'): null|string|array
31
    {
32 13
        parse_str($queryString, $output);
33
34 13
        if (! isset($output[$queryKey])) {
35 4
            return null;
36
        }
37
38 9
        return $output[$queryKey] ?? null;
39
    }
40
}
41