Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Jan 11, 2024
1 parent 63900f4 commit 8017d70
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/Concerns/PaginationLengthAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ protected function itemsByTotalPages(int $pages, ?UriInterface $uri = null): Tra
/**
* Retrieve the given pages in chunks.
*
* @return LazyCollection<int, int[]>
* @return LazyCollection<int, LazyCollection<int, int>>
*/
protected function chunkPages(int $pages, bool $shouldSkipFirstPage): LazyCollection
{
$firstPage = $shouldSkipFirstPage ? $this->config->firstPage + 1 : $this->config->firstPage;
$lastPage = $this->config->firstPage == 0 ? $pages - 1 : $pages;

return LazyCollection::range($firstPage, $lastPage)->chunk($this->config->async ?: INF);
return LazyCollection::range($firstPage, $lastPage)->chunk($this->config->async);
}
}
5 changes: 4 additions & 1 deletion src/Concerns/RetriesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ trait RetriesHttpRequests
/**
* Retry to return HTTP responses from the given callback.
*
* @param Closure(Outcome) $callback
* @template TReturn
*
* @param (Closure(Outcome): TReturn) $callback
* @return TReturn
*/
protected function retry(Closure $callback): mixed
{
Expand Down
10 changes: 6 additions & 4 deletions src/Concerns/SendsAsyncRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait SendsAsyncRequests
/**
* Fetch items by performing asynchronous HTTP calls.
*
* @param LazyCollection<int, int[]> $chunkedPages
* @param LazyCollection<int, LazyCollection<int, int>> $chunkedPages
* @return Traversable<int, mixed>
*/
protected function fetchItemsAsynchronously(LazyCollection $chunkedPages, UriInterface $uri): Traversable
Expand All @@ -37,8 +37,8 @@ protected function fetchItemsAsynchronously(LazyCollection $chunkedPages, UriInt
]);

foreach ($chunkedPages as $pages) {
$outcome = $this->retry(function (Outcome $outcome) use ($uri, $client, $pages) {
$pages = $outcome->pullFailedPages() ?: $pages;
$outcome = $this->retry(function (Outcome $outcome) use ($uri, $client, $pages): Outcome {
$pages = $outcome->pullFailedPages() ?: $pages->all();

return $this->pool($client, $outcome, $this->yieldRequests($uri, $pages));
});
Expand Down Expand Up @@ -75,7 +75,9 @@ protected function pool(Client $client, Outcome $outcome, Generator $requests):
$pool = new Pool($client, $requests, [
'concurrency' => $this->config->async,
'fulfilled' => function (ResponseInterface $response, int $page) use ($outcome) {
$outcome->addItemsFromPage($page, JsonParser::parse($response)->pointer($this->config->pointer));
/** @var Traversable<int, mixed> $items */
$items = JsonParser::parse($response)->pointer($this->config->pointer);
$outcome->addItemsFromPage($page, $items);
},
'rejected' => function (Throwable $e, int $page) use ($outcome) {
$outcome->addFailedPage($page);
Expand Down
4 changes: 2 additions & 2 deletions src/Dtos/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function __construct(
public readonly ?string $nextPageKey,
public readonly ?int $lastPage,
public readonly int $async,
public readonly int $connectionTimeout,
public readonly int $requestTimeout,
public readonly float|int $connectionTimeout,
public readonly float|int $requestTimeout,
public readonly int $attempts,
public readonly ?Closure $backoff,
) {}
Expand Down
6 changes: 1 addition & 5 deletions src/Exceptions/LazyJsonPagesException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
namespace Cerbero\LazyJsonPages\Exceptions;

use Exception;
use Throwable;

class LazyJsonPagesException extends Exception
{
public static function from(Throwable $e): static
{
return $e instanceof static ? $e : new static($e->getMessage());
}
//
}
4 changes: 2 additions & 2 deletions src/Services/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ final class Api
/**
* The server connection timeout in seconds.
*/
private int $connectionTimeout = 5;
private float|int $connectionTimeout = 5;

/**
* The HTTP request timeout in seconds.
*/
private int $requestTimeout = 5;
private float|int $requestTimeout = 5;

/**
* The number of attempts to fetch pages.
Expand Down
2 changes: 1 addition & 1 deletion src/Sources/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Endpoint extends Source
public function matches(): bool
{
return $this->source instanceof UriInterface
|| (is_string($this->source) && $this->isEndpoint($this->source));
|| $this->isEndpoint($this->source);
}

/**
Expand Down
18 changes: 12 additions & 6 deletions src/ValueObjects/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Cerbero\JsonParser\JsonParser;
use Cerbero\LazyJson\Pointers\DotsConverter;
use Traversable;

/**
* The HTTP response.
Expand All @@ -15,14 +16,14 @@ final class Response
/**
* The headers of the HTTP response.
*
* @var array<string, string> $headers
* @var array<string, string[]> $headers
*/
public readonly array $headers;

/**
* Instantiate the class.
*
* @param array<string, string> $headers
* @param array<string, string[]> $headers
*/
public function __construct(public readonly string $json, array $headers)
{
Expand All @@ -32,8 +33,8 @@ public function __construct(public readonly string $json, array $headers)
/**
* Normalize the given headers.
*
* @param array<string, string> $headers
* @return array<string, string>
* @param array<string, string[]> $headers
* @return array<string, string[]>
*/
private function normalizeHeaders(array $headers): array
{
Expand Down Expand Up @@ -64,8 +65,10 @@ public function hasHeader(string $header): bool

/**
* Retrieve the given header.
*
* @return ?string[]
*/
public function header(string $header): ?string
public function header(string $header): ?array
{
return $this->headers[strtolower($header)] ?? null;
}
Expand All @@ -83,9 +86,12 @@ public function json(string $key): mixed

/**
* Retrieve an iterator with the given JSON pointer.
*
* @return Traversable<int, mixed>
*/
public function pointer(string $pointer): JsonParser
public function pointer(string $pointer): Traversable
{
/** @var Traversable<int, mixed> */
return JsonParser::parse($this->json)->pointer($pointer);
}
}

0 comments on commit 8017d70

Please sign in to comment.