OAuth2Request::getGrantParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\OAuth2\HttpClient\Request;
6
7
use function array_key_exists;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\StreamInterface;
10
use Psr\Http\Message\UriInterface;
11
12
class OAuth2Request implements OAuth2RequestInterface
13
{
14
    /** @var RequestInterface */
15
    private $request;
16
17
    /**
18
     * @var array<string, mixed>
19
     */
20
    private $grantParams = [];
21
22 25
    public function __construct(RequestInterface $request)
23
    {
24 25
        $this->request = $request;
25 25
    }
26
27
    /**
28
     * @inheritDoc
29
     */
30 10
    public function getGrantParams(): array
31
    {
32 10
        return $this->grantParams;
33
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38 10
    public function withGrantParams(array $grantParams): OAuth2RequestInterface
39
    {
40 10
        $new = clone $this;
41 10
        $new->grantParams = $grantParams;
42
43 10
        return $new;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $new; (Facile\OAuth2\HttpClient\Request\OAuth2Request) is incompatible with the return type declared by the interface Facile\OAuth2\HttpClient...erface::withGrantParams of type self.

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...
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 1
    public function withGrantParam(string $name, $value): OAuth2RequestInterface
50
    {
51 1
        $new = clone $this;
52 1
        $new->grantParams[$name] = $value;
53
54 1
        return $new;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $new; (Facile\OAuth2\HttpClient\Request\OAuth2Request) is incompatible with the return type declared by the interface Facile\OAuth2\HttpClient...terface::withGrantParam of type self.

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...
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60 1
    public function withoutGrantParam(string $name): OAuth2RequestInterface
61
    {
62 1
        $new = clone $this;
63 1
        if (array_key_exists($name, $new->grantParams)) {
64 1
            unset($new->grantParams[$name]);
65
        }
66
67 1
        return $new;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $new; (Facile\OAuth2\HttpClient\Request\OAuth2Request) is incompatible with the return type declared by the interface Facile\OAuth2\HttpClient...face::withoutGrantParam of type self.

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...
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73 1
    public function getProtocolVersion(): string
74
    {
75 1
        return $this->request->getProtocolVersion();
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81 2
    public function withProtocolVersion($version)
82
    {
83 2
        $new = clone $this;
84 2
        $new->request = $this->request->withProtocolVersion($version);
85
86 2
        return $new;
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92 1
    public function getHeaders(): array
93
    {
94 1
        return $this->request->getHeaders();
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100 10
    public function hasHeader($name): bool
101
    {
102 10
        return $this->request->hasHeader($name);
103
    }
104
105
    /**
106
     * @inheritDoc
107
     */
108 8
    public function getHeader($name): array
109
    {
110 8
        return $this->request->getHeader($name);
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116 1
    public function getHeaderLine($name): string
117
    {
118 1
        return $this->request->getHeaderLine($name);
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124 7
    public function withHeader($name, $value)
125
    {
126 7
        $new = clone $this;
127 7
        $new->request = $this->request->withHeader($name, $value);
128
129 7
        return $new;
130
    }
131
132
    /**
133
     * @inheritDoc
134
     */
135 1
    public function withAddedHeader($name, $value)
136
    {
137 1
        $new = clone $this;
138 1
        $new->request = $this->request->withAddedHeader($name, $value);
139
140 1
        return $new;
141
    }
142
143
    /**
144
     * @inheritDoc
145
     */
146 7
    public function withoutHeader($name)
147
    {
148 7
        $new = clone $this;
149 7
        $new->request = $this->request->withoutHeader($name);
150
151 7
        return $new;
152
    }
153
154
    /**
155
     * @inheritDoc
156
     */
157 2
    public function getBody()
158
    {
159 2
        return $this->request->getBody();
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165 1
    public function withBody(StreamInterface $body)
166
    {
167 1
        $new = clone $this;
168 1
        $new->request = $this->request->withBody($body);
169
170 1
        return $new;
171
    }
172
173
    /**
174
     * @inheritDoc
175
     */
176 2
    public function getRequestTarget(): string
177
    {
178 2
        return $this->request->getRequestTarget();
179
    }
180
181
    /**
182
     * @inheritDoc
183
     */
184 1
    public function withRequestTarget($requestTarget)
185
    {
186 1
        $new = clone $this;
187 1
        $new->request = $this->request->withRequestTarget($requestTarget);
188
189 1
        return $new;
190
    }
191
192
    /**
193
     * @inheritDoc
194
     */
195 6
    public function getMethod(): string
196
    {
197 6
        return $this->request->getMethod();
198
    }
199
200
    /**
201
     * @inheritDoc
202
     */
203 1
    public function withMethod($method)
204
    {
205 1
        $new = clone $this;
206 1
        $new->request = $this->request->withMethod($method);
207
208 1
        return $new;
209
    }
210
211
    /**
212
     * @inheritDoc
213
     */
214 2
    public function getUri(): UriInterface
215
    {
216 2
        return $this->request->getUri();
217
    }
218
219
    /**
220
     * @inheritdoc
221
     */
222 1
    public function withUri(UriInterface $uri, $preserveHost = false)
223
    {
224 1
        $new = clone $this;
225 1
        $new->request = $this->request->withUri($uri, $preserveHost);
226
227 1
        return $new;
228
    }
229
}
230