Skip to content

Commit 123e4b4

Browse files
Added additional return type enforcement
1 parent 505f4cf commit 123e4b4

14 files changed

+64
-56
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGE LOG
77
* Dropped support for PHP 7.1
88
* Made builder class final
99
* Re-worked pagination
10+
* Added additional return type enforcement
1011

1112

1213
## V3.3 (27/11/2020)

src/Api/AbstractApi.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Bitbucket\HttpClient\Message\ResponseMediator;
1818
use Bitbucket\HttpClient\Util\JsonArray;
1919
use Bitbucket\HttpClient\Util\QueryStringBuilder;
20+
use Psr\Http\Message\ResponseInterface;
2021

2122
/**
2223
* @author Joseph Bielawski <[email protected]>
@@ -62,7 +63,7 @@ public function __construct(Client $client)
6263
*
6364
* @return Client
6465
*/
65-
protected function getClient()
66+
protected function getClient(): Client
6667
{
6768
return $this->client;
6869
}
@@ -78,7 +79,7 @@ protected function getClient()
7879
*
7980
* @return \Psr\Http\Message\ResponseInterface
8081
*/
81-
protected function getAsResponse(string $uri, array $params = [], array $headers = [])
82+
protected function getAsResponse(string $uri, array $params = [], array $headers = []): ResponseInterface
8283
{
8384
if (null !== $this->perPage && !isset($params['pagelen'])) {
8485
$params['pagelen'] = $this->perPage;
@@ -98,7 +99,7 @@ protected function getAsResponse(string $uri, array $params = [], array $headers
9899
*
99100
* @return array
100101
*/
101-
protected function get(string $uri, array $params = [], array $headers = [])
102+
protected function get(string $uri, array $params = [], array $headers = []): array
102103
{
103104
$response = $this->getAsResponse($uri, $params, $headers);
104105

@@ -116,7 +117,7 @@ protected function get(string $uri, array $params = [], array $headers = [])
116117
*
117118
* @return array
118119
*/
119-
protected function post(string $uri, array $params = [], array $headers = [])
120+
protected function post(string $uri, array $params = [], array $headers = []): array
120121
{
121122
$body = self::prepareJsonBody($params);
122123

@@ -138,7 +139,7 @@ protected function post(string $uri, array $params = [], array $headers = [])
138139
*
139140
* @return array
140141
*/
141-
protected function postRaw(string $uri, $body = null, array $headers = [])
142+
protected function postRaw(string $uri, $body = null, array $headers = []): array
142143
{
143144
$response = $this->client->getHttpClient()->post(self::prepareUri($uri), $headers, $body ?? '');
144145

@@ -156,7 +157,7 @@ protected function postRaw(string $uri, $body = null, array $headers = [])
156157
*
157158
* @return array
158159
*/
159-
protected function put(string $uri, array $params = [], array $headers = [])
160+
protected function put(string $uri, array $params = [], array $headers = []): array
160161
{
161162
$body = self::prepareJsonBody($params);
162163

@@ -178,7 +179,7 @@ protected function put(string $uri, array $params = [], array $headers = [])
178179
*
179180
* @return array
180181
*/
181-
protected function putRaw(string $uri, $body = null, array $headers = [])
182+
protected function putRaw(string $uri, $body = null, array $headers = []): array
182183
{
183184
$response = $this->client->getHttpClient()->put(self::prepareUri($uri), $headers, $body ?? '');
184185

@@ -196,7 +197,7 @@ protected function putRaw(string $uri, $body = null, array $headers = [])
196197
*
197198
* @return array
198199
*/
199-
protected function delete(string $uri, array $params = [], array $headers = [])
200+
protected function delete(string $uri, array $params = [], array $headers = []): array
200201
{
201202
$body = self::prepareJsonBody($params);
202203

@@ -218,7 +219,7 @@ protected function delete(string $uri, array $params = [], array $headers = [])
218219
*
219220
* @return array
220221
*/
221-
protected function deleteRaw(string $uri, $body = null, array $headers = [])
222+
protected function deleteRaw(string $uri, $body = null, array $headers = []): array
222223
{
223224
$response = $this->client->getHttpClient()->delete(self::prepareUri($uri), $headers, $body ?? '');
224225

@@ -233,7 +234,7 @@ protected function deleteRaw(string $uri, $body = null, array $headers = [])
233234
*
234235
* @return string
235236
*/
236-
private static function prepareUri(string $uri, array $query = [])
237+
private static function prepareUri(string $uri, array $query = []): string
237238
{
238239
return \sprintf('%s%s%s', self::URI_PREFIX, $uri, QueryStringBuilder::build($query));
239240
}
@@ -245,7 +246,7 @@ private static function prepareUri(string $uri, array $query = [])
245246
*
246247
* @return string|null
247248
*/
248-
private static function prepareJsonBody(array $params)
249+
private static function prepareJsonBody(array $params): ?string
249250
{
250251
if (0 === \count($params)) {
251252
return null;
@@ -261,7 +262,7 @@ private static function prepareJsonBody(array $params)
261262
*
262263
* @return array<string,string>
263264
*/
264-
private static function addJsonContentType(array $headers)
265+
private static function addJsonContentType(array $headers): array
265266
{
266267
return \array_merge([ResponseMediator::CONTENT_TYPE_HEADER => ResponseMediator::JSON_CONTENT_TYPE], $headers);
267268
}

src/Client.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
use Bitbucket\HttpClient\Plugin\Authentication;
2727
use Bitbucket\HttpClient\Plugin\ExceptionThrower;
2828
use Bitbucket\HttpClient\Plugin\History;
29+
use Http\Client\Common\HttpMethodsClientInterface;
2930
use Http\Client\Common\Plugin\AddHostPlugin;
3031
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
3132
use Http\Client\Common\Plugin\HistoryPlugin;
3233
use Http\Client\Common\Plugin\RedirectPlugin;
3334
use Http\Discovery\Psr17FactoryDiscovery;
3435
use Psr\Http\Client\ClientInterface;
36+
use Psr\Http\Message\ResponseInterface;
3537

3638
/**
3739
* The Bitbucket API 2.0 client.
@@ -121,7 +123,7 @@ public function __construct(Builder $httpClientBuilder = null)
121123
*
122124
* @return Client
123125
*/
124-
public static function createWithHttpClient(ClientInterface $httpClient)
126+
public static function createWithHttpClient(ClientInterface $httpClient): self
125127
{
126128
$builder = new Builder($httpClient);
127129

@@ -131,47 +133,47 @@ public static function createWithHttpClient(ClientInterface $httpClient)
131133
/**
132134
* @return \Bitbucket\Api\Addon
133135
*/
134-
public function addon()
136+
public function addon(): Addon
135137
{
136138
return new Addon($this);
137139
}
138140

139141
/**
140142
* @return \Bitbucket\Api\CurrentUser
141143
*/
142-
public function currentUser()
144+
public function currentUser(): CurrentUser
143145
{
144146
return new CurrentUser($this);
145147
}
146148

147149
/**
148150
* @return \Bitbucket\Api\HookEvents
149151
*/
150-
public function hookEvents()
152+
public function hookEvents(): HookEvents
151153
{
152154
return new HookEvents($this);
153155
}
154156

155157
/**
156158
* @return \Bitbucket\Api\PullRequests
157159
*/
158-
public function pullRequests()
160+
public function pullRequests(): PullRequests
159161
{
160162
return new PullRequests($this);
161163
}
162164

163165
/**
164166
* @return \Bitbucket\Api\Repositories
165167
*/
166-
public function repositories()
168+
public function repositories(): Repositories
167169
{
168170
return new Repositories($this);
169171
}
170172

171173
/**
172174
* @return \Bitbucket\Api\Snippets
173175
*/
174-
public function snippets()
176+
public function snippets(): Snippets
175177
{
176178
return new Snippets($this);
177179
}
@@ -181,7 +183,7 @@ public function snippets()
181183
*
182184
* @return \Bitbucket\Api\Users
183185
*/
184-
public function users(string $username)
186+
public function users(string $username): Users
185187
{
186188
return new Users($this, $username);
187189
}
@@ -191,7 +193,7 @@ public function users(string $username)
191193
*
192194
* @return \Bitbucket\Api\Workspaces
193195
*/
194-
public function workspaces(string $workspace)
196+
public function workspaces(string $workspace): Workspaces
195197
{
196198
return new Workspaces($this, $workspace);
197199
}
@@ -229,7 +231,7 @@ public function setUrl(string $url): void
229231
*
230232
* @return \Psr\Http\Message\ResponseInterface|null
231233
*/
232-
public function getLastResponse()
234+
public function getLastResponse(): ?ResponseInterface
233235
{
234236
return $this->responseHistory->getLastResponse();
235237
}
@@ -239,7 +241,7 @@ public function getLastResponse()
239241
*
240242
* @return \Http\Client\Common\HttpMethodsClientInterface
241243
*/
242-
public function getHttpClient()
244+
public function getHttpClient(): HttpMethodsClientInterface
243245
{
244246
return $this->getHttpClientBuilder()->getHttpClient();
245247
}
@@ -249,7 +251,7 @@ public function getHttpClient()
249251
*
250252
* @return \Bitbucket\HttpClient\Builder
251253
*/
252-
protected function getHttpClientBuilder()
254+
protected function getHttpClientBuilder(): Builder
253255
{
254256
return $this->httpClientBuilder;
255257
}

src/HttpClient/Builder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Bitbucket\HttpClient;
1515

1616
use Http\Client\Common\HttpMethodsClient;
17+
use Http\Client\Common\HttpMethodsClientInterface;
1718
use Http\Client\Common\Plugin;
1819
use Http\Client\Common\Plugin\Cache\Generator\HeaderCacheKeyGenerator;
1920
use Http\Client\Common\Plugin\CachePlugin;
@@ -101,7 +102,7 @@ public function __construct(
101102
/**
102103
* @return \Http\Client\Common\HttpMethodsClientInterface
103104
*/
104-
public function getHttpClient()
105+
public function getHttpClient(): HttpMethodsClientInterface
105106
{
106107
if (null === $this->pluginClient) {
107108
$plugins = $this->plugins;

src/HttpClient/Message/FileResource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function __construct(string $name, $resource, array $options = [])
6262
*
6363
* @return string
6464
*/
65-
public function getName()
65+
public function getName(): string
6666
{
6767
return $this->name;
6868
}
@@ -82,7 +82,7 @@ public function getResource()
8282
*
8383
* @return array
8484
*/
85-
public function getOptions()
85+
public function getOptions(): array
8686
{
8787
return $this->options;
8888
}

src/HttpClient/Message/ResponseMediator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static function getContent(ResponseInterface $response)
7575
*
7676
* @return array<string,string>
7777
*/
78-
public static function getPagination(ResponseInterface $response)
78+
public static function getPagination(ResponseInterface $response): array
7979
{
8080
try {
8181
/** @var array<string,string> */
@@ -102,7 +102,7 @@ private static function paginationFilter($key)
102102
*
103103
* @return string|null
104104
*/
105-
public static function getErrorMessage(ResponseInterface $response)
105+
public static function getErrorMessage(ResponseInterface $response): ?string
106106
{
107107
try {
108108
/** @var scalar|array */
@@ -121,7 +121,7 @@ public static function getErrorMessage(ResponseInterface $response)
121121
*
122122
* @return string|null
123123
*/
124-
private static function getMessageFromError(array $error)
124+
private static function getMessageFromError(array $error): ?string
125125
{
126126
/** @var scalar|array */
127127
$message = $error['message'] ?? '';
@@ -150,7 +150,7 @@ private static function getMessageFromError(array $error)
150150
*
151151
* @return string
152152
*/
153-
private static function getDetailAsString(array $error)
153+
private static function getDetailAsString(array $error): string
154154
{
155155
/** @var string|array $detail */
156156
$detail = $error['detail'] ?? '';

src/HttpClient/Plugin/Authentication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
7878
*
7979
* @return string
8080
*/
81-
private static function buildAuthorizationHeader(string $method, string $token, string $password = null)
81+
private static function buildAuthorizationHeader(string $method, string $token, string $password = null): string
8282
{
8383
switch ($method) {
8484
case Client::AUTH_HTTP_PASSWORD:

src/HttpClient/Plugin/ExceptionThrower.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Bitbucket\HttpClient\Plugin;
1515

1616
use Bitbucket\Exception\ApiLimitExceededException;
17+
use Bitbucket\Exception\ExceptionInterface;
1718
use Bitbucket\Exception\RuntimeException;
1819
use Bitbucket\Exception\ValidationFailedException;
1920
use Bitbucket\HttpClient\Message\ResponseMediator;
@@ -63,7 +64,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
6364
*
6465
* @return \Bitbucket\Exception\ErrorException|\Bitbucket\Exception\RuntimeException
6566
*/
66-
private static function createException(int $status, string $message)
67+
private static function createException(int $status, string $message): ExceptionInterface
6768
{
6869
if (400 === $status || 422 === $status) {
6970
return new ValidationFailedException($message, $status);

src/HttpClient/Plugin/History.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final class History implements Journal
4040
*
4141
* @return \Psr\Http\Message\ResponseInterface|null
4242
*/
43-
public function getLastResponse()
43+
public function getLastResponse(): ?ResponseInterface
4444
{
4545
return $this->lastResponse;
4646
}

src/HttpClient/Util/JsonArray.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class JsonArray
3333
*
3434
* @return array
3535
*/
36-
public static function decode(string $json)
36+
public static function decode(string $json): array
3737
{
3838
/** @var scalar|array|null */
3939
$data = \json_decode($json, true);
@@ -58,7 +58,7 @@ public static function decode(string $json)
5858
*
5959
* @return string
6060
*/
61-
public static function encode(array $value)
61+
public static function encode(array $value): string
6262
{
6363
$json = \json_encode($value);
6464

0 commit comments

Comments
 (0)