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:49
created

Volume   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 12.88 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 4
dl 17
loc 132
rs 10
ccs 32
cts 32
cp 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlias() 17 17 2
A populateFromResponse() 0 6 1
A retrieve() 0 5 1
A create() 0 5 1
A update() 0 5 1
A delete() 0 4 1
A getMetadata() 0 6 1
A mergeMetadata() 0 6 1
A resetMetadata() 0 5 1
A parseMetadata() 0 5 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    /**
70 3
     * @inheritdoc
71
     */
72 3 View Code Duplication
    protected static function getAlias(): Alias
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...
73 3
    {
74 3
        $alias = parent::getAlias();
75
76
        if (!$alias->hasAliases(self::class)) {
77 1
            $alias
78
                ->add(self::class, 'availability_zone', 'availabilityZone')
79 1
                ->add(self::class, 'source_volid', 'sourceVolumeId')
80 1
                ->add(self::class, 'snapshot_id', 'snapshotId')
81
                ->add(self::class, 'volume_type', 'volumeTypeName')
82
                ->add(self::class, 'os-vol-tenant-attr:tenant_id', 'tenantId')
83
                ->add(self::class, 'os-vol-host-attr:host', 'host')
84
                ->add(self::class, 'created_at', 'createdAt', \DateTimeImmutable::class);
85
        }
86
87
        return $alias;
88 1
    }
89
90 1
    public function populateFromResponse(ResponseInterface $response): self
91 1
    {
92
        parent::populateFromResponse($response);
93
        $this->metadata = $this->parseMetadata($response);
94
        return $this;
95
    }
96
97 1
    public function retrieve()
98
    {
99 1
        $response = $this->executeWithState($this->api->getVolume());
100 1
        $this->populateFromResponse($response);
101
    }
102
103 1
    /**
104
     * @param array $userOptions {@see \OpenStack\BlockStorage\v2\Api::postVolumes}
105 1
     *
106 1
     * @return Creatable
107
     */
108 1
    public function create(array $userOptions): Creatable
109
    {
110 1
        $response = $this->execute($this->api->postVolumes(), $userOptions);
111 1
        return $this->populateFromResponse($response);
112 1
    }
113
114
    public function update()
115 1
    {
116
        $response = $this->executeWithState($this->api->putVolume());
117 1
        $this->populateFromResponse($response);
118 1
    }
119 1
120 1
    public function delete()
121
    {
122 1
        $this->executeWithState($this->api->deleteVolume());
123
    }
124 1
125 1
    public function getMetadata(): array
126 1
    {
127
        $response = $this->executeWithState($this->api->getVolumeMetadata());
128 4
        $this->metadata = $this->parseMetadata($response);
129
        return $this->metadata;
130 4
    }
131 4
132
    public function mergeMetadata(array $metadata)
133
    {
134
        $this->getMetadata();
135
        $this->metadata = array_merge($this->metadata, $metadata);
136
        $this->executeWithState($this->api->putVolumeMetadata());
137
    }
138
139
    public function resetMetadata(array $metadata)
140
    {
141
        $this->metadata = $metadata;
142
        $this->executeWithState($this->api->putVolumeMetadata());
143
    }
144
145
    public function parseMetadata(ResponseInterface $response): array
146
    {
147
        $json = Utils::jsonDecode($response);
148
        return isset($json['metadata']) ? $json['metadata'] : [];
149
    }
150
}
151