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.

Catalog   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 52
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAliases() 0 6 1
A populateFromArray() 0 8 2
A getServiceUrl() 0 17 4
1
<?php declare(strict_types=1);
2
3
namespace OpenStack\Identity\v3\Models;
4
5
use OpenStack\Common\Resource\Alias;
6
use OpenStack\Common\Resource\OperatorResource;
7
8
/**
9
 * @property \OpenStack\Identity\v3\Api $api
10
 */
11
class Catalog extends OperatorResource implements \OpenStack\Common\Auth\Catalog
12
{
13
    /** @var []Service */
14
    public $services;
15 4
16
    /**
17 4
     * @inheritdoc
18 4
     */
19 4
    protected function getAliases(): array
20 4
    {
21
        return parent::getAliases() + [
22
            'services' => new Alias('services', Service::class, true)
23
        ];
24
    }
25
26
    public function populateFromArray(array $data): self
27
    {
28
        foreach ($data as $service) {
29
            $this->services[] = $this->model(Service::class, $service);
30
        }
31
32 5
        return $this;
33
    }
34 5
35 1
    /**
36
     * Retrieve a base URL for a service, according to its catalog name, type, region.
37
     *
38 4
     * @param string $name    The name of the service as it appears in the catalog.
39 4
     * @param string $type    The type of the service as it appears in the catalog.
40 2
     * @param string $region  The region of the service as it appears in the catalog.
41
     * @param string $urlType Unused.
42 2
     *
43
     * @return false|string   FALSE if no URL found
44 2
     */
45
    public function getServiceUrl(string $name, string $type, string $region, string $urlType): string
46
    {
47
        if (empty($this->services)) {
48
            throw new \RuntimeException('No services are defined');
49
        }
50
51
        foreach ($this->services as $service) {
52
            if (false !== ($url = $service->getUrl($name, $type, $region, $urlType))) {
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 getUrl() does only exist in the following implementations of said interface: OpenStack\Identity\v2\Models\Endpoint, OpenStack\Identity\v3\Models\Service.

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...
53
                return $url;
54
            }
55
        }
56
57
        throw new \RuntimeException(sprintf(
58
            "Endpoint URL could not be found in the catalog for this service.\nName: %s\nType: %s\nRegion: %s\nURL type: %s",
59
            $name, $type, $region, $urlType
60
        ));
61
    }
62
}
63