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 |
||
222 | |||
223 | /** |
||
224 | * Get the right or last point of the pagination element. |
||
225 | * |
||
226 | * @param LengthAwarePaginator $paginator |
||
227 | * @return int |
||
228 | */ |
||
229 | public function getRightPoint(LengthAwarePaginator $paginator): int |
||
244 | |||
245 | /** |
||
246 | * Render a plain html list with previous, next and all urls. The current page gets a current class on the list item. |
||
247 | * |
||
248 | * @param LengthAwarePaginator $paginator |
||
249 | * @param bool $full Return the full version of the URL in for the first page |
||
250 | * Ex. /users/page/1 instead of /users |
||
251 | * @param string $class Include class on pagination list |
||
252 | * Ex. <ul class="pagination"> |
||
253 | * @param bool $additionalLinks Include prev and next links on pagination list |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | |||
258 | public function renderPageList(LengthAwarePaginator $paginator, $full = false, $class = null, $additionalLinks = false) |
||
284 | /** |
||
285 | * Render html link tags for SEO indication of previous and next page. |
||
286 | * |
||
287 | * @param 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): string |
||
314 | |||
315 | /** |
||
316 | * @param LengthAwarePaginator $paginator |
||
317 | * @param bool $full Return the full version of the URL in for the first page |
||
318 | * Ex. /users/page/1 instead of /users |
||
319 | * |
||
320 | * @return string |
||
321 | *@deprecated in favor of renderPageList. |
||
322 | * |
||
323 | */ |
||
324 | public function renderHtml(LengthAwarePaginator $paginator, $full = false): string |
||
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): string |
||
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): string |
||
376 | |||
377 | /** |
||
378 | * Register the Route::paginate macro. |
||
379 | */ |
||
380 | public function registerMacros(): void |
||
397 | } |
||
398 |
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: