Complex classes like Pagination 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 Pagination, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class Pagination |
||
| 5 | { |
||
| 6 | public static $current; |
||
| 7 | protected $queryString; |
||
| 8 | |||
| 9 | protected static $page; |
||
| 10 | protected static $pageURL; |
||
| 11 | protected static $lastpage; |
||
| 12 | protected static $totalPages; |
||
| 13 | |||
| 14 | public $pagerClass = 'pagination justify-content-center'; |
||
| 15 | public $liClass = 'page-item'; |
||
| 16 | public $liActiveClass = 'active'; |
||
| 17 | public $aClass = 'page-link'; |
||
| 18 | public $aActiveClass = ''; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Sets the class assigned to the UL element of the pagination object |
||
| 22 | * @param string $class This should be the class or classes that you wish to give to the pagination object |
||
| 23 | * @return $this |
||
| 24 | */ |
||
| 25 | 2 | public function setPaginationClass($class) |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Returns the class to give to the pagination object |
||
| 35 | * @return string The pagination class will be returned |
||
| 36 | */ |
||
| 37 | 4 | public function getPaginationClass() |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Sets the default li class |
||
| 44 | * @param string $class This should be the class that you want to all to all li elements |
||
| 45 | * @return $this |
||
| 46 | */ |
||
| 47 | 1 | public function setLiClass($class) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Gets the current li class |
||
| 55 | * @return string This should be the class to assign on li elements |
||
| 56 | */ |
||
| 57 | 3 | public function getLiClass() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Sets the active class to assign on the li elements |
||
| 64 | * @param string $class This should be the class to assign on active elements |
||
| 65 | * @return $this |
||
| 66 | */ |
||
| 67 | 1 | public function setLiActiveClass($class) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Returns the class to assign to active li elements |
||
| 75 | * @return string This should be the class to assign on active elements |
||
| 76 | */ |
||
| 77 | 4 | public function getLiActiveClass() |
|
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * Sets the default class on a elements |
||
| 85 | * @param string $class This should be the class to add to a elements |
||
| 86 | * @return $this |
||
| 87 | */ |
||
| 88 | 1 | public function setAClass($class) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Returns the class assigned to a elements |
||
| 96 | * @return string Returns the class assigned to a elements |
||
| 97 | */ |
||
| 98 | 3 | public function getAClass() |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Sets the class to assign to active a elements |
||
| 105 | * @param string $class This should be the class to add to active a elements |
||
| 106 | * @return $this |
||
| 107 | */ |
||
| 108 | 1 | public function setAActiveClass($class) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Returns the class assigned to active a elements |
||
| 116 | * @return string This should be the class to add to active a elements |
||
| 117 | */ |
||
| 118 | 3 | public function getAActiveClass() |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Returns paging buttons for the number of records |
||
| 125 | * @param int $records The total number of records |
||
| 126 | * @param string $pageURL The URL of the page you are creating the paging for |
||
| 127 | * @param int $start The start number for the results |
||
| 128 | * @param int $maxshown The number of records that are shown on each page |
||
| 129 | * @param int $numpagesshown The number of pagination buttons to display |
||
| 130 | * @param boolean $arrows If you want arrows to display before and after for next and previous set to true (default) else set to false |
||
| 131 | * @param array $additional Any additional get values to include in the URL |
||
| 132 | * @return string|false Returns the pagination menu if required else will return false |
||
| 133 | */ |
||
| 134 | 4 | public function paging($records, $pageURL, $start = 0, $maxshown = 50, $numpagesshown = 11, $arrows = true, $additional = array()) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Build a link item with the given values |
||
| 156 | * @param string $link This should be any additional items to be included as part of the link |
||
| 157 | * @param mixed $page This should be the link test on the link normally set as numbers but may be anything like arrows or dots etc |
||
| 158 | * @param boolean $current If this is the current link item set this as true so the class is added to the link item |
||
| 159 | * @return string This will return the paging item as a string |
||
| 160 | */ |
||
| 161 | 3 | protected function buildLink($link, $page, $current = false) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Builds the query string to add to the URL |
||
| 168 | * @param mixed $page If the page variable is set to a number will add the page number to the query string else will not add any additional items |
||
| 169 | * @return string The complete string will be returned to add to the link item |
||
| 170 | */ |
||
| 171 | 3 | protected function buildQueryString($page) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Gets the current page |
||
| 179 | * @param int $records The total number of records |
||
| 180 | * @param int $maxshown The number of records that are shown on each page |
||
| 181 | * @param int $numpages The number of pagination buttons to display |
||
| 182 | * return void Nothing is returned |
||
| 183 | */ |
||
| 184 | 3 | protected function getPage($records, $maxshown, $numpages) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the previous arrows as long as arrows is set to true and the page is not the first page |
||
| 201 | * @param boolean $arrows If you want to display previous arrows set to true else set to false |
||
| 202 | * @return string Any previous link arrows will be returned as a string |
||
| 203 | */ |
||
| 204 | 3 | protected function preLinks($arrows = true) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Returns the next arrows as long as arrows is set to true and the page is not the last page |
||
| 218 | * @param boolean $arrows If you want to display next arrows set to true else set to false |
||
| 219 | * @return string Any next link arrows will be returned as a string |
||
| 220 | */ |
||
| 221 | 3 | protected function postLinks($arrows = true) |
|
| 232 | } |
||
| 233 |