Page::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 9
dl 0
loc 21
rs 9.584
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace BenTools\OpenCubes\Component\Pager\Model;
4
5
use Psr\Http\Message\UriInterface;
6
use function BenTools\OpenCubes\stringify_uri;
7
8
final class Page implements \JsonSerializable
9
{
10
    /**
11
     * @var int
12
     */
13
    private $pageNumber;
14
15
    /**
16
     * @var int
17
     */
18
    private $nbItems;
19
20
    /**
21
     * @var int
22
     */
23
    private $offset;
24
25
    /**
26
     * @var bool
27
     */
28
    private $isFirstPage;
29
30
    /**
31
     * @var bool
32
     */
33
    private $isPreviousPage;
34
35
    /**
36
     * @var bool
37
     */
38
    private $isCurrentPage;
39
40
    /**
41
     * @var bool
42
     */
43
    private $isNextPage;
44
45
    /**
46
     * @var bool
47
     */
48
    private $isLastPage;
49
50
    /**
51
     * @var UriInterface
52
     */
53
    private $uri;
54
55
    /**
56
     * Page constructor.
57
     */
58
    public function __construct(
59
        int $pageNumber,
60
        int $nbItems,
61
        int $offset,
62
        bool $isFirstPage,
63
        bool $isPreviousPage,
64
        bool $isCurrentPage,
65
        bool $isNextPage,
66
        bool $isLastPage,
67
        UriInterface $uri
68
    ) {
69
        $this->pageNumber = $pageNumber;
70
        $this->nbItems = $nbItems;
71
        $this->offset = $offset;
72
        $this->isFirstPage = $isFirstPage;
73
        $this->isPreviousPage = $isPreviousPage;
74
        $this->isCurrentPage = $isCurrentPage;
75
        $this->isNextPage = $isNextPage;
76
        $this->isLastPage = $isLastPage;
77
        $this->uri = $uri;
78
    }
79
80
    /**
81
     * @return int
82
     */
83
    public function getPageNumber(): int
84
    {
85
        return $this->pageNumber;
86
    }
87
88
    /**
89
     * @return int
90
     */
91
    public function getNbItems(): int
92
    {
93
        return $this->nbItems;
94
    }
95
96
    /**
97
     * @return int
98
     */
99
    public function getOffset(): int
100
    {
101
        return $this->offset;
102
    }
103
104
    /**
105
     * @return bool
106
     */
107
    public function isFirstPage(): bool
108
    {
109
        return $this->isFirstPage;
110
    }
111
112
    /**
113
     * @return bool
114
     */
115
    public function isPreviousPage(): bool
116
    {
117
        return $this->isPreviousPage;
118
    }
119
120
    /**
121
     * @return bool
122
     */
123
    public function isCurrentPage(): bool
124
    {
125
        return $this->isCurrentPage;
126
    }
127
128
    /**
129
     * @return bool
130
     */
131
    public function isNextPage(): bool
132
    {
133
        return $this->isNextPage;
134
    }
135
136
    /**
137
     * @return bool
138
     */
139
    public function isLastPage(): bool
140
    {
141
        return $this->isLastPage;
142
    }
143
144
    /**
145
     * @return UriInterface
146
     */
147
    public function getUri(): UriInterface
148
    {
149
        return $this->uri;
150
    }
151
152
    /**
153
     * @inheritDoc
154
     */
155
    public function jsonSerialize()
156
    {
157
        return [
158
            'number'      => $this->getPageNumber(),
159
            'nb_items'    => $this->getNbItems(),
160
            'offset'      => $this->getOffset(),
161
            'is_first'    => $this->isFirstPage(),
162
            'is_previous' => $this->isPreviousPage(),
163
            'is_current'  => $this->isCurrentPage(),
164
            'is_next'     => $this->isNextPage(),
165
            'is_last'     => $this->isLastPage(),
166
            'link'        => stringify_uri($this->getUri()),
167
        ];
168
    }
169
}
170