Complex classes like Paginator 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 Paginator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class Paginator |
||
| 47 | { |
||
| 48 | /** |
||
| 49 | * @var integer |
||
| 50 | */ |
||
| 51 | protected $totalItems = 0; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var integer |
||
| 55 | */ |
||
| 56 | protected $numPages = 0; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var integer |
||
| 60 | */ |
||
| 61 | protected $itemsPerPage = 0; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var integer |
||
| 65 | */ |
||
| 66 | protected $currentPage = 0; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var integer |
||
| 70 | */ |
||
| 71 | protected $maxPagesToShow = 10; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $previousText = '«'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $nextText = '»'; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The pagination renderer |
||
| 85 | * |
||
| 86 | * @var Renderer |
||
| 87 | */ |
||
| 88 | protected $renderer = null; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The Jaxon request to be paginated |
||
| 92 | * |
||
| 93 | * @var Request |
||
| 94 | */ |
||
| 95 | protected $request = null; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * The constructor |
||
| 99 | * |
||
| 100 | * @param Renderer $renderer |
||
| 101 | */ |
||
| 102 | public function __construct(Renderer $renderer) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Update the number of pages |
||
| 109 | * |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | protected function updateNumPages() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Set the max number of pages to show |
||
| 119 | * |
||
| 120 | * @param int $maxPagesToShow The max number of pages to show |
||
| 121 | * |
||
| 122 | * @return void |
||
| 123 | * @throws \InvalidArgumentException if $maxPagesToShow is less than 3. |
||
| 124 | */ |
||
| 125 | public function setMaxPagesToShow($maxPagesToShow) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get the max number of pages to show |
||
| 136 | * |
||
| 137 | * @return int |
||
| 138 | */ |
||
| 139 | public function getMaxPagesToShow() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Set the current page number |
||
| 146 | * |
||
| 147 | * @param int $currentPage The current page number |
||
| 148 | * |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | public function setCurrentPage($currentPage) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get the current page number |
||
| 158 | * |
||
| 159 | * @return int |
||
| 160 | */ |
||
| 161 | public function getCurrentPage() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Set the number of items per page |
||
| 168 | * |
||
| 169 | * @param int $itemsPerPage The number of items per page |
||
| 170 | * |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | public function setItemsPerPage($itemsPerPage) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get the number of items per page |
||
| 181 | * |
||
| 182 | * @return int |
||
| 183 | */ |
||
| 184 | public function getItemsPerPage() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set the total number of items |
||
| 191 | * |
||
| 192 | * @param int $totalItems The total number of items |
||
| 193 | * |
||
| 194 | * @return void |
||
| 195 | */ |
||
| 196 | public function setTotalItems($totalItems) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get the total number of items |
||
| 204 | * |
||
| 205 | * @return int |
||
| 206 | */ |
||
| 207 | public function getTotalItems() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get the total number of pages |
||
| 214 | * |
||
| 215 | * @return int |
||
| 216 | */ |
||
| 217 | public function getNumPages() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Set the request to be paginated |
||
| 224 | * |
||
| 225 | * @param Request $request The request to be paginated |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | public function setRequest(Request $request) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get the request to be paginated |
||
| 241 | * |
||
| 242 | * @return Request |
||
| 243 | */ |
||
| 244 | public function getRequest() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Setup the paginator |
||
| 251 | * |
||
| 252 | * @param int $totalItems The total number of items |
||
| 253 | * @param int $itemsPerPage The number of items per page |
||
| 254 | * @param int $currentPage The current page number |
||
| 255 | * @param Request $request The request to be paginated |
||
| 256 | * |
||
| 257 | * @return Paginator |
||
| 258 | */ |
||
| 259 | public function setup($totalItems, $itemsPerPage, $currentPage, $request) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get the js call to a given page |
||
| 271 | * |
||
| 272 | * @param int $pageNum The page number |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function getPageCall($pageNum) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get the next page number |
||
| 283 | * |
||
| 284 | * @return integer|null |
||
| 285 | */ |
||
| 286 | public function getNextPage() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get the previous page number |
||
| 297 | * |
||
| 298 | * @return integer|null |
||
| 299 | */ |
||
| 300 | public function getPrevPage() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get the js call to the next page |
||
| 311 | * |
||
| 312 | * @return string|null |
||
| 313 | */ |
||
| 314 | public function getNextCall() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get the js call to the previous page |
||
| 325 | * |
||
| 326 | * @return string|null |
||
| 327 | */ |
||
| 328 | public function getPrevCall() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get an array of paginated page data. |
||
| 339 | * |
||
| 340 | * Example: |
||
| 341 | * [ |
||
| 342 | * array ('num' => 1, 'call' => '/example/page/1', 'isCurrent' => false), |
||
| 343 | * array ('num' => '...', 'call' => NULL, 'isCurrent' => false), |
||
| 344 | * array ('num' => 3, 'call' => '/example/page/3', 'isCurrent' => false), |
||
| 345 | * array ('num' => 4, 'call' => '/example/page/4', 'isCurrent' => true ), |
||
| 346 | * array ('num' => 5, 'call' => '/example/page/5', 'isCurrent' => false), |
||
| 347 | * array ('num' => '...', 'call' => NULL, 'isCurrent' => false), |
||
| 348 | * array ('num' => 10, 'call' => '/example/page/10', 'isCurrent' => false), |
||
| 349 | * ] |
||
| 350 | * |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | public function getPages() |
||
| 409 | |||
| 410 | |||
| 411 | /** |
||
| 412 | * Create a page data structure. |
||
| 413 | * |
||
| 414 | * @param int $pageNum |
||
| 415 | * |
||
| 416 | * @return array<string,integer|string|boolean> |
||
| 417 | */ |
||
| 418 | protected function createPage($pageNum) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Get the page ellipsis |
||
| 429 | * |
||
| 430 | * @return array<string,string|null|false> |
||
| 431 | */ |
||
| 432 | protected function createPageEllipsis() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Set the text for the previous page link |
||
| 443 | * |
||
| 444 | * @param string $text The text for the previous page link |
||
| 445 | * |
||
| 446 | * @return Paginator |
||
| 447 | */ |
||
| 448 | public function setPreviousText($text) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Get the text for the previous page link |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | public function getPreviousText() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Set the text for the next page link |
||
| 466 | * |
||
| 467 | * @param string $text The text for the previous page link |
||
| 468 | * |
||
| 469 | * @return Paginator |
||
| 470 | */ |
||
| 471 | public function setNextText($text) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get the text for the next page link |
||
| 479 | * |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | public function getNextText() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Render an HTML pagination control. |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | public function render() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Render an HTML pagination control. |
||
| 503 | * |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | public function __toString() |
||
| 510 | } |
||
| 511 |