Complex classes like Pager 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 Pager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class Pager implements \Iterator, \Countable, \Serializable, PagerInterface |
||
| 23 | { |
||
| 24 | public const TYPE_DEFAULT = 'default'; |
||
| 25 | public const TYPE_SIMPLE = 'simple'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | protected $page = 1; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | protected $maxPerPage = 0; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | protected $lastPage = 1; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | protected $nbResults = 0; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | protected $cursor = 1; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array<string, mixed> |
||
| 54 | */ |
||
| 55 | protected $parameters = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | protected $currentMaxLink = 1; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var int|false |
||
| 64 | */ |
||
| 65 | protected $maxRecordLimit = false; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var int |
||
| 69 | */ |
||
| 70 | protected $maxPageLinks = 0; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Results are null prior to its initialization in `initializeIterator()`. |
||
| 74 | * |
||
| 75 | * @var object[]|null |
||
| 76 | */ |
||
| 77 | protected $results; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var int |
||
| 81 | */ |
||
| 82 | protected $resultsCounter = 0; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var ProxyQueryInterface|null |
||
| 86 | */ |
||
| 87 | protected $query; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var string[] |
||
| 91 | */ |
||
| 92 | protected $countColumn = ['id']; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param int $maxPerPage Number of records to display per page |
||
| 96 | */ |
||
| 97 | public function __construct(int $maxPerPage = 10) |
||
| 98 | { |
||
| 99 | $this->setMaxPerPage($maxPerPage); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Returns the current pager's max link. |
||
| 104 | */ |
||
| 105 | public function getCurrentMaxLink(): int |
||
| 106 | { |
||
| 107 | return $this->currentMaxLink; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns the current pager's max record limit. |
||
| 112 | * |
||
| 113 | * @return int|false |
||
| 114 | */ |
||
| 115 | public function getMaxRecordLimit() |
||
| 116 | { |
||
| 117 | return $this->maxRecordLimit; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Sets the current pager's max record limit. |
||
| 122 | */ |
||
| 123 | public function setMaxRecordLimit(int $limit): void |
||
| 124 | { |
||
| 125 | $this->maxRecordLimit = $limit; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Returns an array of page numbers to use in pagination links. |
||
| 130 | * |
||
| 131 | * @return int[] |
||
| 132 | */ |
||
| 133 | public function getLinks(?int $nbLinks = null): array |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns true if the current query requires pagination. |
||
| 156 | */ |
||
| 157 | public function haveToPaginate(): bool |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns the current cursor. |
||
| 164 | */ |
||
| 165 | public function getCursor(): int |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Sets the current cursor. |
||
| 172 | */ |
||
| 173 | public function setCursor(int $pos): void |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Returns an object by cursor position. |
||
| 188 | */ |
||
| 189 | public function getObjectByCursor(int $pos): ?object |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns the current object. |
||
| 198 | */ |
||
| 199 | public function getCurrent(): ?object |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns the next object. |
||
| 206 | */ |
||
| 207 | public function getNext(): ?object |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Returns the previous object. |
||
| 218 | */ |
||
| 219 | public function getPrevious(): ?object |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Returns the first index on the current page. |
||
| 230 | */ |
||
| 231 | public function getFirstIndex(): int |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Returns the last index on the current page. |
||
| 242 | */ |
||
| 243 | public function getLastIndex(): int |
||
| 254 | |||
| 255 | public function getNbResults(): int |
||
| 259 | |||
| 260 | public function getFirstPage(): int |
||
| 264 | |||
| 265 | public function getLastPage(): int |
||
| 269 | |||
| 270 | public function getPage(): int |
||
| 274 | |||
| 275 | public function getNextPage(): int |
||
| 279 | |||
| 280 | public function getPreviousPage(): int |
||
| 284 | |||
| 285 | public function setPage($page): void |
||
| 294 | |||
| 295 | public function getMaxPerPage(): int |
||
| 299 | |||
| 300 | public function setMaxPerPage($max): void |
||
| 321 | |||
| 322 | public function getMaxPageLinks(): int |
||
| 326 | |||
| 327 | public function setMaxPageLinks(int $maxPageLinks): void |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Returns true if on the first page. |
||
| 334 | */ |
||
| 335 | public function isFirstPage(): bool |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Returns true if on the last page. |
||
| 342 | */ |
||
| 343 | public function isLastPage(): bool |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Returns the current pager's parameter holder. |
||
| 350 | * |
||
| 351 | * @return array<string, mixed> |
||
|
|
|||
| 352 | */ |
||
| 353 | public function getParameters(): array |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Returns a parameter. |
||
| 360 | * |
||
| 361 | * @param mixed $default |
||
| 362 | * |
||
| 363 | * @return mixed |
||
| 364 | */ |
||
| 365 | public function getParameter(string $name, $default = null) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Checks whether a parameter has been set. |
||
| 372 | */ |
||
| 373 | public function hasParameter(string $name): bool |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Sets a parameter. |
||
| 380 | * |
||
| 381 | * @param mixed $value |
||
| 382 | */ |
||
| 383 | public function setParameter(string $name, $value): void |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @return object|false |
||
| 390 | */ |
||
| 391 | public function current() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return int|string |
||
| 402 | */ |
||
| 403 | public function key() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return object|false |
||
| 414 | */ |
||
| 415 | public function next() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @return object|false |
||
| 428 | */ |
||
| 429 | public function rewind() |
||
| 439 | |||
| 440 | public function valid(): bool |
||
| 448 | |||
| 449 | public function count(): int |
||
| 453 | |||
| 454 | public function serialize(): string |
||
| 461 | |||
| 462 | public function unserialize($serialized): void |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @return string[] |
||
| 473 | */ |
||
| 474 | public function getCountColumn(): array |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @param string[] $countColumn |
||
| 481 | */ |
||
| 482 | public function setCountColumn(array $countColumn): void |
||
| 486 | |||
| 487 | public function setQuery(ProxyQueryInterface $query): void |
||
| 491 | |||
| 492 | public function getQuery(): ?ProxyQueryInterface |
||
| 496 | |||
| 497 | protected function setNbResults(int $nbResults): void |
||
| 501 | |||
| 502 | protected function setLastPage(int $page): void |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Returns true if the properties used for iteration have been initialized. |
||
| 513 | */ |
||
| 514 | protected function isIteratorInitialized(): bool |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Loads data into properties used for iteration. |
||
| 521 | */ |
||
| 522 | protected function initializeIterator(): void |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Empties properties used for iteration. |
||
| 530 | */ |
||
| 531 | protected function resetIterator(): void |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Retrieve the object for a certain offset. |
||
| 539 | */ |
||
| 540 | protected function retrieveObject(int $offset): ?object |
||
| 557 | } |
||
| 558 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.