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

LoadBalancer::getAliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace OpenStack\Networking\v2\Models;
4
5
use OpenStack\Common\Resource\Alias;
6
use OpenStack\Common\Resource\Creatable;
7
use OpenStack\Common\Resource\Deletable;
8
use OpenStack\Common\Resource\OperatorResource;
9
use OpenStack\Common\Resource\Retrievable;
10
use OpenStack\Common\Resource\Updateable;
11
use OpenStack\Networking\v2\Api;
12
13
/**
14
 * Represents a Neutron v2 LoadBalancer
15
 *
16
 * @property Api $api
17
 */
18
class LoadBalancer extends OperatorResource implements Creatable, Retrievable, Updateable, Deletable
19
{
20
    /**
21
     * @var string
22
     */
23
    public $name;
24
25
    /**
26
     * @var string
27
     */
28
    public $description;
29
30
    /**
31
     * @var boolean
32
     */
33
    public $adminStateUp;
34
35
    /**
36
     * @var string
37
     */
38
    public $tenantId;
39
40
    /**
41
     * @var LoadBalancerListener[]
42
     */
43
    public $listeners;
44
45
    /**
46
     * @var string
47
     */
48
    public $vipAddress;
49
50
    /**
51
     * @var string
52
     */
53
    public $vipSubnetId;
54
55
    /**
56
     * @var string
57
     */
58
    public $id;
59
60
    /**
61
     * @var string
62
     */
63
    public $operatingStatus;
64
65
    /**
66
     * @var string
67
     */
68
    public $provisioningStatus;
69
70
    protected $resourcesKey = 'loadbalancers';
71
    protected $resourceKey = 'loadbalancer';
72
73
    protected $aliases = [
74
        'tenant_id'           => 'tenantId',
75
        'admin_state_up'      => 'adminStateUp',
76
        'vip_address'         => 'vipAddress',
77
        'vip_subnet_id'       => 'vipSubnetId',
78
        'operating_status'    => 'operatingStatus',
79
        'provisioning_status' => 'provisioningStatus',
80
    ];
81
82
    /**
83
     * @inheritdoc
84
     */
85
    protected function getAliases()
86
    {
87
        $aliases = parent::getAliases();
88
        $aliases['listeners'] = new Alias('listeners', LoadBalancerListener::class, true);
89
        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...
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public function create(array $userOptions): Creatable
96
    {
97
        $response = $this->execute($this->api->postLoadBalancer(), $userOptions);
98
        return $this->populateFromResponse($response);
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104
    public function retrieve()
105
    {
106
        $response = $this->execute($this->api->getLoadBalancer(), ['id' => (string)$this->id]);
107
        $this->populateFromResponse($response);
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113
    public function update()
114
    {
115
        $response = $this->executeWithState($this->api->putLoadBalancer());
116
        $this->populateFromResponse($response);
117
    }
118
119
    /**
120
     * {@inheritDoc}
121
     */
122
    public function delete()
123
    {
124
        $this->executeWithState($this->api->deleteLoadBalancer());
125
    }
126
127
    /**
128
     * Add a listener to this load balancer
129
     *
130
     * @param array $userOptions
131
     * @return LoadBalancerListener
132
     */
133
    public function addListener(array $userOptions = []): LoadBalancerListener
134
    {
135
        $userOptions = array_merge(['loadbalancerId' => $this->id], $userOptions);
136
        return $this->model(LoadBalancerListener::class)->create($userOptions);
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 create() does only exist in the following implementations of said interface: OpenStack\BlockStorage\v2\Models\Snapshot, OpenStack\BlockStorage\v2\Models\Volume, OpenStack\BlockStorage\v2\Models\VolumeType, OpenStack\Compute\v2\Models\Flavor, OpenStack\Compute\v2\Models\Keypair, OpenStack\Compute\v2\Models\Server, 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\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\LoadBalancer, OpenStack\Networking\v2\...adBalancerHealthMonitor, OpenStack\Networking\v2\...ls\LoadBalancerListener, OpenStack\Networking\v2\Models\LoadBalancerMember, OpenStack\Networking\v2\Models\LoadBalancerPool, OpenStack\Networking\v2\Models\Network, OpenStack\Networking\v2\Models\Port, OpenStack\Networking\v2\Models\Subnet, 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...
137
    }
138
139
    /**
140
     * Get stats for this loadbalancer
141
     *
142
     * @return LoadBalancerStat
143
     */
144
    public function getStats(): LoadBalancerStat
145
    {
146
        $model = $this->model(LoadBalancerStat::class, ['loadbalancerId' => $this->id]);
147
        $model->retrieve();
148
        return $model;
149
    }
150
151
    /**
152
     * Get the status tree for this loadbalancer
153
     *
154
     * @return LoadBalancerStatus
155
     */
156
    public function getStatuses(): LoadBalancerStatus
157
    {
158
        $model = $this->model(LoadBalancerStatus::class, ['loadbalancerId' => $this->id]);
159
        $model->retrieve();
160
        return $model;
161
    }
162
}
163