Skip to content

Commit d4c730e

Browse files
committed
Improve static analysis
1 parent d87aa07 commit d4c730e

19 files changed

+38
-26
lines changed

src/Concerns/ParsesPages.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ trait ParsesPages
1717
/**
1818
* The number of items per page.
1919
*/
20-
protected readonly int $itemsPerPage;
20+
protected int $itemsPerPage;
2121

2222
/**
2323
* Yield paginated items and the given key from the provided response.
@@ -35,6 +35,7 @@ protected function yieldItemsAndGetKey(ResponseInterface $response, string $key)
3535

3636
foreach (JsonParser::parse($response)->pointers($pointers) as $item) {
3737
if (is_object($item)) {
38+
/** @var object{value: mixed} $item */
3839
$value = $item->value;
3940
} else {
4041
yield $item;
@@ -54,6 +55,7 @@ protected function yieldItemsAndGetKey(ResponseInterface $response, string $key)
5455
*/
5556
protected function yieldItemsFrom(mixed $source): Generator
5657
{
58+
/** @phpstan-ignore-next-line */
5759
yield from JsonParser::parse($source)->pointer($this->config->itemsPointer);
5860
}
5961
}

src/Concerns/ResolvesPages.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ protected function toPage(mixed $value, bool $onlyNumerics = true): string|int|n
2727
is_numeric($value) => (int) $value,
2828
!is_string($value) || $value === '' => null,
2929
!$this->isEndpoint($value) => $onlyNumerics ? null : $value,
30-
default => $this->pageFromParsedUri(parse_url($value), $onlyNumerics),
30+
default => $this->pageFromParsedUri(parse_url($value), $onlyNumerics), /** @phpstan-ignore-line */
3131
};
3232
}
3333

