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 (#128)
by Ha
01:57
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
16
    /** @var string */
17
    public $createdByUserId;
18
19
    /** @var string */
20
    public $startedAt;
21
22
    /** @var string */
23
    public $displayName;
24
25
    /** @var string */
26
    public $revisionEnd;
27
28
    /** @var string */
29
    public $userId;
30
31
    /** @var string */
32
    public $createdByProjectId;
33
34
    /** @var string */
35
    public $id;
36
37
    /** @var array */
38
    public $metrics;
39
40
    /** @var string */
41
    public $host;
42
43
    /** @var string */
44
    public $imageRef;
45
46
    /** @var string */
47
    public $flavorId;
48
49
    /** @var string */
50
    public $serverGroup;
51
52
    /** @var string */
53
    public $originalResourceId;
54
55
    /** @var string */
56
    public $revisionStart;
57
58
    /** @var string */
59
    public $projectId;
60
61
    /** @var string */
62
    public $type;
63
64
    /** @var string */
65
    public $endedAt;
66
67
    protected $aliases = [
68
        'created_by_user_id'    => 'createdByUserId',
69
        'started_at'            => 'startedAt',
70
        'display_name'          => 'displayName',
71
        'revision_end'          => 'revisionEnd',
72
        'user_id'               => 'userId',
73
        'created_by_project_id' => 'createdByProjectId',
74
        'image_ref'             => 'imageRef',
75
        'flavor_id'             => 'flavorId',
76
        'server_group'          => 'serverGroup',
77
        'original_resource_id'  => 'originalResourceId',
78
        'revision_start'        => 'revisionStart',
79
        'project_id'            => 'projectId',
80
        'ended_at'              => 'endedAt',
81
    ];
82
83
    public function retrieve()
84
    {
85
        $response = $this->execute($this->api->getResource(), ['type' => $this->type, 'id' => $this->id]);
86
        $this->populateFromResponse($response);
87
    }
88
89
90
    /**
91
     * @param string $metric
92
     *
93
     * @return Metric
94
     */
95
    public function getMetric(string $metric): Metric
96
    {
97
        $response = $this->execute(
98
            $this->api->getResourceMetric(),
99
            [
100
                'resourceId' => $this->id,
101
                'metric'     => $metric,
102
                'type'       => $this->type,
103
            ]
104
        );
105
        $metric = $this->model(Metric::class)->populateFromResponse($response);
106
107
        return $metric;
108
    }
109
110
    /**
111
     * @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::getResourceMetricMeasures}
112
     *
113
     * @return array
114
     */
115
    public function getMetricMeasures(array $options = []): array
116
    {
117
        $options = array_merge(
118
            $options,
119
            [
120
                'resourceId' => $this->id,
121
                'type'       => $this->type,
122
            ]
123
        );
124
125
        $response = $this->execute($this->api->getResourceMetricMeasures(), $options);
126
127
        return \GuzzleHttp\json_decode($response->getBody());
128
    }
129
130
    /**
131
     * @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::getResourceMetrics}
132
     *
133
     * @return \Generator
134
     */
135
    public function listResourceMetrics(array $options = []): \Generator
136
    {
137
        $options['resourceId'] = $this->id;
138
139
        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\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...
140
    }
141
}
142