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

Snapshot::getAliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace OpenStack\BlockStorage\v2\Models;
4
5
use OpenStack\Common\Resource\Alias;
6
use OpenStack\Common\Resource\OperatorResource;
7
use OpenStack\Common\Resource\Creatable;
8
use OpenStack\Common\Resource\Deletable;
9
use OpenStack\Common\Resource\HasMetadata;
10
use OpenStack\Common\Resource\HasWaiterTrait;
11
use OpenStack\Common\Resource\Listable;
12
use OpenStack\Common\Resource\Retrievable;
13
use OpenStack\Common\Resource\Updateable;
14
use OpenStack\Common\Transport\Utils;
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * @property \OpenStack\BlockStorage\v2\Api $api
19
 */
20
class Snapshot extends OperatorResource implements Listable, Creatable, Updateable, Deletable, Retrievable, HasMetadata
21
{
22
    use HasWaiterTrait;
23
24
    /** @var string */
25
    public $id;
26
27
    /** @var string */
28
    public $name;
29
30
    /** @var string */
31
    public $status;
32
33
    /** @var string */
34
    public $description;
35
36
    /** @var \DateTimeImmutable */
37
    public $createdAt;
38
39
    /** @var array */
40
    public $metadata = [];
41
42
    /** @var string */
43
    public $volumeId;
44
45
    /** @var int */
46
    public $size;
47
48
    protected $resourceKey = 'snapshot';
49
    protected $resourcesKey = 'snapshots';
50
    protected $markerKey = 'id';
51
52
    protected $aliases = [
53
        'volume_id'  => 'volumeId',
54
    ];
55
56 2
    /**
57
     * @inheritdoc
58 2
     */
59 2
    protected function getAliases()
60 2
    {
61
        $aliases = parent::getAliases();
62
        $aliases['created_at'] = new Alias('createdAt', \DateTimeImmutable::class);
63 1
        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...
64
    }
65 1
66 1
    public function populateFromResponse(ResponseInterface $response): self
67
    {
68
        parent::populateFromResponse($response);
69
        $this->metadata = $this->parseMetadata($response);
70
        return $this;
71
    }
72
73
    public function retrieve()
74 1
    {
75
        $response = $this->executeWithState($this->api->getSnapshot());
76 1
        $this->populateFromResponse($response);
77 1
    }
78
79
    /**
80 1
     * @param array $userOptions {@see \OpenStack\BlockStorage\v2\Api::postSnapshots}
81
     *
82 1
     * @return Creatable
83 1
     */
84
    public function create(array $userOptions): Creatable
85 1
    {
86
        $response = $this->execute($this->api->postSnapshots(), $userOptions);
87 1
        return $this->populateFromResponse($response);
88 1
    }
89
90 2
    public function update()
91
    {
92 2
        $this->executeWithState($this->api->putSnapshot());
93 2
    }
94 2
95
    public function delete()
96
    {
97 1
        $this->executeWithState($this->api->deleteSnapshot());
98
    }
99 1
100 1
    public function getMetadata(): array
101 1
    {
102 1
        $response = $this->executeWithState($this->api->getSnapshotMetadata());
103
        $this->metadata = $this->parseMetadata($response);
104 1
        return $this->metadata;
105
    }
106 1
107 1
    public function mergeMetadata(array $metadata)
108 1
    {
109
        $this->getMetadata();
110 4
        $this->metadata = array_merge($this->metadata, $metadata);
111
        $this->executeWithState($this->api->putSnapshotMetadata());
112 4
    }
113 4
114
    public function resetMetadata(array $metadata)
115
    {
116
        $this->metadata = $metadata;
117
        $this->executeWithState($this->api->putSnapshotMetadata());
118
    }
119
120
    public function parseMetadata(ResponseInterface $response): array
121
    {
122
        $json = Utils::jsonDecode($response);
123
        return isset($json['metadata']) ? $json['metadata'] : [];
124
    }
125
}
126