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.

Service   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 12
loc 72
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccount() 0 4 1
A getContainer() 0 4 1
A createContainer() 0 4 1
A listContainers() 0 5 1
A containerExists() 12 12 3

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\ObjectStore\v1;
4
5
use OpenStack\Common\Error\BadResponseError;
6
use OpenStack\Common\Service\AbstractService;
7
use OpenStack\ObjectStore\v1\Models\Account;
8
use OpenStack\ObjectStore\v1\Models\Container;
9
10
/**
11
 * @property \OpenStack\ObjectStore\v1\Api $api
12
 */
13
class Service extends AbstractService
14
{
15
    /**
16
     * Retrieves an Account object.
17
     *
18
     * @return Account
19
     */
20 1
    public function getAccount(): Account
21
    {
22 1
        return $this->model(Account::class);
23
    }
24
25
    /**
26
     * Retrieves a collection of container resources in a generator format.
27
     *
28
     * @param array         $options {@see \OpenStack\ObjectStore\v1\Api::getAccount}
29
     * @param callable|null $mapFn   Allows a function to be mapped over each element in the collection.
30
     *
31
     * @return \Generator
32
     */
33 1
    public function listContainers(array $options = [], callable $mapFn = null): \Generator
34
    {
35 1
        $options = array_merge($options, ['format' => 'json']);
36 1
        return $this->model(Container::class)->enumerate($this->api->getAccount(), $options, $mapFn);
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...
37
    }
38
39
    /**
40
     * Retrieves a Container object and populates its name according to the value provided. Please note that the
41
     * remote API is not contacted.
42
     *
43
     * @param string $name The unique name of the container
44
     *
45
     * @return Container
46
     */
47 2
    public function getContainer(string $name = null): Container
48
    {
49 2
        return $this->model(Container::class, ['name' => $name]);
50
    }
51
52
    /**
53
     * Creates a new container according to the values provided.
54
     *
55
     * @param array $data {@see \OpenStack\ObjectStore\v1\Api::putContainer}
56
     *
57
     * @return Container
58
     */
59 2
    public function createContainer(array $data): Container
60
    {
61 2
        return $this->getContainer()->create($data);
62
    }
63
64
    /**
65
     * Checks the existence of a container.
66
     *
67
     * @param string $name The name of the container
68
     *
69
     * @return bool             TRUE if exists, FALSE if it doesn't
70
     * @throws BadResponseError Thrown for any non 404 status error
71
     */
72 4 View Code Duplication
    public function containerExists(string $name): 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...
73
    {
74
        try {
75 4
            $this->execute($this->api->headContainer(), ['name' => $name]);
76 1
            return true;
77 3
        } catch (BadResponseError $e) {
78 3
            if ($e->getResponse()->getStatusCode() === 404) {
79 2
                return false;
80
            }
81 1
            throw $e;
82
        }
83
    }
84
}
85