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 | |||
| 40 | /** |
||
| 41 | * @param AdapterInterface $adapter An adapter. |
||
| 42 | */ |
||
| 43 | 138 | public function __construct(AdapterInterface $adapter) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Returns the adapter. |
||
| 54 | * |
||
| 55 | * @return AdapterInterface The adapter. |
||
| 56 | */ |
||
| 57 | 85 | public function getAdapter() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Sets whether or not allow out of range pages. |
||
| 64 | * |
||
| 65 | * @param boolean $value |
||
| 66 | * |
||
| 67 | * @return self |
||
| 68 | */ |
||
| 69 | 10 | public function setAllowOutOfRangePages($value) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Returns whether or not allow out of range pages. |
||
| 78 | * |
||
| 79 | * @return Boolean |
||
| 80 | */ |
||
| 81 | 84 | public function getAllowOutOfRangePages() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Sets whether or not normalize out of range pages. |
||
| 88 | * |
||
| 89 | * @param boolean $value |
||
| 90 | * |
||
| 91 | * @return self |
||
| 92 | */ |
||
| 93 | 8 | public function setNormalizeOutOfRangePages($value) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Returns whether or not normalize out of range pages. |
||
| 102 | * |
||
| 103 | * @return Boolean |
||
| 104 | */ |
||
| 105 | 6 | public function getNormalizeOutOfRangePages() |
|
| 109 | |||
| 110 | 16 | private function filterBoolean($value) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Sets the max per page. |
||
| 121 | * |
||
| 122 | * Tries to convert from string and float. |
||
| 123 | * |
||
| 124 | * @param integer $maxPerPage |
||
| 125 | * |
||
| 126 | * @return self |
||
| 127 | * |
||
| 128 | * @throws NotIntegerMaxPerPageException If the max per page is not an integer even converting. |
||
| 129 | * @throws LessThan1MaxPerPageException If the max per page is less than 1. |
||
| 130 | */ |
||
| 131 | 49 | public function setMaxPerPage($maxPerPage) |
|
| 138 | |||
| 139 | 49 | private function filterMaxPerPage($maxPerPage) |
|
| 146 | |||
| 147 | 49 | private function checkMaxPerPage($maxPerPage) |
|
| 157 | |||
| 158 | 43 | private function resetForMaxPerPageChange() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Returns the max per page. |
||
| 166 | * |
||
| 167 | * @return integer |
||
| 168 | */ |
||
| 169 | 91 | public function getMaxPerPage() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Sets the current page. |
||
| 176 | * |
||
| 177 | * Tries to convert from string and float. |
||
| 178 | * |
||
| 179 | * @param integer $currentPage |
||
| 180 | * |
||
| 181 | * @return self |
||
| 182 | * |
||
| 183 | * @throws NotIntegerCurrentPageException If the current page is not an integer even converting. |
||
| 184 | * @throws LessThan1CurrentPageException If the current page is less than 1. |
||
| 185 | * @throws OutOfRangeCurrentPageException If It is not allowed out of range pages and they are not normalized. |
||
| 186 | */ |
||
| 187 | 87 | public function setCurrentPage($currentPage) |
|
| 196 | |||
| 197 | 87 | private function useDeprecatedCurrentPageBooleanArguments($arguments) |
|
| 202 | |||
| 203 | 87 | private function useDeprecatedCurrentPageAllowOutOfRangePagesBooleanArgument($arguments) |
|
| 210 | |||
| 211 | 87 | private function useDeprecatedCurrentPageNormalizeOutOfRangePagesBooleanArgument($arguments) |
|
| 218 | |||
| 219 | 87 | private function useDeprecatedBooleanArgument($arguments, $index, $method) |
|
| 225 | |||
| 226 | 87 | private function filterCurrentPage($currentPage) |
|
| 234 | |||
| 235 | 87 | private function checkCurrentPage($currentPage) |
|
| 245 | |||
| 246 | 81 | private function filterOutOfRangeCurrentPage($currentPage) |
|
| 254 | |||
| 255 | 81 | private function notAllowedCurrentPageOutOfRange($currentPage) |
|
| 260 | |||
| 261 | 79 | private function currentPageOutOfRange($currentPage) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @param int $currentPage |
||
| 268 | * |
||
| 269 | * @return int |
||
| 270 | * |
||
| 271 | * @throws OutOfRangeCurrentPageException If the page should not be normalized |
||
| 272 | */ |
||
| 273 | 3 | private function normalizeOutOfRangeCurrentPage($currentPage) |
|
| 281 | |||
| 282 | 80 | private function resetForCurrentPageChange() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Returns the current page. |
||
| 289 | * |
||
| 290 | * @return integer |
||
| 291 | */ |
||
| 292 | 76 | public function getCurrentPage() |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Returns the results for the current page. |
||
| 299 | * |
||
| 300 | * @return array|\Traversable |
||
| 301 | */ |
||
| 302 | 12 | public function getCurrentPageResults() |
|
| 310 | |||
| 311 | 12 | private function notCachedCurrentPageResults() |
|
| 315 | |||
| 316 | 12 | private function getCurrentPageResultsFromAdapter() |
|
| 323 | |||
| 324 | 13 | private function calculateOffsetForCurrentPageResults() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Calculates the current page offset start |
||
| 331 | * |
||
| 332 | * @return int |
||
| 333 | */ |
||
| 334 | 2 | public function getCurrentPageOffsetStart() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Calculates the current page offset end |
||
| 343 | * |
||
| 344 | * @return int |
||
| 345 | */ |
||
| 346 | 2 | public function getCurrentPageOffsetEnd() |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Returns the number of results. |
||
| 355 | * |
||
| 356 | * @return integer |
||
| 357 | */ |
||
| 358 | 84 | public function getNbResults() |
|
| 366 | |||
| 367 | 84 | private function notCachedNbResults() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Returns the number of pages. |
||
| 374 | * |
||
| 375 | * @return integer |
||
| 376 | */ |
||
| 377 | 74 | public function getNbPages() |
|
| 387 | |||
| 388 | 74 | private function calculateNbPages() |
|
| 392 | |||
| 393 | 1 | private function minimumNbPages() |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Returns if the number of results is higher than the max per page. |
||
| 400 | * |
||
| 401 | * @return Boolean |
||
| 402 | */ |
||
| 403 | 3 | public function haveToPaginate() |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Returns whether there is previous page or not. |
||
| 410 | * |
||
| 411 | * @return Boolean |
||
| 412 | */ |
||
| 413 | 56 | public function hasPreviousPage() |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Returns the previous page. |
||
| 420 | * |
||
| 421 | * @return integer |
||
| 422 | * |
||
| 423 | * @throws LogicException If there is no previous page. |
||
| 424 | */ |
||
| 425 | 43 | public function getPreviousPage() |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Returns whether there is next page or not. |
||
| 436 | * |
||
| 437 | * @return Boolean |
||
| 438 | */ |
||
| 439 | 58 | public function hasNextPage() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Returns the next page. |
||
| 446 | * |
||
| 447 | * @return integer |
||
| 448 | * |
||
| 449 | * @throws LogicException If there is no next page. |
||
| 450 | */ |
||
| 451 | 49 | public function getNextPage() |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Implements the \Countable interface. |
||
| 462 | * |
||
| 463 | * @return integer The number of results. |
||
| 464 | */ |
||
| 465 | 1 | public function count() |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Implements the \IteratorAggregate interface. |
||
| 472 | * |
||
| 473 | * @return \ArrayIterator instance with the current results. |
||
| 474 | */ |
||
| 475 | 3 | public function getIterator() |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Implements the \JsonSerializable interface. |
||
| 492 | * |
||
| 493 | * @return array current page results |
||
| 494 | */ |
||
| 495 | 3 | public function jsonSerialize() |
|
| 496 | { |
||
| 497 | 3 | $results = $this->getCurrentPageResults(); |
|
| 498 | 3 | if ($results instanceof \Traversable) { |
|
| 499 | 2 | return iterator_to_array($results); |
|
| 500 | } |
||
| 501 | |||
| 502 | 1 | return $results; |
|
| 503 | } |
||
| 504 | |||
| 505 | 109 | private function toInteger($value) |
|
| 513 | |||
| 514 | 109 | private function needsToIntegerConversion($value) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Get page number of the item at specified position (1-based index) |
||
| 521 | * |
||
| 522 | * @param integer $position |
||
| 523 | * |
||
| 524 | * @return integer |
||
| 525 | */ |
||
| 526 | 3 | public function getPageNumberForItemAtPosition($position) |
|
| 542 | } |
||
| 543 |
This class, trait or interface has been deprecated.