Skip to content

Commit

Permalink
Fixed PhpStan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Sammyjo20 committed Aug 14, 2023
1 parent 5243246 commit 4bf0bd1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
18 changes: 18 additions & 0 deletions phpstan.baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
parameters:
ignoreErrors:
-
message: "#^Call to an undefined method Saloon\\\\PaginationPlugin\\\\Paginator\\:\\:getTotalPages\\(\\)\\.$#"
count: 2
path: src/Paginator.php
-
message: "#^Unable to resolve the template type TMakeKey in call to method static method Illuminate\\\\Support\\\\LazyCollection\\<\\(int\\|string\\)\\,mixed\\>\\:\\:make\\(\\)$#"
count: 1
path: src/Paginator.php
-
message: "#^Class Saloon\\\\PaginationPlugin\\\\Paginator implements generic interface Iterator but does not specify its types\\: TKey\\, TValue$#"
count: 1
path: src/Paginator.php
-
message: "#^Method Saloon\\\\PaginationPlugin\\\\Paginator\\:\\:collect\\(\\) return type with generic class Illuminate\\\\Support\\\\LazyCollection does not specify its types\\: TKey\\, TValue$#"
count: 1
path: src/Paginator.php
2 changes: 2 additions & 0 deletions src/Contracts/MapPaginatedResponseItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface MapPaginatedResponseItems
{
/**
* Map the items from the paginator
*
* @return array<mixed, mixed>
*/
public function mapPaginatedResponseItems(Response $response): array;
}
16 changes: 12 additions & 4 deletions src/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ public function __construct(Connector $connector, Request $request)
// are at the end of a page.

$this->request->middleware()
->onResponse(static fn (Response $response) => $response->throw())
->onResponse(function (Response $response) {
->onResponse(static fn (Response $response): Response => $response->throw())
->onResponse(function (Response $response): void {
$request = $response->getRequest();

$pageItems = $request instanceof MapPaginatedResponseItems
? $request->mapPaginatedResponseItems($response)
: $this->getPageItems($response, $request);

return $this->totalResults += count($pageItems);
$this->totalResults += count($pageItems);
});
}

Expand Down Expand Up @@ -173,6 +173,8 @@ protected function onRewind(): void

/**
* Iterate through the paginator items
*
* @return iterable<mixed, Response|PromiseInterface>
*/
public function items(): iterable
{
Expand All @@ -184,6 +186,7 @@ public function items(): iterable
return;
}

/** @var Response $response */
foreach ($this as $response) {
$request = $response->getRequest();

Expand Down Expand Up @@ -274,7 +277,10 @@ public function count(): int
// calls that we need to make.

if ($this->isAsyncPaginationEnabled() === true) {
return $this->getTotalPages($this->current()->wait());
/** @var PromiseInterface $promise */
$promise = $this->current();

return $this->getTotalPages($promise->wait());
}

// We are unable to use `iterator_count` because that method only calls
Expand Down Expand Up @@ -303,6 +309,8 @@ abstract protected function isLastPage(Response $response): bool;

/**
* Get the results from the page
*
* @return array<mixed, mixed>
*/
abstract protected function getPageItems(Response $response, Request $request): array;
}
7 changes: 5 additions & 2 deletions src/Traits/HasAsyncPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Saloon\PaginationPlugin\Traits;

use Saloon\Http\Pool;
use Saloon\Contracts\Pool;
use Saloon\Contracts\Response;

trait HasAsyncPagination
Expand All @@ -31,7 +31,10 @@ public function async(bool $async = true): static
*/
public function pool(callable|int $concurrency = 5, ?callable $responseHandler = null, ?callable $exceptionHandler = null): Pool
{
return $this->connector->pool($this->async(), $concurrency, $responseHandler, $exceptionHandler);
/** @var iterable<mixed, \Saloon\Contracts\Request> $iterator */
$iterator = clone $this->async();

return $this->connector->pool($iterator, $concurrency, $responseHandler, $exceptionHandler);
}

/**
Expand Down

0 comments on commit 4bf0bd1

Please sign in to comment.