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.
Completed
Pull Request — master (#159)
by Roman
01:55
created

Catalog::getServiceUrl()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.2
ccs 0
cts 0
cp 0
cc 4
eloc 9
nc 4
nop 4
crap 20
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
     * @var array
18 4
     */
19 4
    protected $aliases = [];
20 4
21
    /**
22
     * @inheritdoc
23
     */
24
    protected function getAliases()
25
    {
26
        $aliases = parent::getAliases();
27
        $aliases['services'] = new Alias('services', Service::class, true);
28
        return $aliases;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $aliases; (array<*,OpenStack\Common\Resource\Alias>) is incompatible with the return type of the parent method OpenStack\Common\Resourc...actResource::getAliases of type OpenStack\Common\Resource\Alias[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
29
    }
30
31
    public function populateFromArray(array $data): self
32 5
    {
33
        foreach ($data as $service) {
34 5
            $this->services[] = $this->model(Service::class, $service);
35 1
        }
36
37
        return $this;
38 4
    }
39 4
40 2
    /**
41
     * Retrieve a base URL for a service, according to its catalog name, type, region.
42 2
     *
43
     * @param string $name    The name of the service as it appears in the catalog.
44 2
     * @param string $type    The type of the service as it appears in the catalog.
45
     * @param string $region  The region of the service as it appears in the catalog.
46
     * @param string $urlType Unused.
47
     *
48
     * @return false|string   FALSE if no URL found
49
     */
50
    public function getServiceUrl(string $name, string $type, string $region, string $urlType): string
51
    {
52
        if (empty($this->services)) {
53
            throw new \RuntimeException('No services are defined');
54
        }
55
56
        foreach ($this->services as $service) {
57
            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...
58
                return $url;
59
            }
60
        }
61
62
        throw new \RuntimeException(sprintf(
63
            "Endpoint URL could not be found in the catalog for this service.\nName: %s\nType: %s\nRegion: %s\nURL type: %s",
64
            $name, $type, $region, $urlType
65
        ));
66
    }
67
}
68