Complex classes like Pagerfanta 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 Pagerfanta, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Pagerfanta implements \Countable, \IteratorAggregate, PagerfantaInterface |
||
|
|||
32 | { |
||
33 | private $adapter; |
||
34 | private $allowOutOfRangePages; |
||
35 | private $normalizeOutOfRangePages; |
||
36 | private $maxPerPage; |
||
37 | private $currentPage; |
||
38 | private $nbResults; |
||
39 | private $currentPageResults; |
||
40 | |||
41 | /** |
||
42 | * @param AdapterInterface $adapter An adapter. |
||
43 | */ |
||
44 | 138 | public function __construct(AdapterInterface $adapter) |
|
52 | |||
53 | /** |
||
54 | * Returns the adapter. |
||
55 | * |
||
56 | * @return AdapterInterface The adapter. |
||
57 | */ |
||
58 | 86 | public function getAdapter() |
|
62 | |||
63 | /** |
||
64 | * Sets whether or not allow out of range pages. |
||
65 | * |
||
66 | * @param Boolean $value |
||
67 | * |
||
68 | * @return self |
||
69 | */ |
||
70 | 10 | public function setAllowOutOfRangePages($value) |
|
76 | |||
77 | /** |
||
78 | * Returns whether or not allow out of range pages. |
||
79 | * |
||
80 | * @return Boolean |
||
81 | */ |
||
82 | 85 | public function getAllowOutOfRangePages() |
|
86 | |||
87 | /** |
||
88 | * Sets whether or not normalize out of range pages. |
||
89 | * |
||
90 | * @param Boolean $value |
||
91 | * |
||
92 | * @return self |
||
93 | */ |
||
94 | 8 | public function setNormalizeOutOfRangePages($value) |
|
100 | |||
101 | /** |
||
102 | * Returns whether or not normalize out of range pages. |
||
103 | * |
||
104 | * @return Boolean |
||
105 | */ |
||
106 | 7 | public function getNormalizeOutOfRangePages() |
|
110 | |||
111 | 16 | private function filterBoolean($value) |
|
119 | |||
120 | /** |
||
121 | * Sets the max per page. |
||
122 | * |
||
123 | * Tries to convert from string and float. |
||
124 | * |
||
125 | * @param integer $maxPerPage |
||
126 | * |
||
127 | * @return self |
||
128 | * |
||
129 | * @throws NotIntegerMaxPerPageException If the max per page is not an integer even converting. |
||
130 | * @throws LessThan1MaxPerPageException If the max per page is less than 1. |
||
131 | */ |
||
132 | 51 | public function setMaxPerPage($maxPerPage) |
|
139 | |||
140 | 51 | private function filterMaxPerPage($maxPerPage) |
|
147 | |||
148 | 51 | private function checkMaxPerPage($maxPerPage) |
|
158 | |||
159 | 45 | private function resetForMaxPerPageChange() |
|
164 | |||
165 | /** |
||
166 | * Returns the max per page. |
||
167 | * |
||
168 | * @return integer |
||
169 | */ |
||
170 | 89 | public function getMaxPerPage() |
|
174 | |||
175 | /** |
||
176 | * Sets the current page. |
||
177 | * |
||
178 | * Tries to convert from string and float. |
||
179 | * |
||
180 | * @param integer $currentPage |
||
181 | * |
||
182 | * @return self |
||
183 | * |
||
184 | * @throws NotIntegerCurrentPageException If the current page is not an integer even converting. |
||
185 | * @throws LessThan1CurrentPageException If the current page is less than 1. |
||
186 | * @throws OutOfRangeCurrentPageException If It is not allowed out of range pages and they are not normalized. |
||
187 | */ |
||
188 | 89 | public function setCurrentPage($currentPage) |
|
197 | |||
198 | 89 | private function useDeprecatedCurrentPageBooleanArguments($arguments) |
|
203 | |||
204 | 89 | private function useDeprecatedCurrentPageAllowOutOfRangePagesBooleanArgument($arguments) |
|
211 | |||
212 | 89 | private function useDeprecatedCurrentPageNormalizeOutOfRangePagesBooleanArgument($arguments) |
|
219 | |||
220 | 89 | private function useDeprecatedBooleanArgument($arguments, $index, $method) |
|
226 | |||
227 | 89 | private function filterCurrentPage($currentPage) |
|
237 | |||
238 | 89 | private function checkCurrentPage($currentPage) |
|
248 | |||
249 | 82 | private function filterOutOfRangeCurrentPage($currentPage) |
|
257 | |||
258 | 82 | private function notAllowedCurrentPageOutOfRange($currentPage) |
|
263 | |||
264 | 80 | private function currentPageOutOfRange($currentPage) |
|
268 | |||
269 | /** |
||
270 | * @param int $currentPage |
||
271 | * |
||
272 | * @return int |
||
273 | * |
||
274 | * @throws OutOfRangeCurrentPageException If the page should not be normalized |
||
275 | */ |
||
276 | 4 | private function normalizeOutOfRangeCurrentPage($currentPage) |
|
284 | |||
285 | 82 | private function resetForCurrentPageChange() |
|
289 | |||
290 | /** |
||
291 | * Returns the current page. |
||
292 | * |
||
293 | * @return integer |
||
294 | */ |
||
295 | 74 | public function getCurrentPage() |
|
299 | |||
300 | /** |
||
301 | * Returns the results for the current page. |
||
302 | * |
||
303 | * @return array|\Traversable |
||
304 | */ |
||
305 | 10 | public function getCurrentPageResults() |
|
313 | |||
314 | 10 | private function notCachedCurrentPageResults() |
|
318 | |||
319 | 10 | private function getCurrentPageResultsFromAdapter() |
|
337 | |||
338 | 11 | private function calculateOffsetForCurrentPageResults() |
|
342 | |||
343 | /** |
||
344 | * Calculates the current page offset start |
||
345 | * |
||
346 | * @return int |
||
347 | */ |
||
348 | 2 | public function getCurrentPageOffsetStart() |
|
354 | |||
355 | /** |
||
356 | * Calculates the current page offset end |
||
357 | * |
||
358 | * @return int |
||
359 | */ |
||
360 | 2 | public function getCurrentPageOffsetEnd() |
|
366 | |||
367 | /** |
||
368 | * Returns the number of results. |
||
369 | * |
||
370 | * @return integer |
||
371 | */ |
||
372 | 85 | public function getNbResults() |
|
380 | |||
381 | 85 | private function notCachedNbResults() |
|
385 | |||
386 | /** |
||
387 | * Returns the number of pages. |
||
388 | * |
||
389 | * @return integer |
||
390 | */ |
||
391 | 75 | public function getNbPages() |
|
401 | |||
402 | 75 | private function calculateNbPages() |
|
406 | |||
407 | 1 | private function minimumNbPages() |
|
411 | |||
412 | /** |
||
413 | * Returns if the number of results is higher than the max per page. |
||
414 | * |
||
415 | * @return Boolean |
||
416 | */ |
||
417 | 3 | public function haveToPaginate() |
|
421 | |||
422 | /** |
||
423 | * Returns whether there is previous page or not. |
||
424 | * |
||
425 | * @return Boolean |
||
426 | */ |
||
427 | 56 | public function hasPreviousPage() |
|
431 | |||
432 | /** |
||
433 | * Returns the previous page. |
||
434 | * |
||
435 | * @return integer |
||
436 | * |
||
437 | * @throws LogicException If there is no previous page. |
||
438 | */ |
||
439 | 43 | public function getPreviousPage() |
|
447 | |||
448 | /** |
||
449 | * Returns whether there is next page or not. |
||
450 | * |
||
451 | * @return Boolean |
||
452 | */ |
||
453 | 58 | public function hasNextPage() |
|
457 | |||
458 | /** |
||
459 | * Returns the next page. |
||
460 | * |
||
461 | * @return integer |
||
462 | * |
||
463 | * @throws LogicException If there is no next page. |
||
464 | */ |
||
465 | 49 | public function getNextPage() |
|
473 | |||
474 | /** |
||
475 | * Implements the \Countable interface. |
||
476 | * |
||
477 | * Return integer The number of results. |
||
478 | */ |
||
479 | 1 | public function count() |
|
483 | |||
484 | /** |
||
485 | * Implements the \IteratorAggregate interface. |
||
486 | * |
||
487 | * Returns an \ArrayIterator instance with the current results. |
||
488 | */ |
||
489 | 3 | public function getIterator() |
|
503 | |||
504 | 111 | private function toInteger($value) |
|
512 | |||
513 | 111 | private function needsToIntegerConversion($value) |
|
517 | |||
518 | /** |
||
519 | * Get page number of the item at specified position (1-based index) |
||
520 | * |
||
521 | * @param integer $position |
||
522 | * |
||
523 | * @return integer |
||
524 | */ |
||
525 | 3 | public function getPageNumberForItemAtPosition($position) |
|
541 | } |
||
542 |
This class, trait or interface has been deprecated.