Issues (46)

src/Concerns/YieldsItemsByLength.php (3 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\LazyJsonPages\Concerns;
6
7
use Cerbero\LazyJsonPages\Exceptions\InvalidKeyException;
8
use Closure;
9
use Generator;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * The trait to yield items from length-aware paginations.
14
 */
15
trait YieldsItemsByLength
16
{
17
    use SendsAsyncRequests;
18
19
    /**
20
     * Yield paginated items until the page resolved from the given key is reached.
21
     *
22
     * @param ?Closure(int): int $callback
23
     * @return Generator<int, mixed>
24
     */
25 42
    protected function yieldItemsUntilKey(string $key, ?Closure $callback = null): Generator
26
    {
27 42
        yield from $this->yieldItemsUntilPage(function (ResponseInterface $response) use ($key, $callback) {
28 38
            yield from $generator = $this->yieldItemsAndGetKey($response, $key);
0 ignored issues
show
The method yieldItemsAndGetKey() does not exist on Cerbero\LazyJsonPages\Concerns\YieldsItemsByLength. Did you maybe mean yieldItemsUntilKey()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
            yield from $generator = $this->/** @scrutinizer ignore-call */ yieldItemsAndGetKey($response, $key);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
30 38
            if (!is_int($page = $this->toPage($generator->getReturn()))) {
0 ignored issues
show
It seems like toPage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
            if (!is_int($page = $this->/** @scrutinizer ignore-call */ toPage($generator->getReturn()))) {
Loading history...
31 1
                throw new InvalidKeyException($key);
32
            }
33
34 37
            return $callback ? $callback($page) : $page;
35 42
        });
36
    }
37
38
    /**
39
     * Yield paginated items until the resolved page is reached.
40
     *
41
     * @param Closure(ResponseInterface): Generator<int, mixed> $callback
42
     * @return Generator<int, mixed>
43
     */
44 44
    protected function yieldItemsUntilPage(Closure $callback): Generator
45
    {
46 44
        yield from $generator = $callback($this->source->pullResponse());
47
48
        /** @var int */
49 39
        $totalPages = $generator->getReturn();
50
51 39
        foreach ($this->fetchPagesAsynchronously($totalPages) as $response) {
52 37
            yield from $this->yieldItemsFrom($response);
0 ignored issues
show
It seems like yieldItemsFrom() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
            yield from $this->/** @scrutinizer ignore-call */ yieldItemsFrom($response);
Loading history...
53
        }
54
    }
55
}
56