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 |
||
| 30 | class Pagerfanta implements \Countable, \IteratorAggregate, \JsonSerializable, PagerfantaInterface |
||
|
|
|||
| 31 | { |
||
| 32 | private $adapter; |
||
| 33 | private $allowOutOfRangePages; |
||
| 34 | private $normalizeOutOfRangePages; |
||
| 35 | private $maxPerPage; |
||
| 36 | private $currentPage; |
||
| 37 | private $nbResults; |
||
| 38 | private $currentPageResults; |
||
| 39 | private $offset = 0; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param AdapterInterface $adapter An adapter. |
||
| 43 | */ |
||
| 44 | 144 | public function __construct(AdapterInterface $adapter) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Returns the adapter. |
||
| 55 | * |
||
| 56 | * @return AdapterInterface The adapter. |
||
| 57 | */ |
||
| 58 | 88 | 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 | 89 | 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 | 6 | 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 | 54 | public function setMaxPerPage($maxPerPage) |
|
| 139 | |||
| 140 | 54 | private function filterMaxPerPage($maxPerPage) |
|
| 147 | |||
| 148 | 54 | private function checkMaxPerPage($maxPerPage) |
|
| 158 | |||
| 159 | 48 | private function resetForMaxPerPageChange() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Returns the max per page. |
||
| 167 | * |
||
| 168 | * @return integer |
||
| 169 | */ |
||
| 170 | 97 | 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 | 92 | public function setCurrentPage($currentPage) |
|
| 197 | |||
| 198 | 92 | private function useDeprecatedCurrentPageBooleanArguments($arguments) |
|
| 203 | |||
| 204 | 92 | private function useDeprecatedCurrentPageAllowOutOfRangePagesBooleanArgument($arguments) |
|
| 211 | |||
| 212 | 92 | private function useDeprecatedCurrentPageNormalizeOutOfRangePagesBooleanArgument($arguments) |
|
| 219 | |||
| 220 | 92 | private function useDeprecatedBooleanArgument($arguments, $index, $method) |
|
| 226 | |||
| 227 | 92 | private function filterCurrentPage($currentPage) |
|
| 235 | |||
| 236 | 92 | private function checkCurrentPage($currentPage) |
|
| 246 | |||
| 247 | 86 | private function filterOutOfRangeCurrentPage($currentPage) |
|
| 255 | |||
| 256 | 86 | private function notAllowedCurrentPageOutOfRange($currentPage) |
|
| 261 | |||
| 262 | 84 | private function currentPageOutOfRange($currentPage) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @param int $currentPage |
||
| 269 | * |
||
| 270 | * @return int |
||
| 271 | * |
||
| 272 | * @throws OutOfRangeCurrentPageException If the page should not be normalized |
||
| 273 | */ |
||
| 274 | 3 | private function normalizeOutOfRangeCurrentPage($currentPage) |
|
| 282 | |||
| 283 | 85 | private function resetForCurrentPageChange() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Returns the current page. |
||
| 290 | * |
||
| 291 | * @return integer |
||
| 292 | */ |
||
| 293 | 82 | public function getCurrentPage() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Returns the results for the current page. |
||
| 300 | * |
||
| 301 | * @return array|\Traversable |
||
| 302 | */ |
||
| 303 | 18 | public function getCurrentPageResults() |
|
| 311 | |||
| 312 | 18 | private function notCachedCurrentPageResults() |
|
| 316 | |||
| 317 | 18 | private function getCurrentPageResultsFromAdapter() |
|
| 324 | |||
| 325 | 19 | private function calculateOffsetForCurrentPageResults() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Calculates the current page offset start |
||
| 332 | * |
||
| 333 | * @return int |
||
| 334 | */ |
||
| 335 | 2 | public function getCurrentPageOffsetStart() |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Calculates the current page offset end |
||
| 344 | * |
||
| 345 | * @return int |
||
| 346 | */ |
||
| 347 | 2 | public function getCurrentPageOffsetEnd() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Returns the number of results. |
||
| 356 | * |
||
| 357 | * @return integer |
||
| 358 | */ |
||
| 359 | 87 | public function getNbResults() |
|
| 367 | |||
| 368 | 87 | private function notCachedNbResults() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Returns the number of pages. |
||
| 375 | * |
||
| 376 | * @return integer |
||
| 377 | */ |
||
| 378 | 77 | public function getNbPages() |
|
| 388 | |||
| 389 | 77 | private function calculateNbPages() |
|
| 393 | |||
| 394 | 1 | private function minimumNbPages() |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Returns if the number of results is higher than the max per page. |
||
| 401 | * |
||
| 402 | * @return boolean |
||
| 403 | */ |
||
| 404 | 3 | public function haveToPaginate() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Returns whether there is previous page or not. |
||
| 411 | * |
||
| 412 | * @return boolean |
||
| 413 | */ |
||
| 414 | 56 | public function hasPreviousPage() |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Returns the previous page. |
||
| 421 | * |
||
| 422 | * @return integer |
||
| 423 | * |
||
| 424 | * @throws LogicException If there is no previous page. |
||
| 425 | */ |
||
| 426 | 43 | public function getPreviousPage() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Returns whether there is next page or not. |
||
| 437 | * |
||
| 438 | * @return boolean |
||
| 439 | */ |
||
| 440 | 58 | public function hasNextPage() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Returns the next page. |
||
| 447 | * |
||
| 448 | * @return integer |
||
| 449 | * |
||
| 450 | * @throws LogicException If there is no next page. |
||
| 451 | */ |
||
| 452 | 49 | public function getNextPage() |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Implements the \Countable interface. |
||
| 463 | * |
||
| 464 | * @return integer The number of results. |
||
| 465 | */ |
||
| 466 | 1 | public function count() |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Implements the \IteratorAggregate interface. |
||
| 473 | * |
||
| 474 | * @return \ArrayIterator instance with the current results. |
||
| 475 | */ |
||
| 476 | 3 | public function getIterator() |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Implements the \JsonSerializable interface. |
||
| 493 | * |
||
| 494 | * @return array current page results |
||
| 495 | */ |
||
| 496 | 4 | public function jsonSerialize() |
|
| 505 | |||
| 506 | 114 | private function toInteger($value) |
|
| 514 | |||
| 515 | 114 | private function needsToIntegerConversion($value) |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Get page number of the item at specified position (1-based index) |
||
| 522 | * |
||
| 523 | * @param integer $position |
||
| 524 | * |
||
| 525 | * @return integer |
||
| 526 | */ |
||
| 527 | 3 | public function getPageNumberForItemAtPosition($position) |
|
| 543 | |||
| 544 | /** |
||
| 545 | * @return integer |
||
| 546 | */ |
||
| 547 | 19 | public function getOffset() |
|
| 551 | |||
| 552 | /** |
||
| 553 | * @param integer $offset |
||
| 554 | * |
||
| 555 | * @return Pagerfanta |
||
| 556 | */ |
||
| 557 | 5 | public function setOffset($offset) |
|
| 563 | } |
||
| 564 |
This class, trait or interface has been deprecated.