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.

Group::checkMembership()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 9
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace OpenStack\Identity\v3\Models;
4
5
use OpenStack\Common\Error\BadResponseError;
6
use OpenStack\Common\Resource\OperatorResource;
7
use OpenStack\Common\Resource\Creatable;
8
use OpenStack\Common\Resource\Deletable;
9
use OpenStack\Common\Resource\Listable;
10
use OpenStack\Common\Resource\Retrievable;
11
use OpenStack\Common\Resource\Updateable;
12
13
/**
14
 * @property \OpenStack\Identity\v3\Api $api
15
 */
16
class Group extends OperatorResource implements Creatable, Listable, Retrievable, Updateable, Deletable
17
{
18
    /** @var string */
19
    public $domainId;
20
21
    /** @var string */
22
    public $id;
23
24
    /** @var string */
25
    public $description;
26
27
    /** @var array */
28
    public $links;
29
30
    /** @var string */
31
    public $name;
32
33
    protected $aliases = ['domain_id' => 'domainId'];
34
35
    protected $resourceKey = 'group';
36
    protected $resourcesKey = 'groups';
37
38
    /**
39
     * {@inheritDoc}
40
     *
41
     * @param array $data {@see \OpenStack\Identity\v3\Api::postGroups}
42
     */
43 2
    public function create(array $data): Creatable
44
    {
45 2
        $response = $this->execute($this->api->postGroups(), $data);
46 2
        return $this->populateFromResponse($response);
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52 1
    public function retrieve()
53
    {
54 1
        $response = $this->execute($this->api->getGroup(), ['id' => $this->id]);
55 1
        $this->populateFromResponse($response);
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61 1
    public function update()
62
    {
63 1
        $response = $this->executeWithState($this->api->patchGroup());
64 1
        $this->populateFromResponse($response);
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70 1
    public function delete()
71
    {
72 1
        $this->execute($this->api->deleteGroup(), ['id' => $this->id]);
73 1
    }
74
75
    /**
76
     * @param array $options {@see \OpenStack\Identity\v3\Api::getGroupUsers}
77
     *
78
     * @return \Generator
79
     */
80 1
    public function listUsers(array $options = []): \Generator
81
    {
82 1
        $options['id'] = $this->id;
83 1
        return $this->model(User::class)->enumerate($this->api->getGroupUsers(), $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\AvailabilityZone, OpenStack\Compute\v2\Models\Flavor, OpenStack\Compute\v2\Models\Host, 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\InterfaceAttachment, OpenStack\Networking\v2\Models\LoadBalancer, OpenStack\Networking\v2\...adBalancerHealthMonitor, OpenStack\Networking\v2\...ls\LoadBalancerListener, OpenStack\Networking\v2\Models\LoadBalancerMember, OpenStack\Networking\v2\Models\LoadBalancerPool, OpenStack\Networking\v2\Models\LoadBalancerStat, OpenStack\Networking\v2\Models\LoadBalancerStatus, 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...
84
    }
85
86
    /**
87
     * @param array $options {@see \OpenStack\Identity\v3\Api::putGroupUser}
88
     */
89 1
    public function addUser(array $options)
90
    {
91 1
        $this->execute($this->api->putGroupUser(), ['groupId' => $this->id] + $options);
92 1
    }
93
94
    /**
95
     * @param array $options {@see \OpenStack\Identity\v3\Api::deleteGroupUser}
96
     */
97 2
    public function removeUser(array $options)
98
    {
99 1
        $this->execute($this->api->deleteGroupUser(), ['groupId' => $this->id] + $options);
100 2
    }
101
102
    /**
103
     * @param array $options {@see \OpenStack\Identity\v3\Api::headGroupUser}
104
     *
105
     * @return bool
106
     */
107 2 View Code Duplication
    public function checkMembership(array $options): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        try {
110 2
            $this->execute($this->api->headGroupUser(), ['groupId' => $this->id] + $options);
111 1
            return true;
112 1
        } catch (BadResponseError $e) {
113 1
            return false;
114
        }
115
    }
116
}
117