8000 [Feature] Add a correct return type on Responses class (#536) · CodingSeo/laravel-json-api@e1fc46a · GitHub
[go: up one dir, main page]

Skip to content

Commit e1fc46a

Browse files
authored
[Feature] Add a correct return type on Responses class (cloudcreativity#536)
Adds correct return types on the Responses class.
1 parent 239db6e commit e1fc46a

File tree

1 file changed

+55
-38
lines changed

1 file changed

+55
-38
lines changed

src/Http/Responses/Responses.php

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
use CloudCreativity\LaravelJsonApi\Encoder\Neomerx\Factory;
2929
use CloudCreativity\LaravelJsonApi\Routing\Route;
3030
use CloudCreativity\LaravelJsonApi\Utils\Helpers;
31+
use Illuminate\Http\RedirectResponse;
3132
use Illuminate\Http\Response;
33+
use InvalidArgumentException;
3234
use Neomerx\JsonApi\Contracts\Document\DocumentInterface;
3335
use Neomerx\JsonApi\Contracts\Document\ErrorInterface;
3436
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
@@ -115,7 +117,7 @@ public function withCodec(Codec $codec): self
115117
public function withMediaType(string $mediaType): self
116118
{
117119
if (!$encoding = $this->api->getEncodings()->find($mediaType)) {
118-
throw new \InvalidArgumentException(
120+
throw new InvalidArgumentException(
119121
"Media type {$mediaType} is not valid for API {$this->api->getName()}."
120122
);
121123
}
@@ -135,13 +137,13 @@ public function withMediaType(string $mediaType): self
135137
* @param int $options
136138
* @param int $depth
137139
* @param string|null $mediaType
138-
* @return Responses
140+
* @return $this
139141
*/
140142
public function withEncoding(
141143
int $options = 0,
142144
int $depth = 512,
143145
string $mediaType = MediaTypeInterface::JSON_API_MEDIA_TYPE
144-
) {
146+
): self {
145147
$encoding = Encoding::create(
146148
$mediaType,
147149
$options,
@@ -174,18 +176,18 @@ public function withEncodingParameters(?EncodingParametersInterface $parameters)
174176
/**
175177
* @param $statusCode
176178
* @param array $headers
177-
* @return mixed
179+
* @return Response
178180
*/
179-
public function statusCode($statusCode, array $headers = [])
181+
public function statusCode($statusCode, array $headers = []): Response
180182
{
181183
return $this->getCodeResponse($statusCode, $headers);
182184
}
183185

184186
/**
185187
* @param array $headers
186-
* @return mixed
188+
* @return Response
187189
*/
188-
public function noContent(array $headers = [])
190+
public function noContent(array $headers = []): Response
189191
{
190192
return $this->getCodeResponse(204, $headers);
191193
}
@@ -194,9 +196,9 @@ public function noContent(array $headers = [])
194196
* @param $meta
195197
* @param int $statusCode
196198
* @param array $headers
197-
* @return mixed
199+
* @return Response
198200
*/
199-
public function meta($meta, $statusCode = self::HTTP_OK, array $headers = [])
201+
public function meta($meta, $statusCode = self::HTTP_OK, array $headers = []): Response
200202
{
201203
return $this->getMetaResponse($meta, $statusCode, $headers);
202204
}
@@ -206,9 +208,9 @@ public function meta($meta, $statusCode = self::HTTP_OK, array $headers = [])
206208
* @param $meta
207209
* @param int $statusCode
208210
* @param array $headers
209-
* @return mixed
211+
* @return Response
210212
*/
211-
public function noData(array $links = [], $meta = null, $statusCode = self::HTTP_OK, array $headers = [])
213+
public function noData(array $links = [], $meta = null, $statusCode = self::HTTP_OK, array $headers = []): Response
212214
{
213215
$encoder = $this->getEncoder();
214216
$content = $encoder->withLinks($links)->encodeMeta($meta ?: []);
@@ -222,30 +224,38 @@ public function noData(array $links = [], $meta = null, $statusCode = self::HTTP
222224
* @param mixed $meta
223225
* @param int $statusCode
224226
* @param array $headers
225-
* @return mixed
227+
* @return Response
226228
*/
227229
public function content(
228230
$data,
229231
array $links = [],
230232
$meta = null,
231233
$statusCode = self::HTTP_OK,
232234
array $headers = []
233-
) {
235+
): Response {
234236
return $this->getContentResponse($data, $statusCode, $links, $meta, $headers);
235237
}
236238

239+
237240
/**
238-
* @inheritdoc
241+
* Get response with regular JSON API Document in body.
242+
*
243+
* @param array|object $data
244+
* @param int $statusCode
245+
* @param null $links
246+
* @param null $meta
247+
* @param array $headers
248+
* @return Response
239249
*/
240250
public function getContentResponse(
241251
$data,
242252
$statusCode = self::HTTP_OK,
243253
$links = null,
244254
$meta = null,
245255
array $headers = []
246-
) {
256+
): Response {
247257
if ($data instanceof PageInterface) {
248-
list ($data, $meta, $links) = $this->extractPage($data, $meta, $links);
258+
[$data, $meta, $links] = $this->extractPage($data, $meta, $links);
249259
}
250260

251261
return parent::getContentResponse($data, $statusCode, $links, $meta, $headers);
@@ -256,9 +266,9 @@ public function getContentResponse(
256266
* @param array $links
257267
* @param mixed $meta
258268
* @param array $headers
259-
* @return mixed
269+
* @return Response
260270
*/
261-
public function created($resource = null, array $links = [], $meta = null, array $headers = [])
271+
public function created($resource = null, array $links = [], $meta = null, array $headers = []): Response
262272
{
263273
if ($this->isNoContent($resource, $links, $meta)) {
264274
return $this->noContent();
@@ -282,14 +292,14 @@ public function created($resource = null, array $links = [], $meta = null, array
282292
* @param array $links
283293
* @param mixed $meta
284294
* @param array $headers
285-
* @return mixed
295+
* @return Response
286296
*/
287297
public function updated(
288298
$resource = null,
289299
array $links = [],
290300
$meta = null,
291301
array $headers = []
292-
) {
302+
): Response {
293303
return $this->getResourceResponse($resource, $links, $meta, $headers);
294304
}
295305

@@ -300,14 +310,14 @@ public function updated(
300310
* @param array $links
301311
* @param mixed|null $meta
302312
* @param array $headers
303-
* @return mixed
313+
* @return Response
304314
*/
305315
public function deleted(
306316
$resource = null,
307317
array $links = [],
308318
$meta = null,
309319
array $headers = []
310-
) {
320+
): Response {
311321
return $this->getResourceResponse($resource, $links, $meta, $headers);
312322
}
313323

@@ -316,9 +326,9 @@ public function deleted(
316326
* @param array $links
317327
* @param null $meta
318328
* @param array $headers
319-
* @return mixed
329+
* @return Response
320330
*/
321-
public function accepted(AsynchronousProcess $job, array $links = [], $meta = null, array $headers = [])
331+
public function accepted(AsynchronousProcess $job, array $links = [], $meta = null, array $headers = []): Response
322332
{
323333
$headers['Content-Location'] = $this->getResourceLocationUrl($job);
324334

@@ -330,7 +340,7 @@ public function accepted(AsynchronousProcess $job, array $links = [], $meta = nu
330340
* @param array $links
331341
* @param null $meta
332342
* @param array $headers
333-
* @return \Illuminate\Http\RedirectResponse|mixed
343+
* @return RedirectResponse|mixed
334344
*/
335345
public function process(AsynchronousProcess $job, array $links = [], $meta = null, array $headers = [])
336346
{
@@ -348,15 +358,15 @@ public function process(AsynchronousProcess $job, array $links = [], $meta = nul
348358
* @param mixed $meta
349359
* @param int $statusCode
350360
* @param array $headers
351-
* @return mixed
361+
* @return Response
352362
*/
353363
public function relationship(
354364
$data,
355365
array $links = [],
356366
$meta = null,
357367
$statusCode = 200,
358368
array $headers = []
359-
) {
369+
): Response {
360370
return $this->getIdentifiersResponse($data, $statusCode, $links, $meta, $headers);
361371
}
362372

@@ -366,17 +376,17 @@ public function relationship(
366376
* @param $links
367377
* @param $meta
368378
* @param array $headers
369-
* @return mixed
379+
* @return Response
370380
*/
371381
public function getIdentifiersResponse(
372382
$data,
373383
$statusCode = self::HTTP_OK,
374384
$links = null,
375385
$meta = null,
376386
array $headers = []
377-
) {
387+
): Response {
378388
if ($data instanceof PageInterface) {
379-
list ($data, $meta, $links) = $this->extractPage($data, $meta, $links);
389+
[$data, $meta, $links] = $this->extractPage($data, $meta, $links);
380390
}
381391

382392
return parent::getIdentifiersResponse($data, $statusCode, $links, $meta, $headers);
@@ -388,9 +398,9 @@ public function getIdentifiersResponse(
388398
* @param Error|ErrorInterface|array $error
389399
* @param int|null $defaultStatusCode
390400
* @param array $headers
391-
* @return mixed
401+
* @return Response
392402
*/
393-
public function error($error, int $defaultStatusCode = null, array $headers = [])
403+
public function error($error, int $defaultStatusCode = null, array $headers = []): Response
394404
{
395405
if (!$error instanceof ErrorInterface) {
396406
$error = $this->factory->createError(
@@ -411,9 +421,10 @@ public function error($error, int $defaultStatusCode = null, array $headers = []
411421
* @param iterable $errors
412422
* @param int|null $defaultStatusCode
413423
* @param array $headers
414-
* @return mixed
424+
*
425+
* @return Response
415426
*/
416-
public function errors(iterable $errors, int $defaultStatusCode = null, array $headers = [])
427+
public function errors(iterable $errors, int $defaultStatusCode = null, array $headers = []): Response
417428
{
418429
$errors = $this->factory->createErrors($errors);
419430
$statusCode = Helpers::httpErrorStatus($errors, $defaultStatusCode);
@@ -426,9 +437,9 @@ public function errors(iterable $errors, int $defaultStatusCode = null, array $h
426437
* @param array $links
427438
* @param null $meta
428439
* @param array $headers
429-
* @return mixed
440+
* @return Response
430441
*/
431-
protected function getResourceResponse($resource, array $links = [], $meta = null, array $headers = [])
442+
protected function getResourceResponse($resource, array $links = [], $meta = null, array $headers = []): Response
432443
{
433444
if ($this->isNoContent($resource, $links, $meta)) {
434445
return $this->noContent();
@@ -518,9 +529,15 @@ protected function getSupportedExtensions()
518529
}
519530

520531
/**
521-
* @inheritdoc
532+
* Create HTTP response.
533+
*
534+
* @param string|null $content
535+
* @param int $statusCode
536+
* @param array $headers
537+
*
538+
* @return Response
522539
*/
523-
protected function createResponse($content, $statusCode, array $headers)
540+
protected function createResponse($content, $statusCode, array $headers): Response
524541
{
525542
return response($content, $statusCode, $headers);
526543
}

0 commit comments

Comments
 (0)
0