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

UrlHelper::parseTokenFromQueryString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
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