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.
Passed
Push — master ( bde994...6bcf7a )
by Jamie
12:13
created

Operator::getHttpBaseUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace OpenStack\Common\Api;
4
5
use function GuzzleHttp\uri_template;
6
use GuzzleHttp\ClientInterface;
7
use OpenStack\Common\Resource\ResourceInterface;
8
use OpenStack\Common\Transport\RequestSerializer;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * {@inheritDoc}
13
 */
14
abstract class Operator implements OperatorInterface
15
{
16
    /** @var ClientInterface */
17
    private $client;
18
19
    /** @var ApiInterface */
20
    protected $api;
21
22
    /**
23
     * {@inheritDoc}
24
     */
25 212
    public function __construct(ClientInterface $client, ApiInterface $api)
26
    {
27 212
        $this->client = $client;
28 212
        $this->api = $api;
29 212
    }
30
31
    /**
32
     * Magic method for dictating how objects are rendered when var_dump is called.
33
     * For the benefit of users, extremely verbose and heavy properties (such as HTTP clients) are
34
     * removed to provide easier access to normal state, such as resource attributes.
35
     *
36
     * @return array
37
     */
38
    public function __debugInfo()
39
    {
40
        $excludedVars = ['client', 'errorBuilder', 'api'];
41
42
        $output = [];
43
44
        foreach (get_object_vars($this) as $key => $val) {
45
            if (!in_array($key, $excludedVars)) {
46
                $output[$key] = $val;
47
            }
48
        }
49
50
        return $output;
51
    }
52
53
    /**
54
     * Retrieves a populated Operation according to the definition and values provided. A
55
     * HTTP client is also injected into the object to allow it to communicate with the remote API.
56
     *
57
     * @param array $definition  The data that dictates how the operation works
58
     *
59
     * @return Operation
60
     */
61 165
    public function getOperation(array $definition)
62
    {
63 165
        return new Operation($definition);
64
    }
65
66 164
    protected function sendRequest(Operation $operation, array $userValues = [], $async = false)
67
    {
68 164
        $uri     = uri_template($operation->getPath(), $userValues);
69 164
        $options = RequestSerializer::serializeOptions($operation, $userValues);
70 164
        $method  = $async ? 'requestAsync' : 'request';
71
72 164
        return $this->client->$method($operation->getMethod(), $uri, $options);
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function executeAsync(array $definition, array $userValues = [])
79
    {
80
        return $this->sendRequest($this->getOperation($definition), $userValues, true);
81
    }
82
83 138
    public function execute(array $definition, array $userValues = [])
84
    {
85 138
        return $this->sendRequest($this->getOperation($definition), $userValues);
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 80
    public function model($class, $data = null)
92
    {
93 80
        $model = new $class($this->client, $this->api);
94
95
        // @codeCoverageIgnoreStart
96
        if (!$model instanceof ResourceInterface) {
97
            throw new \RuntimeException(sprintf('%s does not implement %s', $class, ResourceInterface::class));
98
        }
99
        // @codeCoverageIgnoreEnd
100
101 80
        if ($data instanceof ResponseInterface) {
102 2
            $model->populateFromResponse($data);
103 80
        } elseif (is_array($data)) {
104 23
            $model->populateFromArray($data);
105 23
        }
106
107 80
        return $model;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $model; (OpenStack\Common\Resource\ResourceInterface) is incompatible with the return type declared by the interface OpenStack\Common\Api\OperatorInterface::model of type OpenStack\Common\Api\ResourceInterface.

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...
108
    }
109
110
    /**
111
     * Will create a new instance of this class with the current HTTP client and API injected in. This
112
     * is useful when enumerating over a collection since multiple copies of the same resource class
113
     * are needed.
114
     *
115
     * @return static
116
     */
117 30
    public function newInstance()
118
    {
119 30
        return new static($this->client, $this->api);
120
    }
121
122
    /**
123
     * @return \GuzzleHttp\Psr7\Uri
124
     */
125 1
    protected function getHttpBaseUrl()
126
    {
127 1
        return $this->client->getConfig('base_url');
128
    }
129
}