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 \Illuminate\Translation\Translator | ||
| 17 | */ | ||
| 18 | protected $translator; | ||
| 19 | |||
| 20 | /** | ||
| 21 | * @var \Illuminate\Routing\Router | ||
| 22 | */ | ||
| 23 | protected $router; | ||
| 24 | |||
| 25 | /** | ||
| 26 | * @var \Illuminate\Contracts\Routing\UrlGenerator | ||
| 27 | */ | ||
| 28 | protected $urlGenerator; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * @var string | ||
| 32 | */ | ||
| 33 | protected $pageKeyword; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * @param \Illuminate\Translation\Translator $translator | ||
| 37 | * @param \Illuminate\Routing\Router $router | ||
| 38 | * @param \Illuminate\Contracts\Routing\UrlGenerator $urlGenerator | ||
| 39 | */ | ||
| 40 | public function __construct(Translator $translator, Router $router, UrlGenerator $urlGenerator) | ||
| 41 |     { | ||
| 42 | $this->translator = $translator; | ||
| 43 | $this->router = $router; | ||
| 44 | $this->urlGenerator = $urlGenerator; | ||
| 45 | |||
| 46 | // Unfortunately we can't do this in the service provider since routes are booted first | ||
| 47 |         $this->translator->addNamespace('paginateroute', __DIR__.'/../resources/lang'); | ||
| 48 | |||
| 49 |         $this->pageKeyword = $this->translator->get('paginateroute::paginateroute.page'); | ||
| 50 | } | ||
| 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) | ||
| 81 | |||
| 82 | /** | ||
| 83 | * Get the next page number. | ||
| 84 | * | ||
| 85 | * @param \Illuminate\Contracts\Pagination\Paginator $paginator | ||
| 86 | * | ||
| 87 | * @return string|null | ||
| 88 | */ | ||
| 89 | public function nextPage(Paginator $paginator) | ||
| 97 | |||
| 98 | /** | ||
| 99 | * Determine wether there is a next page. | ||
| 100 | * | ||
| 101 | * @param \Illuminate\Contracts\Pagination\Paginator $paginator | ||
| 102 | * | ||
| 103 | * @return bool | ||
| 104 | */ | ||
| 105 | public function hasNextPage(Paginator $paginator) | ||
| 109 | |||
| 110 | /** | ||
| 111 | * Get the next page URL. | ||
| 112 | * | ||
| 113 | * @param \Illuminate\Contracts\Pagination\Paginator $paginator | ||
| 114 | * | ||
| 115 | * @return string|null | ||
| 116 | */ | ||
| 117 | public function nextPageUrl(Paginator $paginator) | ||
| 118 |     { | ||
| 119 | $nextPage = $this->nextPage($paginator); | ||
| 120 | |||
| 121 |         if ($nextPage === null) { | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | |||
| 125 | return $this->pageUrl($nextPage); | ||
| 126 | } | ||
| 127 | |||
| 128 | /** | ||
| 129 | * Get the previous page number. | ||
| 130 | * | ||
| 131 | * @return string|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() | ||
| 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|null | ||
| 159 | */ | ||
| 160 | public function previousPageUrl($full = false) | ||
| 170 | /** | ||
| 171 | * Get all urls in an array. | ||
| 172 | * | ||
| 173 | * @param \Illuminate\Contracts\Pagination\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) | ||
| 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) | ||
| 215 | |||
| 216 | /** | ||
| 217 | * Get the right or last point of the pagination element | ||
| 218 | * | ||
| 219 | * @param LengthAwarePaginator $paginator | ||
| 220 | * @return int | ||
| 221 | */ | ||
| 222 | public function getRightPoint(LengthAwarePaginator $paginator) | ||
| 235 | |||
| 236 | /** | ||
| 237 | * Render a plain html list with previous, next and all urls. The current page gets a current class on the list item. | ||
| 238 | * | ||
| 239 | * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator | ||
| 240 | * @param bool $full Return the full version of the URL in for the first page | ||
| 241 | * Ex. /users/page/1 instead of /users | ||
| 242 | * @param string $class Include class on pagination list | ||
| 243 | * Ex. <ul class="pagination"> | ||
| 244 | * @param bool $additionalLinks Include prev and next links on pagination list | ||
| 245 | * | ||
| 246 | * @return string | ||
| 247 | */ | ||
| 248 | public function renderPageList(LengthAwarePaginator $paginator, $full = false, $class = null, $additionalLinks = false) | ||
| 283 | |||
| 284 | /** | ||
| 285 | * Render html link tags for SEO indication of previous and next page. | ||
| 286 | * | ||
| 287 | * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator | ||
| 288 | * @param bool $full Return the full version of the URL in for the first page | ||
| 289 | * Ex. /users/page/1 instead of /users | ||
| 290 | * | ||
| 291 | * @return string | ||
| 292 | */ | ||
| 293 | public function renderRelLinks(LengthAwarePaginator $paginator, $full = false) | ||
| 314 | |||
| 315 | /** | ||
| 316 | * @deprecated in favor of renderPageList. | ||
| 317 | * | ||
| 318 | * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator | ||
| 319 | * @param bool $full Return the full version of the URL in for the first page | ||
| 320 | * Ex. /users/page/1 instead of /users | ||
| 321 | * | ||
| 322 | * @return string | ||
| 323 | */ | ||
| 324 | public function renderHtml(LengthAwarePaginator $paginator, $full = false) | ||
| 328 | |||
| 329 | /** | ||
| 330 | * Generate a page URL, based on the request's current URL. | ||
| 331 | * | ||
| 332 | * @param int $page | ||
| 333 | * @param bool $full Return the full version of the URL in for the first page | ||
| 334 | * Ex. /users/page/1 instead of /users | ||
| 335 | * | ||
| 336 | * @return string | ||
| 337 | */ | ||
| 338 | public function pageUrl($page, $full = false) | ||
| 356 | |||
| 357 | /** | ||
| 358 | * Append the page query to a URL. | ||
| 359 | * | ||
| 360 | * @param string $url | ||
| 361 | * @param int $page | ||
| 362 | * @param bool $full Return the full version of the URL in for the first page | ||
| 363 | * Ex. /users/page/1 instead of /users | ||
| 364 | * | ||
| 365 | * @return string | ||
| 366 | */ | ||
| 367 | public function addPageQuery($url, $page, $full = false) | ||
| 376 | |||
| 377 | /** | ||
| 378 | * Register the Route::paginate macro. | ||
| 379 | */ | ||
| 380 | public function registerMacros() | ||
| 397 | } | ||
| 398 | 
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: