Test Failed
Push — master ( a6b51e...5fffdb )
by Gabriel
08:05
created

PersonalAccessGrant::respondToAccessTokenRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
ccs 0
cts 11
cp 0
crap 2
1
<?php
2
3
namespace ByTIC\Hello\Oauth\Grants;
4
5
use DateInterval;
6
use Psr\Http\Message\ServerRequestInterface;
7
use League\OAuth2\Server\Grant\AbstractGrant;
8
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
9
10
/**
11
 * Class PersonalAccessGrant
12
 * @package ByTIC\Hello\Oauth\Grants
13
 */
14
class PersonalAccessGrant extends AbstractGrant
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function respondToAccessTokenRequest(
20
        ServerRequestInterface $request,
21
        ResponseTypeInterface $responseType,
22
        DateInterval $accessTokenTTL
23
    ) {
24
        // Validate request
25
        $client = $this->validateClient($request);
26
        $scopes = $this->validateScopes($this->getRequestParameter('scope', $request));
27
28
        // Finalize the requested scopes
29
        $scopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client);
30
31
        // Issue and persist access token
32
        $accessToken = $this->issueAccessToken(
33
            $accessTokenTTL,
34
            $client,
35
            $this->getRequestParameter('user_id', $request),
36
            $scopes
37
        );
38
        // Inject access token into response type
39
        $responseType->setAccessToken($accessToken);
0 ignored issues
show
Bug introduced by
It seems like $accessToken defined by $this->issueAccessToken(...d', $request), $scopes) on line 32 can be null; however, League\OAuth2\Server\Res...rface::setAccessToken() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
40
        return $responseType;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getIdentifier()
47
    {
48
        return 'personal_access';
49
    }
50
}
51