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.
Passed
Push — master ( 354c7c...907f89 )
by Jamie
05:42
created

Image::downloadData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace OpenStack\Images\v2\Models;
4
5
use function GuzzleHttp\Psr7\uri_for;
6
7
use OpenStack\Common\Resource\AbstractResource;
8
use OpenStack\Common\Resource\Creatable;
9
use OpenStack\Common\Resource\Deletable;
10
use OpenStack\Common\Resource\Listable;
11
use OpenStack\Common\Resource\Retrievable;
12
use OpenStack\Common\Transport\Utils;
13
use OpenStack\Images\v2\JsonPatch;
14
use Psr\Http\Message\StreamInterface;
15
16
/**
17
 * @property \OpenStack\Images\v2\Api $api
18
 */
19
class Image extends AbstractResource implements Creatable, Listable, Retrievable, Deletable
20
{
21
    /** @var string */
22
    public $status;
23
24
    /** @var string */
25
    public $name;
26
27
    /** @var array */
28
    public $tags;
29
30
    /** @var string */
31
    public $containerFormat;
32
33
    /** @var \DateTimeImmutable */
34
    public $createdAt;
35
36
    /** @var string */
37
    public $diskFormat;
38
39
    /** @var \DateTimeImmutable */
40
    public $updatedAt;
41
42
    /** @var string */
43
    public $visibility;
44
45
    /** @var int */
46
    public $minDisk;
47
48
    /** @var bool */
49
    public $protected;
50
51
    /** @var string */
52
    public $id;
53
54
    /** @var \GuzzleHttp\Psr7\Uri */
55
    public $fileUri;
56
57
    /** @var string */
58
    public $checksum;
59
60
    /** @var string */
61
    public $ownerId;
62
63
    /** @var int */
64
    public $size;
65
66
    /** @var int */
67
    public $minRam;
68
69
    /** @var \GuzzleHttp\Psr7\Uri */
70
    public $schemaUri;
71
72
    /** @var int */
73
    public $virtualSize;
74
75
    private $jsonSchema;
76
77
    protected $aliases = [
78
        'container_format' => 'containerFormat',
79
        'created_at'       => 'createdAt',
80
        'disk_format'      => 'diskFormat',
81
        'updated_at'       => 'updatedAt',
82
        'min_disk'         => 'minDisk',
83
        'owner'            => 'ownerId',
84
        'min_ram'          => 'minRam',
85
        'virtual_size'     => 'virtualSize',
86
    ];
87
88 6
    public function populateFromArray(array $data)
89
    {
90 6
        parent::populateFromArray($data);
91
92 6
        $baseUri = $this->getHttpBaseUrl();
93
94 6
        if (isset($data['file'])) {
95 5
            $this->fileUri = Utils::appendPath($baseUri, $data['file']);
96 5
        }
97
98 6
        if (isset($data['schema'])) {
99 5
            $this->schemaUri = Utils::appendPath($baseUri, $data['schema']);
100 6
        }
101 6
    }
102
103 1
    public function create(array $data)
104
    {
105 1
        $response = $this->execute($this->api->postImages(), $data);
106 1
        return $this->populateFromResponse($response);
107
    }
108
109 3
    public function retrieve()
110
    {
111 3
        $response = $this->executeWithState($this->api->getImage());
112 3
        return $this->populateFromResponse($response);
113
    }
114
115 2
    private function getSchema()
116
    {
117 2
        if (null === $this->jsonSchema) {
118 2
            $response = $this->execute($this->api->getImageSchema());
119 2
            $this->jsonSchema = new Schema(Utils::jsonDecode($response, false));
120 2
        }
121
122 2
        return $this->jsonSchema;
123
    }
124
125 2
    public function update(array $data)
126
    {
127
        // retrieve latest state so we can accurately produce a diff
128 2
        $this->retrieve();
129
130 2
        $schema = $this->getSchema();
131 2
        $data   = (object) $data;
132
133
        // formulate src and des structures
134 2
        $des = $schema->normalizeObject($data, $this->aliases);
135 2
        $src = $schema->normalizeObject($this, $this->aliases);
136
137
        // validate user input
138 2
        $schema->validate($des);
139 2
        if (!$schema->isValid()) {
140 1
            throw new \RuntimeException($schema->getErrorString());
141
        }
142
143
        // formulate diff
144 1
        $patch = new JsonPatch();
145 1
        $diff = $patch->disableRestrictedPropRemovals($patch->diff($src, $des), $schema->getPropertyPaths());
146 1
        $json = json_encode($diff, JSON_UNESCAPED_SLASHES);
147
148
        // execute patch operation
149 1
        $response = $this->execute($this->api->patchImage(), [
150 1
            'id'          => $this->id,
151 1
            'patchDoc'    => $json,
152
            'contentType' => 'application/openstack-images-v2.1-json-patch'
153 1
        ]);
154
155 1
        return $this->populateFromResponse($response);
156
    }
157
158 1
    public function delete()
159
    {
160 1
        $this->executeWithState($this->api->deleteImage());
161 1
    }
162
163 1
    public function deactivate()
164
    {
165 1
        $this->executeWithState($this->api->deactivateImage());
166 1
    }
167
168 1
    public function reactivate()
169
    {
170 1
        $this->executeWithState($this->api->reactivateImage());
171 1
    }
172
173 1
    public function uploadData(StreamInterface $stream)
174
    {
175 1
        $this->execute($this->api->postImageData(), [
176 1
            'id'          => $this->id,
177 1
            'data'        => $stream,
178 1
            'contentType' => 'application/octet-stream',
179 1
        ]);
180 1
    }
181
182 1
    public function downloadData()
183
    {
184 1
        $response = $this->executeWithState($this->api->getImageData());
185 1
        return $response->getBody();
186
    }
187
188 1
    public function addMember($memberId)
189
    {
190 1
        return $this->model(Member::class, ['imageId' => $this->id, 'id' => $memberId])->create([]);
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 create() does only exist in the following implementations of said interface: OpenStack\BlockStorage\v2\Models\Snapshot, OpenStack\BlockStorage\v2\Models\Volume, OpenStack\BlockStorage\v2\Models\VolumeType, OpenStack\Compute\v2\Models\Server, 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\Networking\v2\Models\Network, OpenStack\Networking\v2\Models\Subnet, 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...
191
    }
192
193 1
    public function listMembers()
194
    {
195 1
        return $this->model(Member::class)->enumerate($this->api->getImageMembers(), ['imageId' => $this->id]);
196
    }
197
198 1
    public function getMember($memberId)
199
    {
200 1
        return $this->model(Member::class, ['imageId' => $this->id, 'id' => $memberId]);
201
    }
202
}