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 |
||
| 29 | class Pagerfanta implements \Countable, \IteratorAggregate, PagerfantaInterface |
||
|
|
|||
| 30 | { |
||
| 31 | private $adapter; |
||
| 32 | private $allowOutOfRangePages; |
||
| 33 | private $normalizeOutOfRangePages; |
||
| 34 | private $maxPerPage; |
||
| 35 | private $currentPage; |
||
| 36 | private $nbResults; |
||
| 37 | private $currentPageResults; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param AdapterInterface $adapter An adapter. |
||
| 41 | */ |
||
| 42 | 136 | public function __construct(AdapterInterface $adapter) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Returns the adapter. |
||
| 53 | * |
||
| 54 | * @return AdapterInterface The adapter. |
||
| 55 | */ |
||
| 56 | 85 | public function getAdapter() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Sets whether or not allow out of range pages. |
||
| 63 | * |
||
| 64 | * @param Boolean $value |
||
| 65 | * |
||
| 66 | * @return self |
||
| 67 | */ |
||
| 68 | 10 | public function setAllowOutOfRangePages($value) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Returns whether or not allow out of range pages. |
||
| 77 | * |
||
| 78 | * @return Boolean |
||
| 79 | */ |
||
| 80 | 84 | public function getAllowOutOfRangePages() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Sets whether or not normalize out of range pages. |
||
| 87 | * |
||
| 88 | * @param Boolean $value |
||
| 89 | * |
||
| 90 | * @return self |
||
| 91 | */ |
||
| 92 | 8 | public function setNormalizeOutOfRangePages($value) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Returns whether or not normalize out of range pages. |
||
| 101 | * |
||
| 102 | * @return Boolean |
||
| 103 | */ |
||
| 104 | 6 | public function getNormalizeOutOfRangePages() |
|
| 108 | |||
| 109 | 16 | private function filterBoolean($value) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Sets the max per page. |
||
| 120 | * |
||
| 121 | * Tries to convert from string and float. |
||
| 122 | * |
||
| 123 | * @param integer $maxPerPage |
||
| 124 | * |
||
| 125 | * @return self |
||
| 126 | * |
||
| 127 | * @throws NotIntegerMaxPerPageException If the max per page is not an integer even converting. |
||
| 128 | * @throws LessThan1MaxPerPageException If the max per page is less than 1. |
||
| 129 | */ |
||
| 130 | 49 | public function setMaxPerPage($maxPerPage) |
|
| 137 | |||
| 138 | 49 | private function filterMaxPerPage($maxPerPage) |
|
| 145 | |||
| 146 | 49 | private function checkMaxPerPage($maxPerPage) |
|
| 156 | |||
| 157 | 43 | private function resetForMaxPerPageChange() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Returns the max per page. |
||
| 165 | * |
||
| 166 | * @return integer |
||
| 167 | */ |
||
| 168 | 88 | public function getMaxPerPage() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Sets the current page. |
||
| 175 | * |
||
| 176 | * Tries to convert from string and float. |
||
| 177 | * |
||
| 178 | * @param integer $currentPage |
||
| 179 | * |
||
| 180 | * @return self |
||
| 181 | * |
||
| 182 | * @throws NotIntegerCurrentPageException If the current page is not an integer even converting. |
||
| 183 | * @throws LessThan1CurrentPageException If the current page is less than 1. |
||
| 184 | * @throws OutOfRangeCurrentPageException If It is not allowed out of range pages and they are not normalized. |
||
| 185 | */ |
||
| 186 | 87 | public function setCurrentPage($currentPage) |
|
| 195 | |||
| 196 | 87 | private function useDeprecatedCurrentPageBooleanArguments($arguments) |
|
| 201 | |||
| 202 | 87 | private function useDeprecatedCurrentPageAllowOutOfRangePagesBooleanArgument($arguments) |
|
| 209 | |||
| 210 | 87 | private function useDeprecatedCurrentPageNormalizeOutOfRangePagesBooleanArgument($arguments) |
|
| 217 | |||
| 218 | 87 | private function useDeprecatedBooleanArgument($arguments, $index, $method) |
|
| 224 | |||
| 225 | 87 | private function filterCurrentPage($currentPage) |
|
| 233 | |||
| 234 | 87 | private function checkCurrentPage($currentPage) |
|
| 244 | |||
| 245 | 81 | private function filterOutOfRangeCurrentPage($currentPage) |
|
| 253 | |||
| 254 | 81 | private function notAllowedCurrentPageOutOfRange($currentPage) |
|
| 259 | |||
| 260 | 79 | private function currentPageOutOfRange($currentPage) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @param int $currentPage |
||
| 267 | * |
||
| 268 | * @return int |
||
| 269 | * |
||
| 270 | * @throws OutOfRangeCurrentPageException If the page should not be normalized |
||
| 271 | */ |
||
| 272 | 3 | private function normalizeOutOfRangeCurrentPage($currentPage) |
|
| 280 | |||
| 281 | 80 | private function resetForCurrentPageChange() |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Returns the current page. |
||
| 288 | * |
||
| 289 | * @return integer |
||
| 290 | */ |
||
| 291 | 73 | public function getCurrentPage() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Returns the results for the current page. |
||
| 298 | * |
||
| 299 | * @return array|\Traversable |
||
| 300 | */ |
||
| 301 | 9 | public function getCurrentPageResults() |
|
| 309 | |||
| 310 | 9 | private function notCachedCurrentPageResults() |
|
| 314 | |||
| 315 | 9 | private function getCurrentPageResultsFromAdapter() |
|
| 322 | |||
| 323 | 10 | private function calculateOffsetForCurrentPageResults() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Calculates the current page offset start |
||
| 330 | * |
||
| 331 | * @return int |
||
| 332 | */ |
||
| 333 | 2 | public function getCurrentPageOffsetStart() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Calculates the current page offset end |
||
| 342 | * |
||
| 343 | * @return int |
||
| 344 | */ |
||
| 345 | 2 | public function getCurrentPageOffsetEnd() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Returns the number of results. |
||
| 354 | * |
||
| 355 | * @return integer |
||
| 356 | */ |
||
| 357 | 84 | public function getNbResults() |
|
| 365 | |||
| 366 | 84 | private function notCachedNbResults() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Returns the number of pages. |
||
| 373 | * |
||
| 374 | * @return integer |
||
| 375 | */ |
||
| 376 | 74 | public function getNbPages() |
|
| 386 | |||
| 387 | 74 | private function calculateNbPages() |
|
| 391 | |||
| 392 | 1 | private function minimumNbPages() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Returns if the number of results is higher than the max per page. |
||
| 399 | * |
||
| 400 | * @return Boolean |
||
| 401 | */ |
||
| 402 | 3 | public function haveToPaginate() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Returns whether there is previous page or not. |
||
| 409 | * |
||
| 410 | * @return Boolean |
||
| 411 | */ |
||
| 412 | 56 | public function hasPreviousPage() |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Returns the previous page. |
||
| 419 | * |
||
| 420 | * @return integer |
||
| 421 | * |
||
| 422 | * @throws LogicException If there is no previous page. |
||
| 423 | */ |
||
| 424 | 43 | public function getPreviousPage() |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Returns whether there is next page or not. |
||
| 435 | * |
||
| 436 | * @return Boolean |
||
| 437 | */ |
||
| 438 | 58 | public function hasNextPage() |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Returns the next page. |
||
| 445 | * |
||
| 446 | * @return integer |
||
| 447 | * |
||
| 448 | * @throws LogicException If there is no next page. |
||
| 449 | */ |
||
| 450 | 49 | public function getNextPage() |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Implements the \Countable interface. |
||
| 461 | * |
||
| 462 | * Return integer The number of results. |
||
| 463 | */ |
||
| 464 | 1 | public function count() |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Implements the \IteratorAggregate interface. |
||
| 471 | * |
||
| 472 | * Returns an \ArrayIterator instance with the current results. |
||
| 473 | */ |
||
| 474 | 3 | public function getIterator() |
|
| 488 | |||
| 489 | 109 | private function toInteger($value) |
|
| 497 | |||
| 498 | 109 | private function needsToIntegerConversion($value) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Get page number of the item at specified position (1-based index) |
||
| 505 | * |
||
| 506 | * @param integer $position |
||
| 507 | * |
||
| 508 | * @return integer |
||
| 509 | */ |
||
| 510 | 3 | public function getPageNumberForItemAtPosition($position) |
|
| 522 | } |
||
| 523 |
This class, trait or interface has been deprecated.