Passed
Pull Request — master (#48)
by Chito
12:37
created

PaginationResult   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
eloc 31
c 1
b 0
f 1
dl 0
loc 121
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A pagingParam() 0 3 1
A jsonSerialize() 0 8 1
A totalCount() 0 3 1
A hasPrevPage() 0 3 1
A currentPage() 0 3 1
A __debugInfo() 0 5 1
A perPage() 0 3 1
A hasNextPage() 0 3 1
A pageCount() 0 3 1
A items() 0 7 2
A count() 0 4 2
A pagingParams() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lampager\Cake;
6
7
use Cake\Datasource\Paging\PaginatedInterface;
8
use Countable;
9
use JsonSerializable;
10
use Lampager\PaginationResult as LampagerPaginationResult;
11
12
/**
13
 * Class PaginationResult
14
 */
15
class PaginationResult extends LampagerPaginationResult implements JsonSerializable, PaginatedInterface
16
{
17
    protected ?iterable $iterator = null;
18
19
    protected int $limit;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function currentPage(): int
25
    {
26
        return 0;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function perPage(): int
33
    {
34
        return $this->limit;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function totalCount(): ?int
41
    {
42
        return null;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function pageCount(): ?int
49
    {
50
        return null;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function hasPrevPage(): bool
57
    {
58
        return (bool)$this->hasPrevious;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function hasNextPage(): bool
65
    {
66
        return (bool)$this->hasNext;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function items(): iterable
73
    {
74
        if (!$this->iterator) {
75
            $this->iterator = $this->getIterator();
76
        }
77
78
        return $this->iterator;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function pagingParam(string $name): mixed
85
    {
86
        return $this->pagingParams()[$name] ?? null;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function pagingParams(): array
93
    {
94
        return [
95
            'count' => $this->count(),
96
            'totalCount' => $this->totalCount(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->totalCount() targeting Lampager\Cake\PaginationResult::totalCount() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
97
            'perPage' => $this->perPage(),
98
            'pageCount' => $this->pageCount(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->pageCount() targeting Lampager\Cake\PaginationResult::pageCount() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
99
            'currentPage' => $this->currentPage(),
100
            'hasPrevPage' => $this->hasPrevPage(),
101
            'hasNextPage' => $this->hasNextPage(),
102
        ];
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function jsonSerialize(): array
109
    {
110
        $array = get_object_vars($this);
111
        $array['records'] = iterator_to_array($this->items());
112
        unset($array['iterator']);
113
        unset($array['limit']);
114
115
        return $array;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function count(): int
122
    {
123
        $items = $this->items();
124
        return $items instanceof Countable ? count($items) : iterator_count($items);
125
    }
126
127
    /**
128
     * Returns an array that can be used to describe the internal state of this
129
     * object.
130
     */
131
    public function __debugInfo(): array
132
    {
133
        return [
134
            '(help)' => 'This is a Lampager Pagination Result object.',
135
        ] + $this->jsonSerialize();
136
    }
137
}
138