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

Volume::getAliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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