Complex classes like Pagination often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Pagination, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
76 | class Pagination extends BaseObject implements Linkable |
||
77 | { |
||
78 | const LINK_NEXT = 'next'; |
||
79 | const LINK_PREV = 'prev'; |
||
80 | const LINK_FIRST = 'first'; |
||
81 | const LINK_LAST = 'last'; |
||
82 | |||
83 | /** |
||
84 | * @var string name of the parameter storing the current page index. |
||
85 | * @see params |
||
86 | */ |
||
87 | public $pageParam = 'page'; |
||
88 | /** |
||
89 | * @var string name of the parameter storing the page size. |
||
90 | * @see params |
||
91 | */ |
||
92 | public $pageSizeParam = 'per-page'; |
||
93 | /** |
||
94 | * @var bool whether to always have the page parameter in the URL created by [[createUrl()]]. |
||
95 | * If false and [[page]] is 0, the page parameter will not be put in the URL. |
||
96 | */ |
||
97 | public $forcePageParam = true; |
||
98 | /** |
||
99 | * @var string the route of the controller action for displaying the paged contents. |
||
100 | * If not set, it means using the currently requested route. |
||
101 | */ |
||
102 | public $route; |
||
103 | /** |
||
104 | * @var array parameters (name => value) that should be used to obtain the current page number |
||
105 | * and to create new pagination URLs. If not set, all parameters from $_GET will be used instead. |
||
106 | * |
||
107 | * In order to add hash to all links use `array_merge($_GET, ['#' => 'my-hash'])`. |
||
108 | * |
||
109 | * The array element indexed by [[pageParam]] is considered to be the current page number (defaults to 0); |
||
110 | * while the element indexed by [[pageSizeParam]] is treated as the page size (defaults to [[defaultPageSize]]). |
||
111 | */ |
||
112 | public $params; |
||
113 | /** |
||
114 | * @var \yii\web\UrlManager the URL manager used for creating pagination URLs. If not set, |
||
115 | * the "urlManager" application component will be used. |
||
116 | */ |
||
117 | public $urlManager; |
||
118 | /** |
||
119 | * @var bool whether to check if [[page]] is within valid range. |
||
120 | * When this property is true, the value of [[page]] will always be between 0 and ([[pageCount]]-1). |
||
121 | * Because [[pageCount]] relies on the correct value of [[totalCount]] which may not be available |
||
122 | * in some cases (e.g. MongoDB), you may want to set this property to be false to disable the page |
||
123 | * number validation. By doing so, [[page]] will return the value indexed by [[pageParam]] in [[params]]. |
||
124 | */ |
||
125 | public $validatePage = true; |
||
126 | /** |
||
127 | * @var int total number of items. |
||
128 | */ |
||
129 | public $totalCount = 0; |
||
130 | /** |
||
131 | * @var int the default page size. This property will be returned by [[pageSize]] when page size |
||
132 | * cannot be determined by [[pageSizeParam]] from [[params]]. |
||
133 | */ |
||
134 | public $defaultPageSize = 20; |
||
135 | /** |
||
136 | * @var array|false the page size limits. The first array element stands for the minimal page size, and the second |
||
137 | * the maximal page size. If this is false, it means [[pageSize]] should always return the value of [[defaultPageSize]]. |
||
138 | */ |
||
139 | public $pageSizeLimit = [1, 50]; |
||
140 | |||
141 | /** |
||
142 | * @var int number of items on each page. |
||
143 | * If it is less than 1, it means the page size is infinite, and thus a single page contains all items. |
||
144 | */ |
||
145 | private $_pageSize; |
||
146 | |||
147 | |||
148 | /** |
||
149 | * @return int number of pages |
||
150 | */ |
||
151 | 55 | public function getPageCount() |
|
162 | |||
163 | private $_page; |
||
164 | |||
165 | /** |
||
166 | * Returns the zero-based current page number. |
||
167 | * @param bool $recalculate whether to recalculate the current page based on the page size and item count. |
||
168 | * @return int the zero-based current page number. |
||
169 | */ |
||
170 | 52 | public function getPage($recalculate = false) |
|
179 | |||
180 | /** |
||
181 | * Sets the current page number. |
||
182 | * @param int $value the zero-based index of the current page. |
||
183 | * @param bool $validatePage whether to validate the page number. Note that in order |
||
184 | * to validate the page number, both [[validatePage]] and this parameter must be true. |
||
185 | */ |
||
186 | 52 | public function setPage($value, $validatePage = false) |
|
204 | |||
205 | /** |
||
206 | * Returns the number of items per page. |
||
207 | * By default, this method will try to determine the page size by [[pageSizeParam]] in [[params]]. |
||
208 | * If the page size cannot be determined this way, [[defaultPageSize]] will be returned. |
||
209 | * @return int the number of items per page. If it is less than 1, it means the page size is infinite, |
||
210 | * and thus a single page contains all items. |
||
211 | * @see pageSizeLimit |
||
212 | */ |
||
213 | 58 | public function getPageSize() |
|
227 | |||
228 | /** |
||
229 | * @param int $value the number of items per page. |
||
230 | * @param bool $validatePageSize whether to validate page size. |
||
231 | */ |
||
232 | 61 | public function setPageSize($value, $validatePageSize = false) |
|
248 | |||
249 | /** |
||
250 | * Creates the URL suitable for pagination with the specified page number. |
||
251 | * This method is mainly called by pagers when creating URLs used to perform pagination. |
||
252 | * @param int $page the zero-based page number that the URL should point to. |
||
253 | * @param int $pageSize the number of items on each page. If not set, the value of [[pageSize]] will be used. |
||
254 | * @param bool $absolute whether to create an absolute URL. Defaults to `false`. |
||
255 | * @return string the created URL |
||
256 | * @see params |
||
257 | * @see forcePageParam |
||
258 | */ |
||
259 | 12 | public function createUrl($page, $pageSize = null, $absolute = false) |
|
260 | { |
||
261 | 12 | $page = (int) $page; |
|
262 | 12 | $pageSize = (int) $pageSize; |
|
263 | 12 | if (($params = $this->params) === null) { |
|
264 | 10 | $request = Yii::$app->getRequest(); |
|
265 | 10 | $params = $request instanceof Request ? $request->getQueryParams() : []; |
|
266 | } |
||
267 | 12 | if ($page > 0 || $page == 0 && $this->forcePageParam) { |
|
268 | 12 | $params[$this->pageParam] = $page + 1; |
|
269 | } else { |
||
270 | 1 | unset($params[$this->pageParam]); |
|
271 | } |
||
272 | 12 | if ($pageSize <= 0) { |
|
273 | 10 | $pageSize = $this->getPageSize(); |
|
274 | } |
||
275 | 12 | if ($pageSize != $this->defaultPageSize) { |
|
276 | 2 | $params[$this->pageSizeParam] = $pageSize; |
|
277 | } else { |
||
278 | 10 | unset($params[$this->pageSizeParam]); |
|
279 | } |
||
280 | 12 | $params[0] = $this->route === null ? Yii::$app->controller->getRoute() : $this->route; |
|
281 | 12 | $urlManager = $this->urlManager === null ? Yii::$app->getUrlManager() : $this->urlManager; |
|
282 | 12 | if ($absolute) { |
|
283 | return $urlManager->createAbsoluteUrl($params); |
||
284 | } |
||
285 | |||
286 | 12 | return $urlManager->createUrl($params); |
|
287 | } |
||
288 | |||
289 | /** |
||
290 | * @return int the offset of the data. This may be used to set the |
||
291 | * OFFSET value for a SQL statement for fetching the current page of data. |
||
292 | */ |
||
293 | 45 | public function getOffset() |
|
299 | |||
300 | /** |
||
301 | * @return int the limit of the data. This may be used to set the |
||
302 | * LIMIT value for a SQL statement for fetching the current page of data. |
||
303 | * Note that if the page size is infinite, a value -1 will be returned. |
||
304 | */ |
||
305 | 45 | public function getLimit() |
|
311 | |||
312 | /** |
||
313 | * Returns a whole set of links for navigating to the first, last, next and previous pages. |
||
314 | * @param bool $absolute whether the generated URLs should be absolute. |
||
315 | * @return array the links for navigational purpose. The array keys specify the purpose of the links (e.g. [[LINK_FIRST]]), |
||
316 | * and the array values are the corresponding URLs. |
||
317 | */ |
||
318 | public function getLinks($absolute = false) |
||
319 | { |
||
320 | $currentPage = $this->getPage(); |
||
321 | $pageCount = $this->getPageCount(); |
||
322 | $links = [ |
||
323 | Link::REL_SELF => $this->createUrl($currentPage, null, $absolute), |
||
324 | ]; |
||
325 | if ($currentPage > 0) { |
||
326 | $links[self::LINK_FIRST] = $this->createUrl(0, null, $absolute); |
||
327 | $links[self::LINK_PREV] = $this->createUrl($currentPage - 1, null, $absolute); |
||
328 | } |
||
329 | if ($currentPage < $pageCount - 1) { |
||
330 | $links[self::LINK_NEXT] = $this->createUrl($currentPage + 1, null, $absolute); |
||
331 | $links[self::LINK_LAST] = $this->createUrl($pageCount - 1, null, $absolute); |
||
332 | } |
||
333 | |||
334 | return $links; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Returns the value of the specified query parameter. |
||
339 | * This method returns the named parameter value from [[params]]. Null is returned if the value does not exist. |
||
340 | * @param string $name the parameter name |
||
341 | * @param string $defaultValue the value to be returned when the specified parameter does not exist in [[params]]. |
||
342 | * @return string the parameter value |
||
343 | */ |
||
344 | 58 | protected function getQueryParam($name, $defaultValue = null) |
|
353 | } |
||
354 |