Complex classes like PaginateRoute 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 PaginateRoute, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class PaginateRoute |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var Translator |
||
| 16 | */ |
||
| 17 | protected $translator; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var Router |
||
| 21 | */ |
||
| 22 | protected $router; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var UrlGenerator |
||
| 26 | */ |
||
| 27 | protected $urlGenerator; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string|array |
||
| 31 | */ |
||
| 32 | protected $pageKeyword; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param Translator $translator |
||
| 36 | * @param Router $router |
||
| 37 | * @param UrlGenerator $urlGenerator |
||
| 38 | */ |
||
| 39 | public function __construct(Translator $translator, Router $router, UrlGenerator $urlGenerator) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Return the current page. |
||
| 53 | * |
||
| 54 | * @return int |
||
| 55 | */ |
||
| 56 | public function currentPage() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Check if the given page is the current page. |
||
| 71 | * |
||
| 72 | * @param int $page |
||
| 73 | * |
||
| 74 | * @return bool |
||
| 75 | */ |
||
| 76 | public function isCurrentPage($page): bool |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get the next page number. |
||
| 83 | * |
||
| 84 | * @param LengthAwarePaginator $paginator |
||
| 85 | * |
||
| 86 | * @return int|void |
||
| 87 | */ |
||
| 88 | public function nextPage(LengthAwarePaginator $paginator) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Determine weather there is a next page. |
||
| 99 | * |
||
| 100 | * @param LengthAwarePaginator $paginator |
||
| 101 | * |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function hasNextPage(LengthAwarePaginator $paginator): bool |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get the next page URL. |
||
| 111 | * |
||
| 112 | * @param LengthAwarePaginator $paginator |
||
| 113 | * |
||
| 114 | * @return string|void |
||
| 115 | */ |
||
| 116 | public function nextPageUrl(LengthAwarePaginator $paginator) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the previous page number. |
||
| 129 | * |
||
| 130 | * @return int|void|null |
||
| 131 | */ |
||
| 132 | public function previousPage() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Determine wether there is a previous page. |
||
| 143 | * |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | public function hasPreviousPage(): bool |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get the previous page URL. |
||
| 153 | * |
||
| 154 | * @param bool $full Return the full version of the URL in for the first page |
||
| 155 | * Ex. /users/page/1 instead of /users |
||
| 156 | * |
||
| 157 | * @return string|void|null |
||
| 158 | */ |
||
| 159 | public function previousPageUrl($full = false): ?string |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get all urls in an array. |
||
| 172 | * |
||
| 173 | * @param LengthAwarePaginator $paginator |
||
| 174 | * @param bool $full Return the full version of the URL in for the first page |
||
| 175 | * Ex. /users/page/1 instead of /users |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | public function allUrls(LengthAwarePaginator $paginator, $full = false): array |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get the left most point in the pagination element. |
||
| 197 | * |
||
| 198 | * @param LengthAwarePaginator $paginator |
||
| 199 | * @return int |
||
| 200 | */ |
||
| 201 | public function getLeftPoint(LengthAwarePaginator $paginator): int |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the right or last point of the pagination element. |
||
| 220 | * |
||
| 221 | * @param LengthAwarePaginator $paginator |
||
| 222 | * @return int |
||
| 223 | */ |
||
| 224 | public function getRightPoint(LengthAwarePaginator $paginator): int |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Render a plain html list with previous, next and all urls. The current page gets a current class on the list item. |
||
| 242 | * |
||
| 243 | * @param LengthAwarePaginator $paginator |
||
| 244 | * @param bool $full Return the full version of the URL in for the first page |
||
| 245 | * Ex. /users/page/1 instead of /users |
||
| 246 | * @param string $class Include class on pagination list |
||
| 247 | * Ex. <ul class="pagination"> |
||
| 248 | * @param bool $additionalLinks Include prev and next links on pagination list |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | |||
| 253 | public function renderPageList(LengthAwarePaginator $paginator, $full = false, $class = null, $additionalLinks = false): string |
||
| 279 | /** |
||
| 280 | * Render html link tags for SEO indication of previous and next page. |
||
| 281 | * |
||
| 282 | * @param LengthAwarePaginator $paginator |
||
| 283 | * @param bool $full Return the full version of the URL in for the first page |
||
| 284 | * Ex. /users/page/1 instead of /users |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function renderRelLinks(LengthAwarePaginator $paginator, $full = false): string |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param LengthAwarePaginator $paginator |
||
| 312 | * @param bool $full Return the full version of the URL in for the first page |
||
| 313 | * Ex. /users/page/1 instead of /users |
||
| 314 | * |
||
| 315 | * @return string |
||
| 316 | * @deprecated in favor of renderPageList. |
||
| 317 | */ |
||
| 318 | public function renderHtml(LengthAwarePaginator $paginator, $full = false): string |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Generate a page URL, based on the request's current URL. |
||
| 325 | * |
||
| 326 | * @param int $page |
||
| 327 | * @param bool $full Return the full version of the URL in for the first page |
||
| 328 | * Ex. /users/page/1 instead of /users |
||
| 329 | * |
||
| 330 | * @return string|null |
||
| 331 | */ |
||
| 332 | public function pageUrl($page, $full = false): string |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Append the page query to a URL. |
||
| 353 | * |
||
| 354 | * @param string $url |
||
| 355 | * @param int $page |
||
| 356 | * @param bool $full Return the full version of the URL in for the first page |
||
| 357 | * Ex. /users/page/1 instead of /users |
||
| 358 | * |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | public function addPageQuery($url, $page, $full = false): string |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Register the Route::paginate macro. |
||
| 373 | */ |
||
| 374 | public function registerMacros(): void |
||
| 391 | } |
||
| 392 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: