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
Push — master ( 30eaeb...835a4f )
by Ha
01:52
created

Resource::listResourceMetrics()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace OpenStack\Metric\v1\Gnocchi\Models;
4
5
use OpenStack\Common\Resource\OperatorResource;
6
use OpenStack\Common\Resource\Retrievable;
7
use OpenStack\Metric\v1\Gnocchi\Api;
8
9
/**
10
 * @property Api $api
11
 */
12
class Resource extends OperatorResource implements Retrievable
13
{
14
    const RESOURCE_TYPE_GENERIC = 'generic';
15
    const RESOURCE_TYPE_CEPH_ACCOUNT = 'ceph_account';
16
    const RESOURCE_TYPE_HOST = 'host';
17
    const RESOURCE_TYPE_HOST_DISK = 'host_disk';
18
    const RESOURCE_TYPE_HOST_NETWORK_INTERFACE = 'host_network_interface';
19
    const RESOURCE_TYPE_IDENTITY = 'identity';
20
    const RESOURCE_TYPE_IMAGE = 'image';
21
    const RESOURCE_TYPE_INSTANCE = 'instance';
22
    const RESOURCE_TYPE_INSTANCE_DISK = 'instance_disk';
23
    const RESOURCE_TYPE_INSTANCE_NETWORK_INTERFACE = 'instance_network_interface';
24
    const RESOURCE_TYPE_IPMI = 'ipmi';
25
    const RESOURCE_TYPE_NETWORK = 'network';
26
    const RESOURCE_TYPE_STACK = 'stack';
27
    const RESOURCE_TYPE_SWIFT_ACCOUNT = 'swift_account';
28
    const RESOURCE_TYPE_VOLUME = 'volume';
29
30
    /** @var string */
31
    public $createdByUserId;
32
33
    /** @var string */
34
    public $startedAt;
35
36
    /** @var string */
37
    public $displayName;
38
39
    /** @var string */
40
    public $revisionEnd;
41
42
    /** @var string */
43
    public $userId;
44
45
    /** @var string */
46
    public $createdByProjectId;
47
48
    /** @var string */
49
    public $id;
50
51
    /** @var array */
52
    public $metrics;
53
54
    /** @var string */
55
    public $host;
56
57
    /** @var string */
58
    public $imageRef;
59
60
    /** @var string */
61
    public $flavorId;
62
63
    /** @var string */
64
    public $serverGroup;
65
66
    /** @var string */
67
    public $originalResourceId;
68
69
    /** @var string */
70
    public $revisionStart;
71
72
    /** @var string */
73
    public $projectId;
74
75
    /** @var string */
76
    public $type;
77
78
    /** @var string */
79
    public $endedAt;
80
81
    protected $aliases = [
82
        'created_by_user_id'    => 'createdByUserId',
83
        'started_at'            => 'startedAt',
84
        'display_name'          => 'displayName',
85
        'revision_end'          => 'revisionEnd',
86
        'user_id'               => 'userId',
87
        'created_by_project_id' => 'createdByProjectId',
88
        'image_ref'             => 'imageRef',
89
        'flavor_id'             => 'flavorId',
90
        'server_group'          => 'serverGroup',
91
        'original_resource_id'  => 'originalResourceId',
92
        'revision_start'        => 'revisionStart',
93
        'project_id'            => 'projectId',
94
        'ended_at'              => 'endedAt',
95
    ];
96
97
    public function retrieve()
98
    {
99
        $response = $this->execute($this->api->getResource(), ['type' => $this->type, 'id' => $this->id]);
100
        $this->populateFromResponse($response);
101
    }
102
103
    /**
104
     * @param string $metric
105
     *
106
     * @return Metric
107
     */
108
    public function getMetric(string $metric): Metric
109
    {
110
        $response = $this->execute(
111
            $this->api->getResourceMetric(),
112
            [
113
                'resourceId' => $this->id,
114
                'metric'     => $metric,
115
                'type'       => $this->type,
116
            ]
117
        );
118
        $metric = $this->model(Metric::class)->populateFromResponse($response);
119
120
        return $metric;
121
    }
122
123
    /**
124
     * @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::getResourceMetricMeasures}
125
     *
126
     * @return array
127
     */
128
    public function getMetricMeasures(array $options = []): array
129
    {
130
        $options = array_merge(
131
            $options,
132
            [
133
                'resourceId' => $this->id,
134
                'type'       => $this->type,
135
            ]
136
        );
137
138
        $response = $this->execute($this->api->getResourceMetricMeasures(), $options);
139
140
        return \GuzzleHttp\json_decode($response->getBody());
141
    }
142
143
    /**
144
     * @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::getResourceMetrics}
145
     *
146
     * @return \Generator
147
     */
148
    public function listResourceMetrics(array $options = []): \Generator
149
    {
150
        $options['resourceId'] = $this->id;
151
152
        return $this->model(Metric::class)->enumerate($this->api->getResourceMetrics(), $options);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface OpenStack\Common\Resource\ResourceInterface as the method enumerate() does only exist in the following implementations of said interface: OpenStack\BlockStorage\v2\Models\QuotaSet, OpenStack\BlockStorage\v2\Models\Snapshot, OpenStack\BlockStorage\v2\Models\Volume, OpenStack\BlockStorage\v2\Models\VolumeAttachment, OpenStack\BlockStorage\v2\Models\VolumeType, OpenStack\Common\Resource\OperatorResource, OpenStack\Compute\v2\Models\Flavor, OpenStack\Compute\v2\Models\Hypervisor, OpenStack\Compute\v2\Models\HypervisorStatistic, OpenStack\Compute\v2\Models\Image, OpenStack\Compute\v2\Models\Keypair, OpenStack\Compute\v2\Models\QuotaSet, OpenStack\Compute\v2\Models\Server, OpenStack\Identity\v2\Models\Catalog, OpenStack\Identity\v2\Models\Endpoint, OpenStack\Identity\v2\Models\Entry, OpenStack\Identity\v2\Models\Tenant, OpenStack\Identity\v2\Models\Token, OpenStack\Identity\v3\Models\Assignment, OpenStack\Identity\v3\Models\Catalog, OpenStack\Identity\v3\Models\Credential, OpenStack\Identity\v3\Models\Domain, OpenStack\Identity\v3\Models\Endpoint, OpenStack\Identity\v3\Models\Group, OpenStack\Identity\v3\Models\Policy, OpenStack\Identity\v3\Models\Project, OpenStack\Identity\v3\Models\Role, OpenStack\Identity\v3\Models\Service, OpenStack\Identity\v3\Models\Token, OpenStack\Identity\v3\Models\User, OpenStack\Images\v2\Models\Image, OpenStack\Images\v2\Models\Member, OpenStack\Metric\v1\Gnocchi\Models\Metric, OpenStack\Metric\v1\Gnocchi\Models\Resource, OpenStack\Metric\v1\Gnocchi\Models\ResourceType, OpenStack\Networking\v2\...ayer3\Models\FloatingIp, OpenStack\Networking\v2\...ns\Layer3\Models\Router, OpenStack\Networking\v2\...ps\Models\SecurityGroup, OpenStack\Networking\v2\...odels\SecurityGroupRule, OpenStack\Networking\v2\Models\Network, OpenStack\Networking\v2\Models\Port, OpenStack\Networking\v2\Models\Quota, OpenStack\Networking\v2\Models\Subnet, OpenStack\ObjectStore\v1\Models\Account, OpenStack\ObjectStore\v1\Models\Container, OpenStack\ObjectStore\v1\Models\Object.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
153
    }
154
}
155