Completed
Push — master ( 32eaad...e7d4f0 )
by Andy
03:23
created

AbstractProvider::serviceContainerKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace SocialiteProviders\Manager\OAuth2;
3
4
use GuzzleHttp\ClientInterface;
5
use Laravel\Socialite\Two\InvalidStateException;
6
use SocialiteProviders\Manager\SocialiteWasCalled;
7
8
abstract class AbstractProvider extends \Laravel\Socialite\Two\AbstractProvider
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $credentialsResponseBody;
14
15
    public static function serviceContainerKey($providerName)
16
    {
17 5
        return SocialiteWasCalled::SERVICE_CONTAINER_PREFIX . $providerName;
18
    }
19 5
20 2
    /**
21
     * @return \SocialiteProviders\Manager\OAuth2\User
22
     */
23 3
    public function user()
24 3
    {
25 3
        if ($this->hasInvalidState()) {
26
            throw new InvalidStateException;
27 3
        }
28
29 3
        $user = $this->mapUserToObject($this->getUserByToken(
30 3
            $token = $this->getAccessToken($this->getCode())
0 ignored issues
show
Bug introduced by
It seems like $this->getCode() targeting Laravel\Socialite\Two\AbstractProvider::getCode() can also be of type array; however, SocialiteProviders\Manag...vider::getAccessToken() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
31
        ));
32
33
        $user->setToken($token);
34
35
        if ($user instanceof User) {
36
            return $user->setAccessTokenResponseBody($this->credentialsResponseBody);
37
        }
38
39
        return $user;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $user; (Laravel\Socialite\User) is incompatible with the return type declared by the interface Laravel\Socialite\Contracts\Provider::user of type Laravel\Socialite\Contracts\User.

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...
40
    }
41
42 3
    /**
43
     * Get the access token for the given code.
44 3
     *
45
     * @param  string $code
46 3
     * @return string
47 3
     */
48 3
    public function getAccessToken($code)
49 3
    {
50
        $postKey = (version_compare(ClientInterface::VERSION, '6') === 1) ? 'form_params' : 'body';
51 3
52
        $response = $this->getHttpClient()->post($this->getTokenUrl(), [
53 3
            'headers' => ['Accept' => 'application/json'],
54
            $postKey => $this->getTokenFields($code),
55
        ]);
56
57
        $this->credentialsResponseBody = json_decode($response->getBody(), true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($response->getBody(), true) of type * is incompatible with the declared type array of property $credentialsResponseBody.

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...
58
59
        return $this->parseAccessToken($response->getBody());
60
    }
61
}
62