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 |
||
74 | class Pagination extends Object implements Linkable |
||
75 | { |
||
76 | const LINK_NEXT = 'next'; |
||
77 | const LINK_PREV = 'prev'; |
||
78 | const LINK_FIRST = 'first'; |
||
79 | const LINK_LAST = 'last'; |
||
80 | |||
81 | /** |
||
82 | * @var string name of the parameter storing the current page index. |
||
83 | * @see params |
||
84 | */ |
||
85 | public $pageParam = 'page'; |
||
86 | /** |
||
87 | * @var string name of the parameter storing the page size. |
||
88 | * @see params |
||
89 | */ |
||
90 | public $pageSizeParam = 'per-page'; |
||
91 | /** |
||
92 | * @var boolean whether to always have the page parameter in the URL created by [[createUrl()]]. |
||
93 | * If false and [[page]] is 0, the page parameter will not be put in the URL. |
||
94 | */ |
||
95 | public $forcePageParam = true; |
||
96 | /** |
||
97 | * @var string the route of the controller action for displaying the paged contents. |
||
98 | * If not set, it means using the currently requested route. |
||
99 | */ |
||
100 | public $route; |
||
101 | /** |
||
102 | * @var array parameters (name => value) that should be used to obtain the current page number |
||
103 | * and to create new pagination URLs. If not set, all parameters from $_GET will be used instead. |
||
104 | * |
||
105 | * In order to add hash to all links use `array_merge($_GET, ['#' => 'my-hash'])`. |
||
106 | * |
||
107 | * The array element indexed by [[pageParam]] is considered to be the current page number (defaults to 0); |
||
108 | * while the element indexed by [[pageSizeParam]] is treated as the page size (defaults to [[defaultPageSize]]). |
||
109 | */ |
||
110 | public $params; |
||
111 | /** |
||
112 | * @var \yii\web\UrlManager the URL manager used for creating pagination URLs. If not set, |
||
113 | * the "urlManager" application component will be used. |
||
114 | */ |
||
115 | public $urlManager; |
||
116 | /** |
||
117 | * @var boolean whether to check if [[page]] is within valid range. |
||
118 | * When this property is true, the value of [[page]] will always be between 0 and ([[pageCount]]-1). |
||
119 | * Because [[pageCount]] relies on the correct value of [[totalCount]] which may not be available |
||
120 | * in some cases (e.g. MongoDB), you may want to set this property to be false to disable the page |
||
121 | * number validation. By doing so, [[page]] will return the value indexed by [[pageParam]] in [[params]]. |
||
122 | */ |
||
123 | public $validatePage = true; |
||
124 | /** |
||
125 | * @var integer total number of items. |
||
126 | */ |
||
127 | public $totalCount = 0; |
||
128 | /** |
||
129 | * @var integer the default page size. This property will be returned by [[pageSize]] when page size |
||
130 | * cannot be determined by [[pageSizeParam]] from [[params]]. |
||
131 | */ |
||
132 | public $defaultPageSize = 20; |
||
133 | /** |
||
134 | * @var array|boolean the page size limits. The first array element stands for the minimal page size, and the second |
||
135 | * the maximal page size. If this is false, it means [[pageSize]] should always return the value of [[defaultPageSize]]. |
||
136 | */ |
||
137 | public $pageSizeLimit = [1, 50]; |
||
138 | |||
139 | /** |
||
140 | * @var integer number of items on each page. |
||
141 | * If it is less than 1, it means the page size is infinite, and thus a single page contains all items. |
||
142 | */ |
||
143 | private $_pageSize; |
||
144 | |||
145 | |||
146 | /** |
||
147 | * @return integer number of pages |
||
148 | */ |
||
149 | 28 | public function getPageCount() |
|
160 | |||
161 | private $_page; |
||
162 | |||
163 | /** |
||
164 | * Returns the zero-based current page number. |
||
165 | * @param boolean $recalculate whether to recalculate the current page based on the page size and item count. |
||
166 | * @return integer the zero-based current page number. |
||
167 | */ |
||
168 | 28 | public function getPage($recalculate = false) |
|
177 | |||
178 | /** |
||
179 | * Sets the current page number. |
||
180 | * @param integer $value the zero-based index of the current page. |
||
181 | * @param boolean $validatePage whether to validate the page number. Note that in order |
||
182 | * to validate the page number, both [[validatePage]] and this parameter must be true. |
||
183 | */ |
||
184 | 28 | public function setPage($value, $validatePage = false) |
|
202 | |||
203 | /** |
||
204 | * Returns the number of items per page. |
||
205 | * By default, this method will try to determine the page size by [[pageSizeParam]] in [[params]]. |
||
206 | * If the page size cannot be determined this way, [[defaultPageSize]] will be returned. |
||
207 | * @return integer the number of items per page. If it is less than 1, it means the page size is infinite, |
||
208 | * and thus a single page contains all items. |
||
209 | * @see pageSizeLimit |
||
210 | */ |
||
211 | 30 | public function getPageSize() |
|
225 | |||
226 | /** |
||
227 | * @param integer $value the number of items per page. |
||
228 | * @param boolean $validatePageSize whether to validate page size. |
||
229 | */ |
||
230 | 30 | public function setPageSize($value, $validatePageSize = false) |
|
246 | |||
247 | /** |
||
248 | * Creates the URL suitable for pagination with the specified page number. |
||
249 | * This method is mainly called by pagers when creating URLs used to perform pagination. |
||
250 | * @param integer $page the zero-based page number that the URL should point to. |
||
251 | * @param integer $pageSize the number of items on each page. If not set, the value of [[pageSize]] will be used. |
||
252 | * @param boolean $absolute whether to create an absolute URL. Defaults to `false`. |
||
253 | * @return string the created URL |
||
254 | * @see params |
||
255 | * @see forcePageParam |
||
256 | */ |
||
257 | 4 | public function createUrl($page, $pageSize = null, $absolute = false) |
|
286 | |||
287 | /** |
||
288 | * @return integer the offset of the data. This may be used to set the |
||
289 | * OFFSET value for a SQL statement for fetching the current page of data. |
||
290 | */ |
||
291 | 26 | public function getOffset() |
|
297 | |||
298 | /** |
||
299 | * @return integer the limit of the data. This may be used to set the |
||
300 | * LIMIT value for a SQL statement for fetching the current page of data. |
||
301 | * Note that if the page size is infinite, a value -1 will be returned. |
||
302 | */ |
||
303 | 26 | public function getLimit() |
|
309 | |||
310 | /** |
||
311 | * Returns a whole set of links for navigating to the first, last, next and previous pages. |
||
312 | * @param boolean $absolute whether the generated URLs should be absolute. |
||
313 | * @return array the links for navigational purpose. The array keys specify the purpose of the links (e.g. [[LINK_FIRST]]), |
||
314 | * and the array values are the corresponding URLs. |
||
315 | */ |
||
316 | public function getLinks($absolute = false) |
||
334 | |||
335 | /** |
||
336 | * Returns the value of the specified query parameter. |
||
337 | * This method returns the named parameter value from [[params]]. Null is returned if the value does not exist. |
||
338 | * @param string $name the parameter name |
||
339 | * @param string $defaultValue the value to be returned when the specified parameter does not exist in [[params]]. |
||
340 | * @return string the parameter value |
||
341 | */ |
||
342 | 29 | protected function getQueryParam($name, $defaultValue = null) |
|
351 | } |
||
352 |