Completed
Pull Request — master (#38)
by Brian
36:12 queued 26:18
created

Server::getTokenCredentials()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
rs 8.8571
cc 3
eloc 15
nc 3
nop 3
1
<?php
2
3
namespace SocialiteProviders\Manager\OAuth1;
4
5
use GuzzleHttp\Exception\BadResponseException;
6
use League\OAuth1\Client\Credentials\TemporaryCredentials;
7
use League\OAuth1\Client\Credentials\TokenCredentials;
8
9
abstract class Server extends \League\OAuth1\Client\Server\Server
10
{
11
    /**
12
     * Retrieves token credentials by passing in the temporary credentials,
13
     * the temporary credentials identifier as passed back by the server
14
     * and finally the verifier code.
15
     *
16
     * @param TemporaryCredentials $temporaryCredentials
17
     * @param string               $temporaryIdentifier
18
     * @param string               $verifier
19
     *
20
     * @return TokenCredentials
21
     */
22
    public function getTokenCredentials(TemporaryCredentials $temporaryCredentials, $temporaryIdentifier, $verifier)
23
    {
24
        if ($temporaryIdentifier !== $temporaryCredentials->getIdentifier()) {
25
            throw new \InvalidArgumentException(
26
                'Temporary identifier passed back by server does not match that of stored temporary credentials.
27
                Potential man-in-the-middle.'
28
            );
29
        }
30
31
        $uri = $this->urlTokenCredentials();
32
        $bodyParameters = array('oauth_verifier' => $verifier);
33
34
        $client = $this->createHttpClient();
35
36
        $headers = $this->getHeaders($temporaryCredentials, 'POST', $uri, $bodyParameters);
37
38
        try {
39
            $response = $client->post($uri, $headers, $bodyParameters)->send();
40
        } catch (BadResponseException $e) {
41
            return $this->handleTokenCredentialsBadResponse($e);
0 ignored issues
show
Documentation introduced by
$e is of type object<GuzzleHttp\Exception\BadResponseException>, but the function expects a object<Guzzle\Http\Excep...n\BadResponseException>.

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...
42
        }
43
44
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('tokenCrede... $response->getBody()); (array) is incompatible with the return type of the parent method League\OAuth1\Client\Ser...er::getTokenCredentials of type League\OAuth1\Client\Credentials\TokenCredentials.

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...
45
            'tokenCredentials' => $this->createTokenCredentials($response->getBody()),
46
            'credentialsResponseBody' => $response->getBody(),
47
        ];
48
    }
49
}
50