GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#159)
by Roman
01:55
created

Token::getAliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace OpenStack\Identity\v3\Models;
4
5
use OpenStack\Common\Resource\Alias;
6
use Psr\Http\Message\ResponseInterface;
7
use OpenStack\Common\Resource\OperatorResource;
8
use OpenStack\Common\Resource\Creatable;
9
use OpenStack\Common\Resource\Retrievable;
10
11
/**
12
 * @property \OpenStack\Identity\v3\Api $api
13
 */
14
class Token extends OperatorResource implements Creatable, Retrievable, \OpenStack\Common\Auth\Token
15
{
16
    /** @var array */
17
    public $methods;
18
19
    /** @var []Role */
20
    public $roles;
21
22
    /** @var \DateTimeImmutable */
23
    public $expires;
24
25
    /** @var Project */
26
    public $project;
27
28
    /** @var Catalog */
29
    public $catalog;
30
31
    /** @var mixed */
32
    public $extras;
33
34
    /** @var User */
35
    public $user;
36
37
    /** @var \DateTimeImmutable */
38
    public $issued;
39
40
    /** @var string */
41
    public $id;
42
43
    protected $resourceKey = 'token';
44
    protected $resourcesKey = 'tokens';
45
46
    protected $aliases = [];
47
48
    /**
49
     * @inheritdoc
50
     */
51
    protected function getAliases()
52
    {
53 5
        $aliases = parent::getAliases();
54
        $aliases['roles'] = new Alias('roles', Role::class, true);
55 5
        $aliases['expires_at'] = new Alias('expires', \DateTimeImmutable::class);
56
        $aliases['project'] = new Alias('project', Project::class);
57 5
        $aliases['catalog'] = new Alias('catalog', Catalog::class);
58
        $aliases['user'] = new Alias('user', User::class);
59 5
        $aliases['issued_at'] = new Alias('issued', \DateTimeImmutable::class);
60
        return $aliases;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $aliases; (array<*,OpenStack\Common\Resource\Alias>) is incompatible with the return type of the parent method OpenStack\Common\Resourc...actResource::getAliases of type OpenStack\Common\Resource\Alias[].

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...
61
    }
62
63
    /**
64
     * {@inheritDoc}
65 1
     */
66
    public function populateFromResponse(ResponseInterface $response)
67 1
    {
68
        parent::populateFromResponse($response);
69
        $this->id = $response->getHeaderLine('X-Subject-Token');
70
        return $this;
71
    }
72
73 1
    /**
74
     * @return string
75 1
     */
76
    public function getId(): string
77
    {
78
        return $this->id;
79
    }
80
81 1
    /**
82
     * @return bool TRUE if the token has expired (and is invalid); FALSE otherwise.
83 1
     */
84
    public function hasExpired(): bool
85 1
    {
86 1
        return $this->expires <= new \DateTimeImmutable('now', $this->expires->getTimezone());
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function retrieve()
93 10
    {
94
        $response = $this->execute($this->api->getTokens(), ['tokenId' => $this->id]);
95 10
        $this->populateFromResponse($response);
96 4
    }
97 4
98 1
    /**
99
     * {@inheritDoc}
100
     *
101 1
     * @param array $data {@see \OpenStack\Identity\v3\Api::postTokens}
102 1
     */
103
    public function create(array $data): Creatable
104 9
    {
105 1
        if (isset($data['user'])) {
106 1
            $data['methods'] = ['password'];
107 5
            if (!isset($data['user']['id']) && empty($data['user']['domain'])) {
108
                throw new \InvalidArgumentException(
109
                    'When authenticating with a username, you must also provide either the domain name or domain ID to '
110 4
                    . 'which the user belongs to. Alternatively, if you provide a user ID instead, you do not need to '
111 4
                    . 'provide domain information.'
112
                );
113
            }
114
        } elseif (isset($data['tokenId'])) {
115
            $data['methods'] = ['token'];
116
        } else {
117
            throw new \InvalidArgumentException('Either a user or token must be provided.');
118
        }
119
120
        $response = $this->execute($this->api->postTokens(), $data);
121
        return $this->populateFromResponse($response);
122
    }
123
}
124