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 |
||
| 13 | class PaginateRoute |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var Translator |
||
| 17 | */ |
||
| 18 | protected $translator; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var Router |
||
| 22 | */ |
||
| 23 | protected $router; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var UrlGenerator |
||
| 27 | */ |
||
| 28 | protected $urlGenerator; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string|array |
||
| 32 | */ |
||
| 33 | protected $pageKeyword; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param Translator $translator |
||
| 37 | * @param Router $router |
||
| 38 | * @param UrlGenerator $urlGenerator |
||
| 39 | */ |
||
| 40 | public function __construct(Translator $translator, Router $router, UrlGenerator $urlGenerator) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Return the current page. |
||
| 54 | * |
||
| 55 | * @return int |
||
| 56 | */ |
||
| 57 | public function currentPage() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Check if the given page is the current page. |
||
| 72 | * |
||
| 73 | * @param int $page |
||
| 74 | * |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | public function isCurrentPage($page): bool |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get the next page number. |
||
| 84 | * |
||
| 85 | * @param Paginator $paginator |
||
| 86 | * |
||
| 87 | * @return int|void |
||
| 88 | */ |
||
| 89 | public function nextPage(Paginator $paginator) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Determine wether there is a next page. |
||
| 100 | * |
||
| 101 | * @param Paginator $paginator |
||
| 102 | * |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | public function hasNextPage(Paginator $paginator): bool |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get the next page URL. |
||
| 112 | * |
||
| 113 | * @param Paginator $paginator |
||
| 114 | * |
||
| 115 | * @return string|void |
||
| 116 | */ |
||
| 117 | public function nextPageUrl(Paginator $paginator) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get the previous page number. |
||
| 130 | * |
||
| 131 | * @return int|void|null |
||
| 132 | */ |
||
| 133 | public function previousPage() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Determine wether there is a previous page. |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function hasPreviousPage(): bool |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get the previous page URL. |
||
| 154 | * |
||
| 155 | * @param bool $full Return the full version of the URL in for the first page |
||
| 156 | * Ex. /users/page/1 instead of /users |
||
| 157 | * |
||
| 158 | * @return string|void|null |
||
| 159 | */ |
||
| 160 | public function previousPageUrl($full = false): ?string |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get all urls in an array. |
||
| 173 | * |
||
| 174 | * @param LengthAwarePaginator $paginator |
||
| 175 | * @param bool $full Return the full version of the URL in for the first page |
||
| 176 | * Ex. /users/page/1 instead of /users |
||
| 177 | * |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | public function allUrls(LengthAwarePaginator $paginator, $full = false): array |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get the left most point in the pagination element. |
||
| 198 | * |
||
| 199 | * @param LengthAwarePaginator $paginator |
||
| 200 | * @return int |
||
| 201 | */ |
||
| 202 | public function getLeftPoint(LengthAwarePaginator $paginator): int |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get the right or last point of the pagination element. |
||
| 221 | * |
||
| 222 | * @param LengthAwarePaginator $paginator |
||
| 223 | * @return int |
||
| 224 | */ |
||
| 225 | public function getRightPoint(LengthAwarePaginator $paginator): int |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Render a plain html list with previous, next and all urls. The current page gets a current class on the list item. |
||
| 243 | * |
||
| 244 | * @param LengthAwarePaginator $paginator |
||
| 245 | * @param bool $full Return the full version of the URL in for the first page |
||
| 246 | * Ex. /users/page/1 instead of /users |
||
| 247 | * @param string $class Include class on pagination list |
||
| 248 | * Ex. <ul class="pagination"> |
||
| 249 | * @param bool $additionalLinks Include prev and next links on pagination list |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | |||
| 254 | public function renderPageList(LengthAwarePaginator $paginator, $full = false, $class = null, $additionalLinks = false) |
||
| 280 | /** |
||
| 281 | * Render html link tags for SEO indication of previous and next page. |
||
| 282 | * |
||
| 283 | * @param LengthAwarePaginator $paginator |
||
| 284 | * @param bool $full Return the full version of the URL in for the first page |
||
| 285 | * Ex. /users/page/1 instead of /users |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function renderRelLinks(LengthAwarePaginator $paginator, $full = false): string |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param LengthAwarePaginator $paginator |
||
| 313 | * @param bool $full Return the full version of the URL in for the first page |
||
| 314 | * Ex. /users/page/1 instead of /users |
||
| 315 | * |
||
| 316 | * @return string |
||
| 317 | *@deprecated in favor of renderPageList. |
||
| 318 | * |
||
| 319 | */ |
||
| 320 | public function renderHtml(LengthAwarePaginator $paginator, $full = false): string |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Generate a page URL, based on the request's current URL. |
||
| 327 | * |
||
| 328 | * @param int $page |
||
| 329 | * @param bool $full Return the full version of the URL in for the first page |
||
| 330 | * Ex. /users/page/1 instead of /users |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function pageUrl($page, $full = false): string |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Append the page query to a URL. |
||
| 355 | * |
||
| 356 | * @param string $url |
||
| 357 | * @param int $page |
||
| 358 | * @param bool $full Return the full version of the URL in for the first page |
||
| 359 | * Ex. /users/page/1 instead of /users |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public function addPageQuery($url, $page, $full = false): string |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Register the Route::paginate macro. |
||
| 375 | */ |
||
| 376 | public function registerMacros(): void |
||
| 393 | } |
||
| 394 |
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: