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 $styles Include $styles on pagination list |
||
247 | * @param bool $additionalLinks Include prev and next links on pagination list |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | |||
252 | public function renderPageList(LengthAwarePaginator $paginator, $full = false, $styles = null, $additionalLinks = false): string |
||
306 | /** |
||
307 | * Render html link tags for SEO indication of previous and next page. |
||
308 | * |
||
309 | * @param LengthAwarePaginator $paginator |
||
310 | * @param bool $full Return the full version of the URL in for the first page |
||
311 | * Ex. /users/page/1 instead of /users |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | public function renderRelLinks(LengthAwarePaginator $paginator, $full = false): string |
||
336 | |||
337 | /** |
||
338 | * @param LengthAwarePaginator $paginator |
||
339 | * @param bool $full Return the full version of the URL in for the first page |
||
340 | * Ex. /users/page/1 instead of /users |
||
341 | * |
||
342 | * @return string |
||
343 | * @deprecated in favor of renderPageList. |
||
344 | */ |
||
345 | public function renderHtml(LengthAwarePaginator $paginator, $full = false): string |
||
349 | |||
350 | /** |
||
351 | * Generate a page URL, based on the request's current URL. |
||
352 | * |
||
353 | * @param int $page |
||
354 | * @param bool $full Return the full version of the URL in for the first page |
||
355 | * Ex. /users/page/1 instead of /users |
||
356 | * |
||
357 | * @return string|null |
||
358 | */ |
||
359 | public function pageUrl($page, $full = false): string |
||
377 | |||
378 | /** |
||
379 | * Append the page query to a URL. |
||
380 | * |
||
381 | * @param string $url |
||
382 | * @param int $page |
||
383 | * @param bool $full Return the full version of the URL in for the first page |
||
384 | * Ex. /users/page/1 instead of /users |
||
385 | * |
||
386 | * @return string |
||
387 | */ |
||
388 | public function addPageQuery($url, $page, $full = false): string |
||
397 | |||
398 | /** |
||
399 | * Register the Route::paginate macro. |
||
400 | */ |
||
401 | public function registerMacros(): void |
||
418 | } |
||
419 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: