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 ( 307cce...27531d )
by Jamie
04:17 queued 15s
created

Service::createUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace OpenStack\Identity\v3;
4
5
use GuzzleHttp\ClientInterface;
6
use OpenStack\Common\Auth\IdentityService;
7
use OpenStack\Common\Error\BadResponseError;
8
use OpenStack\Common\Service\AbstractService;
9
use OpenStack\Identity\v3\Models;
10
11
/**
12
 * Represents the Keystone v3 service.
13
 *
14
 * @property \OpenStack\Identity\v3\Api $api
15
 */
16
class Service extends AbstractService implements IdentityService
17
{
18 5
    public static function factory(ClientInterface $client)
19
    {
20 5
        return new static($client, new Api());
21
    }
22
23
    /**
24
     * Authenticates credentials, giving back a token and a base URL for the service.
25
     *
26
     * @param  array $options {@see \OpenStack\Identity\v3\Api::postTokens}
27
     *
28
     * @return array Returns a {@see Models\Token} as the first element, a string base URL as the second
29
     */
30 6
    public function authenticate(array $options)
31
    {
32 6
        $authOptions = array_intersect_key($options, $this->api->postTokens()['params']);
33
34 6
        $token = $this->generateToken($authOptions);
35
36 2
        $name      = $options['catalogName'];
37 2
        $type      = $options['catalogType'];
38 2
        $region    = $options['region'];
39 2
        $interface = isset($options['interface']) ? $options['interface'] : Enum::INTERFACE_PUBLIC;
40
41 2
        if ($baseUrl = $token->catalog->getServiceUrl($name, $type, $region, $interface)) {
42 1
            return [$token, $baseUrl];
43
        }
44
45 1
        throw new \RuntimeException(sprintf("No service found with type [%s] name [%s] region [%s] interface [%s]",
46 1
            $type, $name, $region, $interface));
47
    }
48
49
    /**
50
     * Generates a new authentication token
51
     *
52
     * @param array $options {@see \OpenStack\Identity\v3\Api::postTokens}
53
     *
54
     * @return Models\Token
55
     */
56 8
    public function generateToken(array $options)
57
    {
58 8
        return $this->model(Models\Token::class)->create($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 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\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...
59
    }
60
61
    /**
62
     * Retrieves a token object and populates its unique identifier object. This operation will not perform a GET or
63
     * HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API.
64
     *
65
     * @param string $id The unique ID of the token to retrieve
66
     *
67
     * @return Models\Token
68
     */
69 1
    public function getToken($id)
70
    {
71 1
        return $this->model(Models\Token::class, ['id' => $id]);
72
    }
73
74
    /**
75
     * Validates a token, identified by its ID, and returns TRUE if its valid, FALSE if not.
76
     *
77
     * @param string $id The unique ID of the token
78
     *
79
     * @return bool
80
     */
81 2
    public function validateToken($id)
82
    {
83
        try {
84 2
            $this->execute($this->api->headTokens(), ['tokenId' => $id]);
85 1
            return true;
86 1
        } catch (BadResponseError $e) {
87 1
            return false;
88
        }
89
    }
90
91
    /**
92
     * Revokes a token, identified by its ID. After this operation completes, users will not be able to use this token
93
     * again for authentication.
94
     *
95
     * @param string $id The unique ID of the token
96
     *
97
     * @return Models\Token
98
     */
99 1
    public function revokeToken($id)
100
    {
101 1
        $this->execute($this->api->deleteTokens(), ['tokenId' => $id]);
102 1
    }
103
104
    /**
105
     * Creates a new service according to the provided options.
106
     *
107
     * @param array $options {@see \OpenStack\Identity\v3\Api::postServices}
108
     *
109
     * @return Models\Service
110
     */
111 1
    public function createService(array $options)
112
    {
113 1
        return $this->model(Models\Service::class)->create($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 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\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...
114
    }
115
116
    /**
117
     * Returns a generator which will yield a collection of service objects. The elements which generators yield can be
118
     * accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you
119
     * will need to use retrieve() to pull in the full state of the remote resource from the API.
120
     *
121
     * @param array $options {@see \OpenStack\Identity\v3\Api::getServices}
122
     *
123
     * @return \Generator
124
     */
125 1
    public function listServices(array $options = [])
126
    {
127 1
        return $this->model(Models\Service::class)->enumerate($this->api->getServices(), $options);
128
    }
129
130
    /**
131
     * Retrieves a service object and populates its unique identifier object. This operation will not perform a GET or
132
     * HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API.
133
     *
134
     * @param string $id The unique ID of the service
135
     *
136
     * @return Models\Service
137
     */
138 1
    public function getService($id)
139
    {
140 1
        return $this->model(Models\Service::class, ['id' => $id]);
141
    }
142
143
    /**
144
     * Creates a new endpoint according to the provided options.
145
     *
146
     * @param array $options {@see \OpenStack\Identity\v3\Api::postEndpoints}
147
     *
148
     * @return Models\Endpoint
149
     */
150 2
    public function createEndpoint(array $options)
151
    {
152 2
        return $this->model(Models\Endpoint::class)->create($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 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\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...
153
    }
154
155
    /**
156
     * Retrieves an endpoint object and populates its unique identifier object. This operation will not perform a GET or
157
     * HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API.
158
     *
159
     * @param string $id The unique ID of the service
160
     *
161
     * @return Models\Endpoint
162
     */
163 1
    public function getEndpoint($id)
164
    {
165 1
        return $this->model(Models\Endpoint::class, ['id' => $id]);
166
    }
167
168
    /**
169
     * Returns a generator which will yield a collection of endpoint objects. The elements which generators yield can be
170
     * accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you
171
     * will need to use retrieve() to pull in the full state of the remote resource from the API.
172
     *
173
     * @param array $options {@see \OpenStack\Identity\v3\Api::getEndpoints}
174
     *
175
     * @return \Generator
176
     */
177 1
    public function listEndpoints(array $options = [])
178
    {
179 1
        return $this->model(Models\Endpoint::class)->enumerate($this->api->getEndpoints(), $options);
180
    }
181
182
    /**
183
     * Creates a new domain according to the provided options.
184
     *
185
     * @param array $options {@see \OpenStack\Identity\v3\Api::postDomains}
186
     *
187
     * @return Models\Domain
188
     */
189 1
    public function createDomain(array $options)
190
    {
191 1
        return $this->model(Models\Domain::class)->create($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 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\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...
192
    }
193
194
    /**
195
     * Returns a generator which will yield a collection of domain objects. The elements which generators yield can be
196
     * accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you
197
     * will need to use retrieve() to pull in the full state of the remote resource from the API.
198
     *
199
     * @param array $options {@see \OpenStack\Identity\v3\Api::getDomains}
200
     *
201
     * @return \Generator
202
     */
203 1
    public function listDomains(array $options = [])
204
    {
205 1
        return $this->model(Models\Domain::class)->enumerate($this->api->getDomains(), $options);
206
    }
207
208
    /**
209
     * Retrieves a domain object and populates its unique identifier object. This operation will not perform a GET or
210
     * HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API.
211
     *
212
     * @param string $id The unique ID of the domain
213
     *
214
     * @return Models\Domain
215
     */
216 1
    public function getDomain($id)
217
    {
218 1
        return $this->model(Models\Domain::class, ['id' => $id]);
219
    }
220
221
    /**
222
     * Creates a new project according to the provided options.
223
     *
224
     * @param array $options {@see \OpenStack\Identity\v3\Api::postProjects}
225
     *
226
     * @return Models\Project
227
     */
228 1
    public function createProject(array $options)
229
    {
230 1
        return $this->model(Models\Project::class)->create($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 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\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...
231
    }
232
233
    /**
234
     * Returns a generator which will yield a collection of project objects. The elements which generators yield can be
235
     * accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you
236
     * will need to use retrieve() to pull in the full state of the remote resource from the API.
237
     *
238
     * @param array $options {@see \OpenStack\Identity\v3\Api::getProjects}
239
     *
240
     * @return \Generator
241
     */
242 1
    public function listProjects(array $options = [])
243
    {
244 1
        return $this->model(Models\Project::class)->enumerate($this->api->getProjects(), $options);
245
    }
246
247
    /**
248
     * Retrieves a project object and populates its unique identifier object. This operation will not perform a GET or
249
     * HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API.
250
     *
251
     * @param string $id The unique ID of the project
252
     *
253
     * @return Models\Project
254
     */
255 1
    public function getProject($id)
256
    {
257 1
        return $this->model(Models\Project::class, ['id' => $id]);
258
    }
259
260
    /**
261
     * Creates a new user according to the provided options.
262
     *
263
     * @param array $options {@see \OpenStack\Identity\v3\Api::postUsers}
264
     *
265
     * @return Models\User
266
     */
267 1
    public function createUser(array $options)
268
    {
269 1
        return $this->model(Models\User::class)->create($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 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\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...
270
    }
271
272
    /**
273
     * Returns a generator which will yield a collection of user objects. The elements which generators yield can be
274
     * accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you
275
     * will need to use retrieve() to pull in the full state of the remote resource from the API.
276
     *
277
     * @param array $options {@see \OpenStack\Identity\v3\Api::getUsers}
278
     *
279
     * @return \Generator
280
     */
281 1
    public function listUsers(array $options = [])
282
    {
283 1
        return $this->model(Models\User::class)->enumerate($this->api->getUsers(), $options);
284
    }
285
286
    /**
287
     * Retrieves a user object and populates its unique identifier object. This operation will not perform a GET or
288
     * HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API.
289
     *
290
     * @param string $id The unique ID of the user
291
     *
292
     * @return Models\User
293
     */
294 1
    public function getUser($id)
295
    {
296 1
        return $this->model(Models\User::class, ['id' => $id]);
297
    }
298
299
    /**
300
     * Creates a new group according to the provided options.
301
     *
302
     * @param array $options {@see \OpenStack\Identity\v3\Api::postGroups}
303
     *
304
     * @return Models\Group
305
     */
306 1
    public function createGroup(array $options)
307
    {
308 1
        return $this->model(Models\Group::class)->create($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 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\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...
309
    }
310
311
    /**
312
     * Returns a generator which will yield a collection of group objects. The elements which generators yield can be
313
     * accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you
314
     * will need to use retrieve() to pull in the full state of the remote resource from the API.
315
     *
316
     * @param array $options {@see \OpenStack\Identity\v3\Api::getGroups}
317
     *
318
     * @return \Generator
319
     */
320 1
    public function listGroups(array $options = [])
321
    {
322 1
        return $this->model(Models\Group::class)->enumerate($this->api->getGroups(), $options);
323
    }
324
325
    /**
326
     * Retrieves a group object and populates its unique identifier object. This operation will not perform a GET or
327
     * HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API.
328
     *
329
     * @param string $id The unique ID of the group
330
     *
331
     * @return Models\Group
332
     */
333 1
    public function getGroup($id)
334
    {
335 1
        return $this->model(Models\Group::class, ['id' => $id]);
336
    }
337
338
    /**
339
     * Creates a new credential according to the provided options.
340
     *
341
     * @param array $options {@see \OpenStack\Identity\v3\Api::postCredentials}
342
     *
343
     * @return Models\Credential
344
     */
345 1
    public function createCredential(array $options)
346
    {
347 1
        return $this->model(Models\Credential::class)->create($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 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\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...
348
    }
349
350
    /**
351
     * Returns a generator which will yield a collection of credential objects. The elements which generators yield can
352
     * be accessed using a foreach loop. Often the API will not return the full state of the resource in collections;
353
     * you will need to use retrieve() to pull in the full state of the remote resource from the API.
354
     *
355
     * @return \Generator
356
     */
357 1
    public function listCredentials()
358
    {
359 1
        return $this->model(Models\Credential::class)->enumerate($this->api->getCredentials());
360
    }
361
362
    /**
363
     * Retrieves a credential object and populates its unique identifier object. This operation will not perform a GET
364
     * or HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API.
365
     *
366
     * @param string $id The unique ID of the credential
367
     *
368
     * @return Models\Credential
369
     */
370 1
    public function getCredential($id)
371
    {
372 1
        return $this->model(Models\Credential::class, ['id' => $id]);
373
    }
374
375
    /**
376
     * Creates a new role according to the provided options.
377
     *
378
     * @param array $options {@see \OpenStack\Identity\v3\Api::postRoles}
379
     *
380
     * @return Models\Role
381
     */
382 1
    public function createRole(array $options)
383
    {
384 1
        return $this->model(Models\Role::class)->create($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 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\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...
385
    }
386
387
    /**
388
     * Returns a generator which will yield a collection of role objects. The elements which generators yield can be
389
     * accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you
390
     * will need to use retrieve() to pull in the full state of the remote resource from the API.
391
     *
392
     * @param array $options {@see \OpenStack\Identity\v3\Api::getRoles}
393
     *
394
     * @return \Generator
395
     */
396 1
    public function listRoles(array $options = [])
397
    {
398 1
        return $this->model(Models\Role::class)->enumerate($this->api->getRoles(), $options);
399
    }
400
401
    /**
402
     * Returns a generator which will yield a collection of role assignment objects. The elements which generators
403
     * yield can be accessed using a foreach loop. Often the API will not return the full state of the resource in
404
     * collections; you will need to use retrieve() to pull in the full state of the remote resource from the API.
405
     *
406
     * @param array $options {@see \OpenStack\Identity\v3\Api::getRoleAssignments}
407
     *
408
     * @return \Generator
409
     */
410 1
    public function listRoleAssignments(array $options = [])
411
    {
412 1
        return $this->model(Models\Assignment::class)->enumerate($this->api->getRoleAssignments(), $options);
413
    }
414
415
    /**
416
     * Creates a new policy according to the provided options.
417
     *
418
     * @param array $options {@see \OpenStack\Identity\v3\Api::postPolicies}
419
     *
420
     * @return Models\Policy
421
     */
422 1
    public function createPolicy(array $options)
423
    {
424 1
        return $this->model(Models\Policy::class)->create($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 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\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...
425
    }
426
427
    /**
428
     * Returns a generator which will yield a collection of policy objects. The elements which generators yield can be
429
     * accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you
430
     * will need to use retrieve() to pull in the full state of the remote resource from the API.
431
     *
432
     * @param array $options {@see \OpenStack\Identity\v3\Api::getPolicies}
433
     *
434
     * @return \Generator
435
     */
436 1
    public function listPolicies(array $options = [])
437
    {
438 1
        return $this->model(Models\Policy::class)->enumerate($this->api->getPolicies(), $options);
439
    }
440
441
    /**
442
     * Retrieves a policy object and populates its unique identifier object. This operation will not perform a GET or
443
     * HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API.
444
     *
445
     * @param string $id The unique ID of the policy
446
     *
447
     * @return Models\Policy
448
     */
449 1
    public function getPolicy($id)
450
    {
451 1
        return $this->model(Models\Policy::class, ['id' => $id]);
452
    }
453
}
454