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

Image::getAliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace OpenStack\Compute\v2\Models;
4
5
use OpenStack\Common\Resource\Alias;
6
use OpenStack\Common\Resource\OperatorResource;
7
use OpenStack\Common\Resource\Deletable;
8
use OpenStack\Common\Resource\HasMetadata;
9
use OpenStack\Common\Resource\Listable;
10
use OpenStack\Common\Resource\Retrievable;
11
use OpenStack\Common\Transport\Utils;
12
use Psr\Http\Message\ResponseInterface;
13
14
/**
15
 * Represents a Compute v2 Image
16
 *
17
 * @property \OpenStack\Compute\v2\Api $api
18
 */
19
class Image extends OperatorResource implements Listable, Retrievable, Deletable, HasMetadata
20
{
21
    /** @var string */
22
    public $id;
23
24
    /** @var array */
25
    public $links;
26
27
    /** @var array */
28
    public $metadata;
29
30
    /** @var int */
31
    public $minDisk;
32
33
    /** @var int */
34
    public $minRam;
35
36
    /** @var string */
37
    public $name;
38
39
    /** @var string */
40
    public $progress;
41
42
    /** @var string */
43
    public $status;
44
45
    /** @var \DateTimeImmutable */
46
    public $created;
47
48
    /** @var \DateTimeImmutable */
49
    public $updated;
50
51
    /**
52
     * @var array
53
     */
54 1
    protected $aliases = [];
55
56 1
    protected $resourceKey = 'image';
57 1
    protected $resourcesKey = 'images';
58 1
59
    /**
60
     * @inheritdoc
61
     */
62
    protected function getAliases()
63 1
    {
64
        $aliases = parent::getAliases();
65 1
        $aliases['created'] = new Alias('created', \DateTimeImmutable::class);
66 1
        $aliases['updated'] = new Alias('updated', \DateTimeImmutable::class);
67
        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...
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73 1
    public function retrieve()
74
    {
75 1
        $response = $this->execute($this->api->getImage(), ['id' => (string)$this->id]);
76 1
        $this->populateFromResponse($response);
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82
    public function delete()
83
    {
84
        $this->execute($this->api->deleteImage(), ['id' => (string)$this->id]);
85
    }
86
87 1
    /**
88
     * Retrieves metadata from the API.
89 1
     *
90 1
     * @return array
91
     */
92
    public function getMetadata(): array
93
    {
94
        $response = $this->execute($this->api->getImageMetadata(), ['id' => $this->id]);
95
        return $this->parseMetadata($response);
96
    }
97
98
    /**
99
     * Resets all the metadata for this image with the values provided. All existing metadata keys
100
     * will either be replaced or removed.
101
     *
102 1
     * @param array $metadata {@see \OpenStack\Compute\v2\Api::putImageMetadata}
103
     */
104 1
    public function resetMetadata(array $metadata)
105 1
    {
106
        $response = $this->execute($this->api->putImageMetadata(), ['id' => $this->id, 'metadata' => $metadata]);
107
        $this->metadata = $this->parseMetadata($response);
108
    }
109
110
    /**
111
     * Merges the existing metadata for the image with the values provided. Any existing keys
112
     * referenced in the user options will be replaced with the user's new values. All other
113
     * existing keys will remain unaffected.
114
     *
115 1
     * @param array $metadata {@see \OpenStack\Compute\v2\Api::postImageMetadata}
116
     */
117 1
    public function mergeMetadata(array $metadata)
118 1
    {
119
        $response = $this->execute($this->api->postImageMetadata(), ['id' => $this->id, 'metadata' => $metadata]);
120
        $this->metadata = $this->parseMetadata($response);
121
    }
122
123
    /**
124
     * Retrieve the value for a specific metadata key.
125
     *
126 1
     * @param string $key {@see \OpenStack\Compute\v2\Api::getImageMetadataKey}
127
     *
128 1
     * @return mixed
129 1
     */
130 View Code Duplication
    public function getMetadataItem(string $key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        $response = $this->execute($this->api->getImageMetadataKey(), ['id' => $this->id, 'key' => $key]);
133
        $value = $this->parseMetadata($response)[$key];
134
        $this->metadata[$key] = $value;
135
        return $value;
136
    }
137
138
    /**
139
     * Remove a specific metadata key.
140
     *
141
     * @param string $key {@see \OpenStack\Compute\v2\Api::deleteImageMetadataKey}
142
     */
143 View Code Duplication
    public function deleteMetadataItem(string $key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145
        if (isset($this->metadata[$key])) {
146
            unset($this->metadata[$key]);
147
        }
148
149
        $this->execute($this->api->deleteImageMetadataKey(), ['id' => $this->id, 'key' => $key]);
150
    }
151
152
    public function parseMetadata(ResponseInterface $response): array
153
    {
154
        return Utils::jsonDecode($response)['metadata'];
155
    }
156
}
157