3434
/**
3535
* Retrieve the page from the given parsed URI.
3636
*
37+
* @param array{path?: string, query?: string} $parsedUri
3738
* @return ($onlyNumerics is true ? int|null : string|int|null)
3839
*/
3940
protected function pageFromParsedUri(array $parsedUri, bool $onlyNumerics = true): string|int|null
@@ -55,7 +56,9 @@ protected function pageFromParsedUri(array $parsedUri, bool $onlyNumerics = true
5556
protected function uriForPage(UriInterface $uri, string $page): UriInterface
5657
{
5758
if ($key = $this->config->offsetKey) {
58-
return Uri::withQueryValue($uri, $key, strval(($page - $this->config->firstPage) * $this->itemsPerPage));
59+
$value = (intval($page) - $this->config->firstPage) * $this->itemsPerPage;
60+
61+
return Uri::withQueryValue($uri, $key, strval($value));
5962
}
6063

6164
if (!$pattern = $this->config->pageInPath) {

src/Concerns/RetriesHttpRequests.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Cerbero\LazyJsonPages\Concerns;
66

77
use Cerbero\LazyJsonPages\Exceptions\OutOfAttemptsException;
8+
use Closure;
89
use Generator;
910
use GuzzleHttp\Exception\TransferException;
1011
use Illuminate\Support\LazyCollection;
@@ -20,10 +21,11 @@ trait RetriesHttpRequests
2021
/**
2122
* Retry to yield HTTP responses from the given callback.
2223
*
23-
* @param callable $callback
24-
* @return Generator<int, mixed>
24+
* @template TGen of Generator
25+
* @param Closure(): TGen $callback
26+
* @return TGen
2527
*/
26-
protected function retry(callable $callback): Generator
28+
protected function retry(Closure $callback): Generator
2729
{
2830
$attempt = 0;
2931
$remainingAttempts = $this->config->attempts;

src/Concerns/YieldsItemsByLength.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ trait YieldsItemsByLength
2020
* Yield paginated items until the page resolved from the given key is reached.
2121
*
2222
* @param ?Closure(int): int $callback
23-
* @return Generator<int, mixed, null, int>
23+
* @return Generator<int, mixed>
2424
*/
2525
protected function yieldItemsUntilKey(string $key, ?Closure $callback = null): Generator
2626
{
@@ -38,14 +38,17 @@ protected function yieldItemsUntilKey(string $key, ?Closure $callback = null): G
3838
/**
3939
* Yield paginated items until the resolved page is reached.
4040
*
41-
* @param Closure(ResponseInterface): Generator<int, mixed, null, int> $callback
41+
* @param Closure(ResponseInterface): Generator<int, mixed> $callback
4242
* @return Generator<int, mixed>
4343
*/
4444
protected function yieldItemsUntilPage(Closure $callback): Generator
4545
{
4646
yield from $generator = $callback($this->source->pullResponse());
4747

48-
foreach ($this->fetchPagesAsynchronously($generator->getReturn()) as $response) {
48+
/** @var int */
49+
$totalPages = $generator->getReturn();
50+
51+
foreach ($this->fetchPagesAsynchronously($totalPages) as $response) {
4952
yield from $this->yieldItemsFrom($response);
5053
}
5154
}

src/Data/RateLimit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function wasReached(): bool
5757
/**
5858
* Retrieve the timestamp when this rate limit resets.
5959
*/
60-
public function resetsAt(): float
60+
public function resetsAt(): ?float
6161
{
6262
return $this->resetsAt;
6363
}

src/Exceptions/OutOfAttemptsException.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Cerbero\LazyJsonPages\Exceptions;
44

5-
use Closure;
65
use GuzzleHttp\Exception\TransferException;
76
use Illuminate\Support\LazyCollection;
87

@@ -15,7 +14,7 @@ class OutOfAttemptsException extends LazyJsonPagesException
1514
* Instantiate the class.
1615
*
1716
* @param array<int, int> $failedPages
18-
* @param (Closure(): Generator<int, mixed>) $items
17+
* @param LazyCollection<int, mixed> $items
1918
*/
2019
public function __construct(
2120
TransferException $e,

src/LazyJsonPages.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,15 @@ public function onError(Closure $callback): self
277277
* Retrieve a lazy collection yielding the paginated items.
278278
*
279279
* @return LazyCollection<int, mixed>
280-
* @throws UnsupportedPaginationException
280+
* @throws \Cerbero\LazyJsonPages\Exceptions\UnsupportedPaginationException
281281
*/
282282
public function collect(string $dot = '*'): LazyCollection
283283
{
284284
$this->config[Config::OPTION_ITEMS_POINTER] = DotsConverter::toPointer($dot);
285285

286286
return new LazyCollection(function () {
287287
$client = $this->client->make();
288-
$config = new Config(...$this->config);
288+
$config = new Config(...$this->config); /** @phpstan-ignore-line */
289289
$source = (new AnySource($this->source))->setClient($client);
290290

291291
yield from new AnyPagination($source, $client, $config);

src/Middleware/Tap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __construct(private readonly TapCallbacks $callbacks) {}
5050
/**
5151
* Handle an HTTP request before and after it is sent.
5252
*
53-
* @param callable(RequestInterface, array): PromiseInterface $handler
53+
* @param callable(RequestInterface, array<string, mixed>): PromiseInterface $handler
5454
*/
5555
public function __invoke(callable $handler): Closure
5656
{

src/Paginations/CursorAwarePagination.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function matches(): bool
3434
public function getIterator(): Traversable
3535
{
3636
yield from $this->yieldItemsByCursor(function (ResponseInterface $response) {
37+
/** @phpstan-ignore-next-line */
3738
yield from $generator = $this->yieldItemsAndGetKey($response, $this->config->cursorKey);
3839

3940
return $generator->getReturn();

src/Paginations/LastPageAwarePagination.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function matches(): bool
2929
*/
3030
public function getIterator(): Traversable
3131
{
32+
/** @phpstan-ignore-next-line */
3233
yield from $this->yieldItemsUntilKey($this->config->lastPageKey, function (int $page) {
3334
return $this->config->firstPage === 0 ? $page + 1 : $page;
3435
});

0 commit comments

Comments
 (0)