Passed
Push — master ( f80f70...56399a )
by Thomas Mauro
03:05 queued 10s
created

parse_callback_params.php ➔ parse_callback_params()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 4
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\OpenIDClient;
6
7
use Facile\OpenIDClient\Exception\RuntimeException;
8
use function in_array;
9
use function parse_str;
10
use Psr\Http\Message\ServerRequestInterface;
11
use function strtoupper;
12
13
/**
14
 * @param ServerRequestInterface $serverRequest
15
 *
16
 * @return array<string, mixed>
17
 *
18
 * @template P as array{error?: string, error_description?: string, error_uri?: string, response?: string}&array<string, mixed>
19
 * @psalm-return P
20
 */
21
function parse_callback_params(ServerRequestInterface $serverRequest): array
22
{
23 4
    $method = strtoupper($serverRequest->getMethod());
24
25 4
    if (! in_array($method, ['GET', 'POST'], true)) {
26 1
        throw new RuntimeException('Invalid callback method');
27
    }
28
29 3
    if ('POST' === $method) {
30 1
        parse_str((string) $serverRequest->getBody(), $params);
31 2
    } elseif ('' !== $serverRequest->getUri()->getFragment()) {
32 1
        parse_str($serverRequest->getUri()->getFragment(), $params);
33
    } else {
34 1
        parse_str($serverRequest->getUri()->getQuery(), $params);
35
    }
36
37
    /** @var P $params */
38 3
    return $params;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $params returns the type Facile\OpenIDClient\P which is incompatible with the type-hinted return array.
Loading history...
39
}
40