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) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Render a plain html list with previous, next and all urls. The current page gets a current class on the list item. |
||
| 214 | * |
||
| 215 | * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator |
||
| 216 | * @param bool $full Return the full version of the URL in for the first page |
||
| 217 | * Ex. /users/page/1 instead of /users |
||
| 218 | * @param string $class Include class on pagination list |
||
| 219 | * Ex. <ul class="pagination"> |
||
| 220 | * @param bool $additionalLinks Include prev and next links on pagination list |
||
| 221 | * |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | public function renderPageList(LengthAwarePaginator $paginator, $full = false, $class = null, $additionalLinks = false) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Render html link tags for SEO indication of previous and next page. |
||
| 262 | * |
||
| 263 | * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator |
||
| 264 | * @param bool $full Return the full version of the URL in for the first page |
||
| 265 | * Ex. /users/page/1 instead of /users |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function renderRelLinks(LengthAwarePaginator $paginator, $full = false) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @deprecated in favor of renderPageList. |
||
| 293 | * |
||
| 294 | * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator |
||
| 295 | * @param bool $full Return the full version of the URL in for the first page |
||
| 296 | * Ex. /users/page/1 instead of /users |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public function renderHtml(LengthAwarePaginator $paginator, $full = false) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Generate a page URL, based on the request's current URL. |
||
| 307 | * |
||
| 308 | * @param int $page |
||
| 309 | * @param bool $full Return the full version of the URL in for the first page |
||
| 310 | * Ex. /users/page/1 instead of /users |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function pageUrl($page, $full = false) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Append the page query to a URL. |
||
| 335 | * |
||
| 336 | * @param string $url |
||
| 337 | * @param int $page |
||
| 338 | * @param bool $full Return the full version of the URL in for the first page |
||
| 339 | * Ex. /users/page/1 instead of /users |
||
| 340 | * |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | public function addPageQuery($url, $page, $full = false) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Register the Route::paginate macro. |
||
| 355 | */ |
||
| 356 | public function registerMacros() |
||
| 373 | } |
||
| 374 |
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: