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 | 125 | public function __construct(AdapterInterface $adapter) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Returns the adapter. |
||
| 53 | * |
||
| 54 | * @return AdapterInterface The adapter. |
||
| 55 | */ |
||
| 56 | 74 | 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 | 75 | 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 | 7 | 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 | 50 | public function setMaxPerPage($maxPerPage) |
|
| 137 | |||
| 138 | 50 | private function filterMaxPerPage($maxPerPage) |
|
| 145 | |||
| 146 | 50 | private function checkMaxPerPage($maxPerPage) |
|
| 156 | |||
| 157 | 44 | private function resetForMaxPerPageChange() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Returns the max per page. |
||
| 165 | * |
||
| 166 | * @return integer |
||
| 167 | */ |
||
| 168 | 78 | 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 | 79 | public function setCurrentPage($currentPage) |
|
| 195 | |||
| 196 | 79 | private function useDeprecatedCurrentPageBooleanArguments($arguments) |
|
| 201 | |||
| 202 | 79 | private function useDeprecatedCurrentPageAllowOutOfRangePagesBooleanArgument($arguments) |
|
| 209 | |||
| 210 | 79 | private function useDeprecatedCurrentPageNormalizeOutOfRangePagesBooleanArgument($arguments) |
|
| 217 | |||
| 218 | 79 | private function useDeprecatedBooleanArgument($arguments, $index, $method) |
|
| 224 | |||
| 225 | 79 | private function filterCurrentPage($currentPage) |
|
| 235 | |||
| 236 | 79 | private function checkCurrentPage($currentPage) |
|
| 246 | |||
| 247 | 72 | private function filterOutOfRangeCurrentPage($currentPage) |
|
| 255 | |||
| 256 | 72 | private function notAllowedCurrentPageOutOfRange($currentPage) |
|
| 261 | |||
| 262 | 70 | 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 | 4 | private function normalizeOutOfRangeCurrentPage($currentPage) |
|
| 282 | |||
| 283 | 72 | private function resetForCurrentPageChange() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Returns the current page. |
||
| 290 | * |
||
| 291 | * @return integer |
||
| 292 | */ |
||
| 293 | 64 | public function getCurrentPage() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Returns the results for the current page. |
||
| 300 | * |
||
| 301 | * @return array|\Traversable |
||
| 302 | */ |
||
| 303 | 10 | public function getCurrentPageResults() |
|
| 311 | |||
| 312 | 10 | private function notCachedCurrentPageResults() |
|
| 316 | |||
| 317 | 10 | private function getCurrentPageResultsFromAdapter() |
|
| 335 | |||
| 336 | 11 | private function calculateOffsetForCurrentPageResults() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Calculates the current page offset start |
||
| 343 | * |
||
| 344 | * @return int |
||
| 345 | */ |
||
| 346 | 2 | public function getCurrentPageOffsetStart() |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Calculates the current page offset end |
||
| 355 | * |
||
| 356 | * @return int |
||
| 357 | */ |
||
| 358 | 2 | public function getCurrentPageOffsetEnd() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Returns the number of results. |
||
| 367 | * |
||
| 368 | * @return integer |
||
| 369 | */ |
||
| 370 | 73 | public function getNbResults() |
|
| 378 | |||
| 379 | 73 | private function notCachedNbResults() |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Returns the number of pages. |
||
| 386 | * |
||
| 387 | * @return integer |
||
| 388 | */ |
||
| 389 | 65 | public function getNbPages() |
|
| 399 | |||
| 400 | 65 | private function calculateNbPages() |
|
| 404 | |||
| 405 | 1 | private function minimumNbPages() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Returns if the number of results is higher than the max per page. |
||
| 412 | * |
||
| 413 | * @return Boolean |
||
| 414 | */ |
||
| 415 | 3 | public function haveToPaginate() |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Returns whether there is previous page or not. |
||
| 422 | * |
||
| 423 | * @return Boolean |
||
| 424 | */ |
||
| 425 | 46 | public function hasPreviousPage() |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Returns the previous page. |
||
| 432 | * |
||
| 433 | * @return integer |
||
| 434 | * |
||
| 435 | * @throws LogicException If there is no previous page. |
||
| 436 | */ |
||
| 437 | 35 | public function getPreviousPage() |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Returns whether there is next page or not. |
||
| 448 | * |
||
| 449 | * @return Boolean |
||
| 450 | */ |
||
| 451 | 48 | public function hasNextPage() |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Returns the next page. |
||
| 458 | * |
||
| 459 | * @return integer |
||
| 460 | * |
||
| 461 | * @throws LogicException If there is no next page. |
||
| 462 | */ |
||
| 463 | 40 | public function getNextPage() |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Implements the \Countable interface. |
||
| 474 | * |
||
| 475 | * Return integer The number of results. |
||
| 476 | */ |
||
| 477 | 1 | public function count() |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Implements the \IteratorAggregate interface. |
||
| 484 | * |
||
| 485 | * Returns an \ArrayIterator instance with the current results. |
||
| 486 | */ |
||
| 487 | 3 | public function getIterator() |
|
| 501 | |||
| 502 | 100 | private function toInteger($value) |
|
| 510 | |||
| 511 | 100 | private function needsToIntegerConversion($value) |
|
| 515 | } |
||
| 516 |
This class, trait or interface has been deprecated.