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.

Project   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 153
Duplicated Lines 11.76 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 1
dl 18
loc 153
ccs 41
cts 41
cp 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A retrieve() 0 5 1
A update() 0 5 1
A delete() 0 4 1
A grantUserRole() 0 4 1
A revokeUserRole() 0 4 1
A grantGroupRole() 0 4 1
A revokeGroupRole() 0 4 1
A checkUserRole() 9 9 2
A checkGroupRole() 9 9 2
A create() 0 6 1
A listUserRoles() 0 5 1
A listGroupRoles() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Project extends OperatorResource implements Creatable, Retrievable, Listable, Updateable, Deletable
17
{
18
    /** @var string */
19
    public $domainId;
20
21
    /** @var string */
22
    public $parentId;
23
24
    /** @var bool */
25
    public $enabled;
26
27
    /** @var string */
28
    public $description;
29
30
    /** @var string */
31
    public $id;
32
33
    /** @var array */
34
    public $links;
35
36
    /** @var string */
37
    public $name;
38
39
    protected $aliases = [
40
        'domain_id' => 'domainId',
41
        'parent_id' => 'parentId',
42
    ];
43
44
    protected $resourceKey = 'project';
45
    protected $resourcesKey = 'projects';
46
47
    /**
48
     * {@inheritDoc}
49
     *
50
     * @param array $data {@see \OpenStack\Identity\v3\Api::postProjects}
51
     */
52 1
    public function create(array $data): Creatable
53
    {
54 1
        $response = $this->execute($this->api->postProjects(), $data);
55 1
        $this->populateFromResponse($response);
56 1
        return $this;
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62 1
    public function retrieve()
63
    {
64 1
        $response = $this->executeWithState($this->api->getProject());
65 1
        $this->populateFromResponse($response);
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71 1
    public function update()
72
    {
73 1
        $response = $this->executeWithState($this->api->patchProject());
74 1
        $this->populateFromResponse($response);
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80 1
    public function delete()
81
    {
82 1
        $this->executeWithState($this->api->deleteProject());
83 1
    }
84
85
    /**
86
     * @param array $options {@see \OpenStack\Identity\v3\Api::getProjectUserRoles}
87
     *
88
     * @return \Generator
89
     */
90 1
    public function listUserRoles(array $options = []): \Generator
91
    {
92 1
        $options['projectId'] = $this->id;
93 1
        return $this->model(Role::class)->enumerate($this->api->getProjectUserRoles(), $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...
94
    }
95
96
    /**
97
     * @param array $options {@see \OpenStack\Identity\v3\Api::putProjectUserRole}
98
     */
99 1
    public function grantUserRole(array $options)
100
    {
101 1
        $this->execute($this->api->putProjectUserRole(), ['projectId' => $this->id] + $options);
102 1
    }
103
104
    /**
105
     * @param array $options {@see \OpenStack\Identity\v3\Api::headProjectUserRole}
106
     *
107
     * @return bool
108
     */
109 2 View Code Duplication
    public function checkUserRole(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...
110
    {
111
        try {
112 2
            $this->execute($this->api->headProjectUserRole(), ['projectId' => $this->id] + $options);
113 1
            return true;
114 1
        } catch (BadResponseError $e) {
115 1
            return false;
116
        }
117
    }
118
119
    /**
120
     * @param array $options {@see \OpenStack\Identity\v3\Api::deleteProjectUserRole}
121
     */
122 1
    public function revokeUserRole(array $options)
123
    {
124 1
        $this->execute($this->api->deleteProjectUserRole(), ['projectId' => $this->id] + $options);
125 1
    }
126
127
    /**
128
     * @param array $options {@see \OpenStack\Identity\v3\Api::getProjectGroupRoles}
129
     *
130
     * @return \Generator
131
     */
132 1
    public function listGroupRoles(array $options = []): \Generator
133
    {
134 1
        $options['projectId'] = $this->id;
135 1
        return $this->model(Role::class)->enumerate($this->api->getProjectGroupRoles(), $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...
136
    }
137
138
    /**
139
     * @param array $options {@see \OpenStack\Identity\v3\Api::putProjectGroupRole}
140
     */
141 1
    public function grantGroupRole(array $options)
142
    {
143 1
        $this->execute($this->api->putProjectGroupRole(), ['projectId' => $this->id] + $options);
144 1
    }
145
146
    /**
147
     * @param array $options {@see \OpenStack\Identity\v3\Api::headProjectGroupRole}
148
     *
149
     * @return bool
150
     */
151 2 View Code Duplication
    public function checkGroupRole(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...
152
    {
153
        try {
154 2
            $this->execute($this->api->headProjectGroupRole(), ['projectId' => $this->id] + $options);
155 1
            return true;
156 1
        } catch (BadResponseError $e) {
157 1
            return false;
158
        }
159
    }
160
161
    /**
162
     * @param array $options {@see \OpenStack\Identity\v3\Api::deleteProjectGroupRole}
163
     */
164 1
    public function revokeGroupRole(array $options)
165
    {
166 1
        $this->execute($this->api->deleteProjectGroupRole(), ['projectId' => $this->id] + $options);
167 1
    }
168
}
169