Skip to content

Commit fdf5bf2

Browse files
committed
refactor(Response): Simplify exception handling and cleaner code
- Remove commented-out code for better readability - Update return type of object() method for clarity - Use a more concise format for invoking the callback in throw() method - Enhance throwIf() method signature to include optional callback
1 parent 339f9a3 commit fdf5bf2

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

src/Foundation/Response.php

+10-17
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,6 @@ public function __debugInfo(): array
8282
'handlerStats' => $this->handlerStats(),
8383
'requestSummary' => $this->request instanceof RequestInterface ? Message::toString($this->request) : null,
8484
'responseSummary' => Message::toString($this),
85-
// 'headers' => Arr::map(
86-
// $this->headers(),
87-
// fn (array $header, string $name): string => $this->getHeaderLine($name),
88-
// ),
89-
// 'status' => $this->status(),
90-
// 'reason' => $this->reason(),
91-
// 'body' => $this->body(),
9285
'decodedBody' => $this->json(),
9386
]);
9487
}
@@ -159,8 +152,10 @@ public function array(mixed $key = null, mixed $default = null): mixed
159152
* Get the JSON decoded body of the response as an object.
160153
*
161154
* @noinspection JsonEncodingApiUsageInspection
155+
*
156+
* @return mixed|object
162157
*/
163-
public function object(): ?object
158+
public function object(): mixed
164159
{
165160
return json_decode($this->body());
166161
}
@@ -267,7 +262,7 @@ public function saveAs(mixed $resourceOrPath): void
267262
fwrite($resource, $stream->read(1024));
268263
}
269264

270-
// rewind($resource);
265+
rewind($resource);
271266
fclose($resource);
272267
}
273268

@@ -454,15 +449,11 @@ public function toException(): ?RequestException
454449
*
455450
* @throws RequestException
456451
*/
457-
public function throw(): self
452+
public function throw(?callable $callback = null): self
458453
{
459-
$callback = \func_get_args()[0] ?? null;
460-
461454
if ($this->failed()) {
462455
throw tap($this->toException(), function (?RequestException $requestException) use ($callback): void {
463-
if ($callback && \is_callable($callback)) {
464-
$callback($this, $requestException);
465-
}
456+
$callback and $callback($this, $requestException);
466457
});
467458
}
468459

@@ -472,11 +463,13 @@ public function throw(): self
472463
/**
473464
* Throw an exception if a server or client error occurred and the given condition evaluates to true.
474465
*
466+
* @param bool|\Closure|mixed $condition
467+
*
475468
* @throws RequestException
476469
*/
477-
public function throwIf(bool|\Closure $condition): self
470+
public function throwIf(mixed $condition, ?callable $callback = null): self
478471
{
479-
return value($condition, $this) ? $this->throw(\func_get_args()[1] ?? null) : $this;
472+
return value($condition, $this) ? $this->throw($callback) : $this;
480473
}
481474

482475
/**

0 commit comments

Comments
 (0)