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

Resource::getMetricMeasures()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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
     * @param string $metric
91
     *
92
     * @return Metric
93
     */
94
    public function getMetric(string $metric): Metric
95
    {
96
        $response = $this->execute(
97
            $this->api->getResourceMetric(),
98
            [
99
                'resourceId' => $this->id,
100
                'metric'     => $metric,
101
                'type'       => $this->type,
102
            ]
103
        );
104
        $metric = $this->model(Metric::class)->populateFromResponse($response);
105
106
        return $metric;
107
    }
108
109
    /**
110
     * @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::getResourceMetricMeasures}
111
     *
112
     * @return array
113
     */
114
    public function getMetricMeasures(array $options = []): array
115
    {
116
        $options = array_merge(
117
            $options,
118
            [
119
                'resourceId' => $this->id,
120
                'type'       => $this->type,
121
            ]
122
        );
123
124
        $response = $this->execute($this->api->getResourceMetricMeasures(), $options);
125
126
        return \GuzzleHttp\json_decode($response->getBody());
127
    }
128
129
    /**
130
     * @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::getResourceMetrics}
131
     *
132
     * @return \Generator
133
     */
134
    public function listResourceMetrics(array $options = []): \Generator
135
    {
136
        $options['resourceId'] = $this->id;
137
138
        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...
139
    }
140
}
141