TotalItemsAwarePagination::matches()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
nc 3
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\LazyJsonPages\Paginations;
6
7
use Cerbero\LazyJsonPages\Concerns\YieldsItemsByLength;
8
use Traversable;
9
10
/**
11
 * The pagination aware of the total number of items.
12
 */
13
class TotalItemsAwarePagination extends Pagination
14
{
15
    use YieldsItemsByLength;
0 ignored issues
show
introduced by
The trait Cerbero\LazyJsonPages\Concerns\YieldsItemsByLength requires some properties which are not provided by Cerbero\LazyJsonPages\Pa...talItemsAwarePagination: $attempts, $backoff, $firstPage, $async, $rateLimits
Loading history...
16
17
    /**
18
     * Determine whether this pagination matches the configuration.
19
     */
20 34
    public function matches(): bool
21
    {
22 34
        return $this->config->totalItemsKey !== null
23 34
            && $this->config->totalPagesKey === null
24 34
            && $this->config->lastPageKey === null;
25
    }
26
27
    /**
28
     * Yield the paginated items.
29
     *
30
     * @return Traversable<int, mixed>
31
     */
32 4
    public function getIterator(): Traversable
33
    {
34
        /** @phpstan-ignore-next-line */
35 4
        yield from $this->yieldItemsUntilKey($this->config->totalItemsKey, function (int $totalItems) {
0 ignored issues
show
Bug introduced by
It seems like $this->config->totalItemsKey can also be of type null; however, parameter $key of Cerbero\LazyJsonPages\Pa...n::yieldItemsUntilKey() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

35
        yield from $this->yieldItemsUntilKey(/** @scrutinizer ignore-type */ $this->config->totalItemsKey, function (int $totalItems) {
Loading history...
36 4
            return $this->itemsPerPage > 0 ? (int) ceil($totalItems / $this->itemsPerPage) : 0;
37 4
        });
38
    }
39
}
40