RefreshTokenController::issueAccessToken()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 0
cts 4
cp 0
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace CodexShaper\OAuth2\Server\Http\Controllers;
4
5
use CodexShaper\OAuth2\Server\Http\Requests\ServerRequest;
6
use CodexShaper\OAuth2\Server\Http\Responses\ServerResponse;
7
use CodexShaper\OAuth2\Server\Manager;
8
9 View Code Duplication
class RefreshTokenController
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
10
{
11
    /**
12
     * The server manager.
13
     *
14
     * @var \CodexShaper\OAuth2\Server\Manager
15
     */
16
    protected $manager;
17
18
    /**
19
     * The authorization server.
20
     *
21
     * @var \League\OAuth2\Server\AuthorizationServer
22
     */
23
    protected $server;
24
25
    /**
26
     * The psr7 server request.
27
     *
28
     * @var \CodexShaper\OAuth2\Server\Http\Requests\ServerRequest
29
     */
30
    protected $request;
31
32
    /**
33
     * The psr7 server response.
34
     *
35
     * @var \CodexShaper\OAuth2\Server\Http\Responses\ServerResponse
36
     */
37
    protected $response;
38
39
    /**
40
     * Create a new refresh token controller instance.
41
     *
42
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
43
     */
44
    public function __construct()
45
    {
46
        $this->manager = new Manager();
47
        $this->server = $this->manager->makeAuthorizationServer();
48
        $this->request = ServerRequest::getPsrServerRequest();
49
        $this->response = ServerResponse::getPsrServerResponse();
0 ignored issues
show
Documentation Bug introduced by
It seems like \CodexShaper\OAuth2\Serv...:getPsrServerResponse() of type object<Nyholm\Psr7\Response> is incompatible with the declared type object<CodexShaper\OAuth...sponses\ServerResponse> of property $response.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
    }
51
52
    /**
53
     * Make access token.
54
     *
55
     * @return \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface
56
     */
57
    public function issueAccessToken()
58
    {
59
        try {
60
61
            // Try to respond to the request
62
            return $this->server->respondToAccessTokenRequest($this->request, $this->response)->getBody();
0 ignored issues
show
Documentation introduced by
$this->request is of type object<CodexShaper\OAuth...Requests\ServerRequest>, but the function expects a object<Psr\Http\Message\ServerRequestInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->response is of type object<CodexShaper\OAuth...sponses\ServerResponse>, but the function expects a object<Psr\Http\Message\ResponseInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug Best Practice introduced by
The return type of return $this->server->re...->response)->getBody(); (Psr\Http\Message\StreamInterface) is incompatible with the return type documented by CodexShaper\OAuth2\Serve...oller::issueAccessToken of type League\OAuth2\Server\Res...s\ResponseTypeInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
63
        } catch (OAuthServerException $exception) {
0 ignored issues
show
Bug introduced by
The class CodexShaper\OAuth2\Serve...rs\OAuthServerException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
64
65
            // All instances of OAuthServerException can be formatted into a HTTP response
66
            return $exception->generateHttpResponse($this->response);
67
        }
68
    }
69
}
70